Commit 690d45c6
690d45c6482eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): Array.new(n) builds poly_array of n nils
Array.new(3) is CRuby shorthand for Array.new(3, nil): an array
of three nil values, NOT an empty array. Spinel's single-arg
lowering fell through to sp_IntArray_new() (length 0) so
.length / .inspect / [i].nil? saw the wrong shape. The
matching Array.new(n, nil) arm just above already emitted a
poly_array of n nils; the single-arg form was just missing.
Two pieces:
-
spinel_analyze.rb: infer_type for
Array.new(n)(single arg, no block) now returns "poly_array" instead of "int_array". Mirrored in both inference sites. -
spinel_codegen.rb: compile_constructor_expr's Array arm emits the same poly_array + sp_box_nil() fill loop the
Array.new(n, nil)arm uses.
Refs #619 puzzle 4. Doesn't yet close the full Array.new(3) == [nil, nil, nil] comparison because the RHS literal stays
int_array of zeros (lifting [nil, ...] literals to poly_array
broke other patterns -- the optcarrot [nil] * N initialization
shape relies on the int_array starting type so observation-based
widening picks the right concrete variant later).
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 690d45c6482e
- Parent
- 97be67625233
spinel_analyze.rbmodifiedView file
return vt + "_ptr_array" end end # `Array.new(n)` (single arg, no fill value, no block) is CRuby # shorthand for `Array.new(n, nil)`, so the result is a # poly_array of n nils. Mirrors the codegen arm. Issue #619 # puzzle 4. if aargs.length == 1 && @nd_block[nid] < 0 @needs_rb_value = 1 @needs_gc = 1 return "poly_array" end end return "int_array" end return vt + "_ptr_array" end end # `Array.new(n)` (single arg) -> n nils. Issue #619 puzzle 4. if aargs.length == 1 && @nd_block[nid] < 0 @needs_rb_value = 1 @needs_gc = 1 return "poly_array" end end return "int_array" endspinel_codegen.rbmodifiedView file
Loading diff…
test/array_new_n_fills_nil.rbaddedView file
Loading diff…
test/array_new_n_fills_nil.rb.expectedaddedView file
Loading diff…