Commit 9aa9708a

9aa9708afdafMerged

Ryan Seys <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

codegen,analyze: build Array#to_h pair form from each pair's array kind

[[1, 2], [3, 4]].to_h returned {0 => 0}. The pair-form lowering cast each element to sp_PolyArray * and read it with sp_PolyArray_get, but a pair of like-typed values is a homogeneous typed array ([1, 2] is an IntArray, ["a", "b"] a StrArray), so the read landed on the wrong struct layout and yielded zero. Mixed pairs (["a", 1], [:x, 10]) happened to be PolyArrays and worked.

Extract each key and value with sp_poly_arr_get, which boxes an element from any array kind by dispatching on its cls_id, then unbox to the result hash's key/value C types. Also root the receiver array across the hash-building loop (a set can rehash/allocate), and add the missing poly-key arm.

A non-literal receiver (pairs.to_h for a method param) cannot have its pair element types resolved statically, so infer a fully boxed PolyPolyHash instead of defaulting to SymPolyHash, which mis-rendered string keys as empty symbols.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
9aa9708afdaf
Parent
3d4efd9a07a7

src/analyze_infer.cmodifiedView file

@@ -1903,7 +1903,10 @@
19031903 }
19041904 }
19051905 }
1906- return TY_SYM_POLY_HASH;
1906+ /* Non-literal receiver: the pair element types are not statically known
1907+ (e.g. `a.to_h` for a method param), so a fully boxed hash preserves
1908+ whatever keys/values the pairs hold instead of mis-typing them. */
1909+ return TY_POLY_POLY_HASH;
19071910 }
19081911 }
19091912