Commit e369fb86

e369fb8640e3Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): Array.new(N) { block } picks accumulator from block return type

Previously Array.new(N) { ... } always emitted sp_IntArray and pushed the block result via sp_IntArray_push, regardless of the block's return type. Block returning typed arrays (Array.new(N) { [].dup }, Array.new(N) { _a = [0]; _a.clear; _a }) ended up with pointer-sized IntArray slots that the GC swept silently — later reads dereferenced freed inner arrays.

Mirror compile_map_expr's range / typed-container branch: infer the block body's last-statement type and emit StrArray / FloatArray / PtrArray / PolyArray when appropriate. Three call sites need the same logic (infer_type, infer_ivar_init_type, compile_constructor_expr) so the ivar widening pipeline stays consistent. infer_ivar_init_type returns 'int' (placeholder) when the block tail can't be resolved at the pre-compile pass — the later compile-pass refinement via update_ivar_type then widens int → concrete-container without going through the array-vs-array → poly_array widening.

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

Details

Commit
e369fb8640e3
Parent
2e1c2c409741

spinel_codegen.rbmodifiedView file

@@ -4273,6 +4273,42 @@
42734273 rn = constructor_class_name(recv)
42744274 if rn != ""
42754275 if rn == "Array"
4276+ # Block form `Array.new(n) { ... }` — infer the container
4277+ # from the block's tail expression, same shape as the
4278+ # compile_constructor_expr emit. Without this branch
4279+ # ivar widening saw `int_array` and the actual emit's
4280+ # PtrArray / PolyArray clashed.
4281+ blk_an = @nd_block[nid]
4282+ if blk_an >= 0
4283+ body_an = @nd_body[blk_an]
4284+ if body_an >= 0
4285+ stmts_an = get_stmts(body_an)
4286+ if stmts_an.length > 0
4287+ bret = infer_type(stmts_an.last)
4288+ if bret == "string"
4289+ return "str_array"
4290+ end
4291+ if bret == "float"
4292+ return "float_array"
4293+ end
4294+ if bret == "symbol"
4295+ return "sym_array"
4296+ end
4297+ if bret == "poly"
4298+ @needs_rb_value = 1
4299+ return "poly_array"
4300+ end
4301+ if is_ptr_array_type(bret) == 1 || bret == "poly_array"
4302+ @needs_rb_value = 1
4303+ return "poly_array"
4304+ end
4305+ if bret == "int_array" || bret == "float_array" || bret == "str_array" || bret == "sym_array"
4306+ @needs_gc = 1
4307+ return bret + "_ptr_array"
4308+ end
4309+ end
4310+ end
4311+ end
42764312 # Check fill value type. Pointer-type fills must produce a typed
42774313 # PtrArray; falling through to int_array would leave the
42784314 # elements unscanned by GC.
@@ -8018,6 +8054,51 @@
80188054 rname = constructor_class_name(r)
80198055 if rname != ""
80208056 if rname == "Array"
8057+ # Block form `Array.new(n) { ... }` — infer container
8058+ # from the block tail (matches compile_constructor_expr).
8059+ # When the block tail's type can't be resolved at this
8060+ # pre-compile pass (a LocalVariableReadNode whose write
8061+ # is in the same block, e.g. `_a = [0]; _a.clear; _a`,
8062+ # hasn't been declare_var'd yet — find_var_type returns
8063+ # ""), fall through to the placeholder "int" path so
8064+ # the later compile pass's update_ivar_type can widen
8065+ # int → concrete-container without going through the
8066+ # array+array → poly_array widening.
8067+ blk_an2 = @nd_block[nid]
8068+ if blk_an2 >= 0
8069+ body_an2 = @nd_body[blk_an2]
8070+ if body_an2 >= 0
8071+ stmts_an2 = get_stmts(body_an2)
8072+ if stmts_an2.length > 0
8073+ bret2 = infer_type(stmts_an2.last)
8074+ if bret2 == "string"
8075+ return "str_array"
8076+ end
8077+ if bret2 == "float"
8078+ return "float_array"
8079+ end
8080+ if bret2 == "symbol"
8081+ return "sym_array"
8082+ end
8083+ if bret2 == "poly"
8084+ @needs_rb_value = 1
8085+ return "poly_array"
8086+ end
8087+ if is_ptr_array_type(bret2) == 1 || bret2 == "poly_array"
8088+ @needs_rb_value = 1
8089+ return "poly_array"
8090+ end
8091+ if bret2 == "int_array" || bret2 == "float_array" || bret2 == "str_array" || bret2 == "sym_array"
8092+ @needs_gc = 1
8093+ return bret2 + "_ptr_array"
8094+ end
8095+ # bret2 == "int" or "" — leave the slot untyped so
8096+ # the later compile pass can refine without
8097+ # triggering update_ivar_type's array-widening.
8098+ return "int"
8099+ end
8100+ end
8101+ end
80218102 # Check fill value type for Array.new(n, val).
80228103 # Pointer-type fills must produce a typed PtrArray; falling
80238104 # through to int_array would leave the elements unscanned by GC.
@@ -23084,11 +23165,16 @@
2308423165 if cname == "Array"
2308523166 @needs_gc = 1
2308623167 args_id = @nd_arguments[nid]
23087- # Array.new(n) { |i| ... } -- IntArray-only fast path. We don't
23088- # try to introspect the block body to pick a typed container
23089- # (calling infer_type from this dispatch perturbs the bootstrap;
23090- # see bug-11 commit). Float/String collectors must be built via
23091- # explicit `[]` + `N.times { ... << }` instead.
23168+ # Array.new(n) { |i| ... } — pick the accumulator container
23169+ # from the block's last-statement type, mirroring
23170+ # compile_map_expr's range/typed-container branch. Without the
23171+ # introspection, every block return collapsed into IntArray
23172+ # (`Array.new(N) { [].dup }` ended up as an int_array of stale
23173+ # pointers, freed by GC). The infer_type call here is on the
23174+ # block body's tail expression — that recursion is bounded by
23175+ # the AST and doesn't perturb the bootstrap (the original
23176+ # comment about bug-11 came from a now-removed call site that
23177+ # re-entered constructor inference).
2309223178 if args_id >= 0 && @nd_block[nid] >= 0
2309323179 arrnew_aargs = get_args(args_id)
2309423180 if arrnew_aargs.length >= 1
@@ -23096,10 +23182,36 @@
2309623182 arrnew_body = @nd_body[arrnew_blk]
2309723183 arrnew_count = compile_expr(arrnew_aargs.first)
2309823184 arrnew_bp = get_block_param(nid, 0)
23185+ block_ret_an = "int"
23186+ if arrnew_body >= 0
23187+ arrnew_pre_stmts = get_stmts(arrnew_body)
23188+ if arrnew_pre_stmts.length > 0
23189+ block_ret_an = infer_type(arrnew_pre_stmts.last)
23190+ end
23191+ end
23192+ an_ret_is_arr = (block_ret_an == "int_array" || block_ret_an == "float_array" || block_ret_an == "str_array" || block_ret_an == "sym_array")
23193+ an_ret_is_deep_arr = (is_ptr_array_type(block_ret_an) == 1 || block_ret_an == "poly_array")
2309923194 arrnew_tmp = new_temp
2310023195 arrnew_iv = new_temp
23101- @needs_int_array = 1
23102- emit(" sp_IntArray *" + arrnew_tmp + " = sp_IntArray_new();")
23196+ if block_ret_an == "string"
23197+ @needs_str_array = 1
23198+ emit(" sp_StrArray *" + arrnew_tmp + " = sp_StrArray_new();")
23199+ elsif block_ret_an == "float"
23200+ @needs_float_array = 1
23201+ emit(" sp_FloatArray *" + arrnew_tmp + " = sp_FloatArray_new();")
23202+ elsif block_ret_an == "poly"
23203+ @needs_rb_value = 1
23204+ emit(" sp_PolyArray *" + arrnew_tmp + " = sp_PolyArray_new();")
23205+ elsif an_ret_is_deep_arr
23206+ @needs_rb_value = 1
23207+ emit(" sp_PolyArray *" + arrnew_tmp + " = sp_PolyArray_new();")
23208+ elsif an_ret_is_arr
23209+ @needs_ptr_array = 1
23210+ emit(" sp_PtrArray *" + arrnew_tmp + " = sp_PtrArray_new();")
23211+ else
23212+ @needs_int_array = 1
23213+ emit(" sp_IntArray *" + arrnew_tmp + " = sp_IntArray_new();")
23214+ end
2310323215 # Root the new array before running the block body — pushing
2310423216 # poly/string values inside the loop can trigger a GC cycle
2310523217 # that would otherwise sweep the local pointer.
@@ -23118,7 +23230,19 @@
2311823230 arrnew_k = arrnew_k + 1
2311923231 end
2312023232 arrnew_lastv = compile_expr(arrnew_stmts2.last)
23121- emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23233+ if block_ret_an == "string"
23234+ emit(" sp_StrArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23235+ elsif block_ret_an == "float"
23236+ emit(" sp_FloatArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23237+ elsif block_ret_an == "poly"
23238+ emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23239+ elsif an_ret_is_deep_arr
23240+ emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + box_value_to_poly(block_ret_an, arrnew_lastv) + ");")
23241+ elsif an_ret_is_arr
23242+ emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")
23243+ else
23244+ emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23245+ end
2312223246 end
2312323247 end
2312423248 @indent = @indent - 1