Commit 080abef9

080abef961deMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(codegen,analyze): heterogeneous <int_array literal> << x lifts to poly_array

([1, 2] << "c") == [1, 2, "c"] should fold to true: CRuby's << accepts any element type and the resulting array stays heterogeneous. Spinel's << arm for int_array recv pushed the arg through sp_IntArray_push unconditionally, so a string arg landed via pointer-from-int conversion and the result compared unequal to the RHS poly_array literal.

Lift only when the recv is a direct ArrayNode literal (i.e. a transient [1, 2]) and the arg type is non-int -- the lift rebuilds the recv's elements as box_int(...) pushes into a fresh poly_array, then pushes the boxed arg. Mirrored in the analyzer's << return-type rule so the surrounding == picks the poly_array dispatch.

Scoped to ArrayNode literals so a stored ivar / local (the optcarrot @slots = [nil] * N; @slots << ... shape) keeps its existing observation-widening path and doesn't cascade typed-int reads into poly.

Refs #619 puzzle 7. Closes the last #619 puzzle along with the rest of this issue's series (puzzles 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10).

Closes #619

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
080abef961de
Parent
e34a705af2d2

spinel_analyze.rbmodifiedView file

@@ -3672,6 +3672,25 @@
36723672 return "string"
36733673 end
36743674 # Array `<<` returns the recv (so `(arr << x) << y` chains).
3675+ # `<int_array literal> << <non-int>` (`[1, 2] << "c"`) lifts to
3676+ # poly_array at codegen so the result holds the heterogeneous
3677+ # elements; mirror that in inference so the surrounding `==`
3678+ # picks the poly_array dispatch arm rather than treating one
3679+ # side as int_array. Issue #619 puzzle 7.
3680+ if lt == "int_array" && @nd_type[recv] == "ArrayNode"
3681+ args_id_p7 = @nd_arguments[nid]
3682+ if args_id_p7 >= 0
3683+ aa_p7 = get_args(args_id_p7)
3684+ if aa_p7.length > 0
3685+ at_p7 = infer_type(aa_p7[0])
3686+ if at_p7 != "int" && at_p7 != "bool" && at_p7 != "nil"
3687+ @needs_rb_value = 1
3688+ @needs_gc = 1
3689+ return "poly_array"
3690+ end
3691+ end
3692+ end
3693+ end
36753694 if is_array_type(lt) == 1
36763695 return lt
36773696 end