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:

  1. 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.

  2. 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

@@ -5561,6 +5561,15 @@
55615561 return vt + "_ptr_array"
55625562 end
55635563 end
5564+ # `Array.new(n)` (single arg, no fill value, no block) is CRuby
5565+ # shorthand for `Array.new(n, nil)`, so the result is a
5566+ # poly_array of n nils. Mirrors the codegen arm. Issue #619
5567+ # puzzle 4.
5568+ if aargs.length == 1 && @nd_block[nid] < 0
5569+ @needs_rb_value = 1
5570+ @needs_gc = 1
5571+ return "poly_array"
5572+ end
55645573 end
55655574 return "int_array"
55665575 end
@@ -9798,6 +9807,12 @@
97989807 return vt + "_ptr_array"
97999808 end
98009809 end
9810+ # `Array.new(n)` (single arg) -> n nils. Issue #619 puzzle 4.
9811+ if aargs.length == 1 && @nd_block[nid] < 0
9812+ @needs_rb_value = 1
9813+ @needs_gc = 1
9814+ return "poly_array"
9815+ end
98019816 end
98029817 return "int_array"
98039818 end