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 the int_array -> <typed>_array upgrade. scan_locals's << push observation already upgrades the local in pass 2 (when the intermediate variable's type is in scope and infer_type resolves it correctly), but the existing merge logic only propagated int -> concrete and nil -> nullable_pointer back 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 through refine_method_body_locals instead of running its own single-pass scan_locals. Without this the function's return type was pinned at int_array independently 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

@@ -12410,42 +12410,18 @@
1241012410 declare_var(pnames[j], pt)
1241112411 j = j + 1
1241212412 end
12413- # Also declare locals for better return type inference
12413+ # Also declare locals for better return type inference.
12414+ # Route through refine_method_body_locals so the same
12415+ # multi-pass refinement precompute_all_scope_decls runs at
12416+ # end-of-analyze applies here too — without it #412's
12417+ # int_array -> ptr_array upgrade (and #411's nil ->
12418+ # nullable_pointer promotion) only land at scope-decl emit
12419+ # time, after `infer_body_return` has already pinned the
12420+ # function's return type at the unrefined value.
1241412421 if @meth_body_ids[i] >= 0
1241512422 lnames = "".split(",")
1241612423 ltypes = "".split(",")
12417- scan_locals(@meth_body_ids[i], lnames, ltypes, pnames)
12418- lk = 0
12419- while lk < lnames.length
12420- declare_var(lnames[lk], ltypes[lk])
12421- lk = lk + 1
12422- end
12423- # Second pass: upgrade nil/int types with better information
12424- lnames2 = "".split(",")
12425- ltypes2 = "".split(",")
12426- scan_locals(@meth_body_ids[i], lnames2, ltypes2, pnames)
12427- lk = 0
12428- while lk < lnames2.length
12429- mk = 0
12430- while mk < lnames.length
12431- if lnames[mk] == lnames2[lk]
12432- if ltypes[mk] == "int" || ltypes[mk] == "nil"
12433- if ltypes2[lk] != "int" && ltypes2[lk] != "nil"
12434- ltypes[mk] = ltypes2[lk]
12435- set_var_type(lnames[mk], ltypes2[lk])
12436- end
12437- elsif (ltypes2[lk] == "str_poly_hash" || ltypes2[lk] == "sym_poly_hash") && ltypes[mk] != ltypes2[lk]
12438- # The 2nd pass discovered the iterated value is
12439- # poly (each-rebuild over a poly_hash whose source
12440- # type wasn't known on the 1st pass). Upgrade.
12441- ltypes[mk] = ltypes2[lk]
12442- set_var_type(lnames[mk], ltypes2[lk])
12443- end
12444- end
12445- mk = mk + 1
12446- end
12447- lk = lk + 1
12448- end
12424+ refine_method_body_locals(@meth_body_ids[i], lnames, ltypes, pnames)
1244912425 end
1245012426 rt = infer_body_return(@meth_body_ids[i])
1245112427 @meth_return_types[i] = rt
@@ -19794,6 +19770,19 @@
1979419770 elsif (ltypes2[j] == "str_poly_hash" || ltypes2[j] == "sym_poly_hash") && ltypes[k] != ltypes2[j]
1979519771 ltypes[k] = ltypes2[j]
1979619772 set_var_type(lnames[k], ltypes2[j])
19773+ # Issue #412: pass 1 typed `results` as int_array (from
19774+ # the empty `[]` literal). Pass 2's `<<` push observation,
19775+ # with the rhs local now in scope, refined the type to
19776+ # str_array / float_array / sym_array / *_ptr_array. Without
19777+ # this branch the refined type is dropped and codegen emits
19778+ # `sp_IntArray *` against a body that pushes typed pointers,
19779+ # tripping the cc step at the push site.
19780+ elsif ltypes[k] == "int_array" && ltypes2[j] != "int_array" &&
19781+ (ltypes2[j] == "str_array" || ltypes2[j] == "float_array" ||
19782+ ltypes2[j] == "sym_array" || is_ptr_array_type(ltypes2[j]) == 1 ||
19783+ ltypes2[j] == "poly_array")
19784+ ltypes[k] = ltypes2[j]
19785+ set_var_type(lnames[k], ltypes2[j])
1979719786 end
1979819787 end
1979919788 k = k + 1