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
return poly_dispatch_return_type(mname) end # Method call on int (possible IntArray element storing object pointers) if rt == "int" # Issue #429: when recv is a LocalVariableReadNode whose # var-type table entry hasn't been pinned yet (find_var_type # returns ""), `rt` defaults to "int" via infer_type's # LocalVariableReadNode fallback. The cross-class widening # below would then pick the FIRST user class with a non-int # `<mname>` return, silently widening `r = c.get(...)` to the # wrong type when `c` is statically obj_<Other> but its type # hasn't propagated through the iterative loop yet. Bail out # of the int-recv path in that case ONLY when the candidates # disagree -- if every class defining mname returns the same # type, the widening's result is correct regardless of which # one the recv actually points at. A single matching class # (the multi_return_bare shape, where every callsite's recv # is the same class) keeps the widening intact too. recv_is_unresolved_local_429 = 0 if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode" && find_var_type(@nd_name[recv]) == "" unique_rt_429 = "" diverged_429 = 0 ci_429 = 0 while ci_429 < @cls_names.length if cls_find_method_direct(ci_429, mname) >= 0 mr_429 = cls_method_return(ci_429, mname) if mr_429 != "int" && mr_429 != "" if unique_rt_429 == "" unique_rt_429 = mr_429 elsif unique_rt_429 != mr_429 diverged_429 = 1 ci_429 = @cls_names.length end end end ci_429 = ci_429 + 1 end if diverged_429 == 1 recv_is_unresolved_local_429 = 1 end end if rt == "int" && recv_is_unresolved_local_429 == 0 ci = 0 while ci < @cls_names.length # Check zero-arg methods (getters)test/imeth_name_collision_recv_aware.rbaddedView file
Loading diff…
test/imeth_name_collision_recv_aware.rb.expectedaddedView file
Loading diff…