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

@@ -9908,34 +9908,43 @@
99089908 recv_iow_fwd = @nd_receiver[nid]
99099909 recv_is_ivar_or_local = recv_iow_fwd >= 0 && (@nd_type[recv_iow_fwd] == "InstanceVariableReadNode" || @nd_type[recv_iow_fwd] == "LocalVariableReadNode")
99109910 if rt == "int" && recv_is_ivar_or_local && primitive_method_shared_with_user_class(mname) == 0
9911- matched_ci_fwd = -1
9912- ci_fwd = 0
9913- while ci_fwd < @cls_names.length
9914- if cls_find_method_direct(ci_fwd, mname) >= 0
9915- if matched_ci_fwd >= 0
9916- matched_ci_fwd = -2
9917- break
9918- end
9919- matched_ci_fwd = ci_fwd
9920- end
9921- ci_fwd = ci_fwd + 1
9922- end
9923- if matched_ci_fwd >= 0
9924- midx_fwd = cls_find_method_direct(matched_ci_fwd, mname)
9925- args_id_fwd = @nd_arguments[nid]
9926- if args_id_fwd >= 0 && midx_fwd >= 0
9927- arg_ids_fwd = get_args(args_id_fwd)
9928- ptypes_fwd = cls_meth_ptypes_get(matched_ci_fwd, midx_fwd)
9929- if ptypes_fwd.length > 0
9930- kk_fwd = 0
9931- while kk_fwd < arg_ids_fwd.length
9932- at_fwd = infer_type(arg_ids_fwd[kk_fwd])
9933- if kk_fwd < ptypes_fwd.length
9934- ptypes_fwd[kk_fwd] = unify_call_types(ptypes_fwd[kk_fwd], at_fwd, arg_ids_fwd[kk_fwd])
9911+ # Walk every user class that defines mname AND whose
9912+ # param count matches the call's arg count. Pre-fix
9913+ # this branch bailed on multi-match cases
9914+ # (matched_ci_fwd = -2), leaving every candidate's
9915+ # params at the int default and producing
9916+ # incompatible-pointer C errors when poly-typed
9917+ # iteration over heterogeneous receivers reaches
9918+ # them. Issue #407 case 2: `[IndexHandler.new,
9919+ # UsersHandler.new].each { |h| h.handle(req, res) }`
9920+ # -- both Index/UsersHandler#handle(req,res) get
9921+ # widened from "/" and "" args; sibling classes that
9922+ # happen to define `handle` with a different arity
9923+ # (SQLite's attr_accessor, 0-arg getter) are filtered
9924+ # out by the arity check.
9925+ args_id_fwd_407 = @nd_arguments[nid]
9926+ if args_id_fwd_407 >= 0
9927+ arg_ids_fwd_407 = get_args(args_id_fwd_407)
9928+ arg_count_fwd_407 = arg_ids_fwd_407.length
9929+ if arg_count_fwd_407 > 0
9930+ ci_fwd = 0
9931+ while ci_fwd < @cls_names.length
9932+ midx_fwd_407c = cls_find_method_direct(ci_fwd, mname)
9933+ if midx_fwd_407c >= 0
9934+ ptypes_fwd_407c = cls_meth_ptypes_get(ci_fwd, midx_fwd_407c)
9935+ if ptypes_fwd_407c.length == arg_count_fwd_407
9936+ kk_fwd_407c = 0
9937+ while kk_fwd_407c < arg_count_fwd_407
9938+ at_fwd_407c = infer_type(arg_ids_fwd_407[kk_fwd_407c])
9939+ if kk_fwd_407c < ptypes_fwd_407c.length
9940+ 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])
9941+ end
9942+ kk_fwd_407c = kk_fwd_407c + 1
9943+ end
9944+ cls_meth_ptypes_put(ci_fwd, midx_fwd_407c, ptypes_fwd_407c)
99359945 end
9936- kk_fwd = kk_fwd + 1
99379946 end
9938- cls_meth_ptypes_put(matched_ci_fwd, midx_fwd, ptypes_fwd)
9947+ ci_fwd = ci_fwd + 1
99399948 end
99409949 end
99419950 end