Commit 4044da9a
4044da9afd67Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): empty-array local upgrades through pass-2 push observation (#412)
An empty [] followed by a << push in a loop, with the pushed
value held in an intermediate variable, emitted as sp_IntArray *
even though the pushed values were class pointers:
def make results = [] while i < 3 instance = Foo.new results << instance # was sp_IntArray_push(lv_results, lv_instance) i += 1 end results end
Two coordinated changes:
-
refine_method_body_locals's pass-1<->pass-2 merge now handles theint_array -> <typed>_arrayupgrade. scan_locals's<<push observation already upgrades the local in pass 2 (when the intermediate variable's type is in scope andinfer_typeresolves it correctly), but the existing merge logic only propagatedint -> concreteandnil -> nullable_pointerback to pass 1. The new branch covers str_array, float_array, sym_array, *_ptr_array, and poly_array. -
infer_all_returns's top-level-method branch now routes throughrefine_method_body_localsinstead of running its own single-pass scan_locals. Without this the function's return type was pinned atint_arrayindependently of the scope-decl pass that eventually computed the right local type. Same shape as the cmeth-branch fix in #411.
The instance-method branch in infer_all_returns already had
a richer 2-pass refinement; leaving it as-is to keep the
change surgical. A future pass could route all three branches
through refine_method_body_locals for consistency.
Test empty_array_loop_intermediate_push.rb covers (a) the
canonical issue shape with class-pointer pushes (the repro),
(b) a string-typed-array variant to verify the merge branch
covers int_array -> str_array too, and (c) a baseline
direct-push variant as a regression check that the previously
working path still works.
Closes #412
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 4044da9afd67
- Parent
- 7ec2e96f5700
spinel_analyze.rbmodifiedView file
declare_var(pnames[j], pt) j = j + 1 end # Also declare locals for better return type inference # Also declare locals for better return type inference. # Route through refine_method_body_locals so the same # multi-pass refinement precompute_all_scope_decls runs at # end-of-analyze applies here too — without it #412's # int_array -> ptr_array upgrade (and #411's nil -> # nullable_pointer promotion) only land at scope-decl emit # time, after `infer_body_return` has already pinned the # function's return type at the unrefined value. if @meth_body_ids[i] >= 0 lnames = "".split(",") ltypes = "".split(",") scan_locals(@meth_body_ids[i], lnames, ltypes, pnames) lk = 0 while lk < lnames.length declare_var(lnames[lk], ltypes[lk]) lk = lk + 1 end # Second pass: upgrade nil/int types with better information lnames2 = "".split(",") ltypes2 = "".split(",") scan_locals(@meth_body_ids[i], lnames2, ltypes2, pnames) lk = 0 while lk < lnames2.length mk = 0 while mk < lnames.length if lnames[mk] == lnames2[lk] if ltypes[mk] == "int" || ltypes[mk] == "nil" if ltypes2[lk] != "int" && ltypes2[lk] != "nil" ltypes[mk] = ltypes2[lk] set_var_type(lnames[mk], ltypes2[lk]) end elsif (ltypes2[lk] == "str_poly_hash" || ltypes2[lk] == "sym_poly_hash") && ltypes[mk] != ltypes2[lk] # The 2nd pass discovered the iterated value is # poly (each-rebuild over a poly_hash whose source # type wasn't known on the 1st pass). Upgrade. ltypes[mk] = ltypes2[lk] set_var_type(lnames[mk], ltypes2[lk]) end end mk = mk + 1 end lk = lk + 1 end refine_method_body_locals(@meth_body_ids[i], lnames, ltypes, pnames) end rt = infer_body_return(@meth_body_ids[i]) @meth_return_types[i] = rt elsif (ltypes2[j] == "str_poly_hash" || ltypes2[j] == "sym_poly_hash") && ltypes[k] != ltypes2[j] ltypes[k] = ltypes2[j] set_var_type(lnames[k], ltypes2[j]) # Issue #412: pass 1 typed `results` as int_array (from # the empty `[]` literal). Pass 2's `<<` push observation, # with the rhs local now in scope, refined the type to # str_array / float_array / sym_array / *_ptr_array. Without # this branch the refined type is dropped and codegen emits # `sp_IntArray *` against a body that pushes typed pointers, # tripping the cc step at the push site. elsif ltypes[k] == "int_array" && ltypes2[j] != "int_array" && (ltypes2[j] == "str_array" || ltypes2[j] == "float_array" || ltypes2[j] == "sym_array" || is_ptr_array_type(ltypes2[j]) == 1 || ltypes2[j] == "poly_array") ltypes[k] = ltypes2[j] set_var_type(lnames[k], ltypes2[j]) end end k = k + 1test/empty_array_loop_intermediate_push.rbaddedView file
Loading diff…
test/empty_array_loop_intermediate_push.rb.expectedaddedView file
Loading diff…