Commit a4ccdc1d
a4ccdc1de7e8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): multi-class arity-aware forward-ref widening (#407 case 2)
scan_new_calls' forward-ref widening at line 9910 bailed when
more than one user class defined the same method name
(matched_ci_fwd = -2 = ambiguous), leaving every candidate's
params at the int default. The poly-recv widening at line 9953
fired only when the recv's static type already resolved to
"poly" -- the heterogeneous-array iteration shape
(handlers = [IndexHandler.new, UsersHandler.new]; handlers[i] .handle(req, res)) hits this with h typed int during the
inference loop because the var-type table isn't yet populated
(per matz's earlier comment on this issue).
Fix: when rt is int / unresolved and the recv is a LocalVar
/ IVar read, walk every user class that defines mname AND
whose ptypes count exactly matches the call's arg count. The
arity filter excludes attr_accessor-only entries (0-param
getters) that happen to share the name with a multi-arg def
-- the canonical case 2 shape, where SQLite's
attr_accessor :handle collides with Handler subclass'
def handle(req, res).
Trade-off vs the previous single-match guard:
- Old: refused to widen when more than one class matched. Conservative -- never wrong on the widening, but left heterogeneous-array shapes unfixable.
- New: widens every arity-matching class. unify_call_types still collapses to poly when a later disagreeing site rolls in, so the safety net of the iterative loop's convergence remains intact.
Bootstrap fixpoint OK; #207's existing repro stays green
(the arity filter excludes its attr_accessor :foo getters
from cross-arity unification). Optcarrot checksum 59662
unchanged, fps 658.9 (within noise). 382 tests pass.
Case 3 (attr_accessor :methods, :headers collisions
without a multi-arg def on a sibling class) is also covered
by the same arity filter: getters with 0 params don't
unify against multi-arg call sites, so Response#headers'
str_str_hash return type stays pinned.
Closes #407 Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- a4ccdc1de7e8
- Parent
- aca207aebb49
spinel_analyze.rbmodifiedView file
recv_iow_fwd = @nd_receiver[nid] recv_is_ivar_or_local = recv_iow_fwd >= 0 && (@nd_type[recv_iow_fwd] == "InstanceVariableReadNode" || @nd_type[recv_iow_fwd] == "LocalVariableReadNode") if rt == "int" && recv_is_ivar_or_local && primitive_method_shared_with_user_class(mname) == 0 matched_ci_fwd = -1 ci_fwd = 0 while ci_fwd < @cls_names.length if cls_find_method_direct(ci_fwd, mname) >= 0 if matched_ci_fwd >= 0 matched_ci_fwd = -2 break end matched_ci_fwd = ci_fwd end ci_fwd = ci_fwd + 1 end if matched_ci_fwd >= 0 midx_fwd = cls_find_method_direct(matched_ci_fwd, mname) args_id_fwd = @nd_arguments[nid] if args_id_fwd >= 0 && midx_fwd >= 0 arg_ids_fwd = get_args(args_id_fwd) ptypes_fwd = cls_meth_ptypes_get(matched_ci_fwd, midx_fwd) if ptypes_fwd.length > 0 kk_fwd = 0 while kk_fwd < arg_ids_fwd.length at_fwd = infer_type(arg_ids_fwd[kk_fwd]) if kk_fwd < ptypes_fwd.length ptypes_fwd[kk_fwd] = unify_call_types(ptypes_fwd[kk_fwd], at_fwd, arg_ids_fwd[kk_fwd]) # Walk every user class that defines mname AND whose # param count matches the call's arg count. Pre-fix # this branch bailed on multi-match cases # (matched_ci_fwd = -2), leaving every candidate's # params at the int default and producing # incompatible-pointer C errors when poly-typed # iteration over heterogeneous receivers reaches # them. Issue #407 case 2: `[IndexHandler.new, # UsersHandler.new].each { |h| h.handle(req, res) }` # -- both Index/UsersHandler#handle(req,res) get # widened from "/" and "" args; sibling classes that # happen to define `handle` with a different arity # (SQLite's attr_accessor, 0-arg getter) are filtered # out by the arity check. args_id_fwd_407 = @nd_arguments[nid] if args_id_fwd_407 >= 0 arg_ids_fwd_407 = get_args(args_id_fwd_407) arg_count_fwd_407 = arg_ids_fwd_407.length if arg_count_fwd_407 > 0 ci_fwd = 0 while ci_fwd < @cls_names.length midx_fwd_407c = cls_find_method_direct(ci_fwd, mname) if midx_fwd_407c >= 0 ptypes_fwd_407c = cls_meth_ptypes_get(ci_fwd, midx_fwd_407c) if ptypes_fwd_407c.length == arg_count_fwd_407 kk_fwd_407c = 0 while kk_fwd_407c < arg_count_fwd_407 at_fwd_407c = infer_type(arg_ids_fwd_407[kk_fwd_407c]) if kk_fwd_407c < ptypes_fwd_407c.length ptypes_fwd_407c[kk_fwd_407c] = unify_call_types(ptypes_fwd_407c[kk_fwd_407c], at_fwd_407c, arg_ids_fwd_407[kk_fwd_407c]) end kk_fwd_407c = kk_fwd_407c + 1 end cls_meth_ptypes_put(ci_fwd, midx_fwd_407c, ptypes_fwd_407c) end kk_fwd = kk_fwd + 1 end cls_meth_ptypes_put(matched_ci_fwd, midx_fwd, ptypes_fwd) ci_fwd = ci_fwd + 1 end end endtest/method_name_collision_multi_class.rbaddedView file
Loading diff…
test/method_name_collision_multi_class.rb.expectedaddedView file
Loading diff…