Commit c4275799

c4275799dd46Merged

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

Description

fix(analyze): skip cross-class widening for unresolved-local recv (#429)

`infer_recv_method_type`'s int-recv arm walked every user class with the named method and picked the first non-int return. The intent was to dispatch `arr[i].method()` where arr is IntArray-of-boxed-pointers and the recv reads as int. But the same path fired during scan_locals' first pass for `r = c.get("/foo")` where `c` was statically obj_IntClient: find_var_type("c") returns "" before the scope decls land, infer_type falls back to "int" via the LocalVariableReadNode default, and the cross-class scan picked the first matching class -- StrBag in Ori's repro -- pinning `r` to String even though the actual recv was IntClient.

Fix: when the recv is a LocalVariableReadNode whose var-type isn't pinned yet, AND multiple candidate classes disagree on the return type, bail out of the int-recv widening. A later iteration of the inference loop (with `c`'s declaration in @scope_names) routes through the is_obj_type arm and resolves correctly. Single-candidate / agreeing-candidate cases keep the existing widening intact -- the multi_return_bare test (same-class same-mname with multiple tuple return shapes) is the canonical agreeing case and stays green.

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

Details

Commit
c4275799dd46
Parent
631485cd1a7b

spinel_analyze.rbmodifiedView file

@@ -4815,7 +4815,44 @@
48154815 return poly_dispatch_return_type(mname)
48164816 end
48174817 # Method call on int (possible IntArray element storing object pointers)
4818- if rt == "int"
4818+ # Issue #429: when recv is a LocalVariableReadNode whose
4819+ # var-type table entry hasn't been pinned yet (find_var_type
4820+ # returns ""), `rt` defaults to "int" via infer_type's
4821+ # LocalVariableReadNode fallback. The cross-class widening
4822+ # below would then pick the FIRST user class with a non-int
4823+ # `<mname>` return, silently widening `r = c.get(...)` to the
4824+ # wrong type when `c` is statically obj_<Other> but its type
4825+ # hasn't propagated through the iterative loop yet. Bail out
4826+ # of the int-recv path in that case ONLY when the candidates
4827+ # disagree -- if every class defining mname returns the same
4828+ # type, the widening's result is correct regardless of which
4829+ # one the recv actually points at. A single matching class
4830+ # (the multi_return_bare shape, where every callsite's recv
4831+ # is the same class) keeps the widening intact too.
4832+ recv_is_unresolved_local_429 = 0
4833+ if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode" && find_var_type(@nd_name[recv]) == ""
4834+ unique_rt_429 = ""
4835+ diverged_429 = 0
4836+ ci_429 = 0
4837+ while ci_429 < @cls_names.length
4838+ if cls_find_method_direct(ci_429, mname) >= 0
4839+ mr_429 = cls_method_return(ci_429, mname)
4840+ if mr_429 != "int" && mr_429 != ""
4841+ if unique_rt_429 == ""
4842+ unique_rt_429 = mr_429
4843+ elsif unique_rt_429 != mr_429
4844+ diverged_429 = 1
4845+ ci_429 = @cls_names.length
4846+ end
4847+ end
4848+ end
4849+ ci_429 = ci_429 + 1
4850+ end
4851+ if diverged_429 == 1
4852+ recv_is_unresolved_local_429 = 1
4853+ end
4854+ end
4855+ if rt == "int" && recv_is_unresolved_local_429 == 0
48194856 ci = 0
48204857 while ci < @cls_names.length
48214858 # Check zero-arg methods (getters)