Commit 2273c1ed

2273c1ed0e30Merged

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

Description

fix(analyze,codegen): Array.new(n, nil) returns poly_array of nils

Array.new(3, nil) in MRI fills each slot with the nil singleton and .inspect prints "[nil, nil, nil]". Spinel previously lowered the call to a sp_IntArray pushing the C default 0, and inspect printed "[0, 0, 0]" -- the fill value's nil type never picked up a dedicated arm in either the analyzer's Array.new dispatch or the codegen's compile_ constructor_expr.

Add a "nil" arm in both, parallel to the "poly" arm. The result is sp_PolyArray filled with sp_box_nil(), so downstream [i].nil? / inspect / etc. see the real nil tag.

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

Details

Commit
2273c1ed0e30
Parent
712d6114c584

spinel_analyze.rbmodifiedView file

@@ -5235,6 +5235,15 @@
52355235 @needs_rb_value = 1
52365236 return "poly_array"
52375237 end
5238+ # `Array.new(n, nil)` fills each slot with the nil singleton;
5239+ # the element shape is sp_RbVal with the nil tag, so the
5240+ # result must be poly_array (not int_array filled with 0).
5241+ # Mirrors the codegen arm in compile_constructor_expr.
5242+ if vt == "nil"
5243+ @needs_rb_value = 1
5244+ @needs_gc = 1
5245+ return "poly_array"
5246+ end
52385247 if type_is_pointer(vt) == 1
52395248 @needs_gc = 1
52405249 return vt + "_ptr_array"