Commit 7f94e2c6
7f94e2c65514Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): subclass override params widen from parent's typed call-site
Closes #516. When a parent-class instance method called another
method via implicit-self (some_method(arg)) and a subclass
direct-overrode that method, the parent's typed arg flowed
into the parent's same-named method (via scan_cls_method_calls'
no-receiver branch) but the subclass override stayed at the int
default. The override's body then emitted dispatch warnings on
the misinferred param.
Fix in scan_cls_method_calls' implicit-self CallNode arm: after widening the same-class method's ptypes from the call arg_ids, walk every other class and -- when it direct-overrides the same method name and is a descendant of the current class -- widen its ptypes from the same arg_ids. The override's C signature now agrees with the cls_id-switch dispatch the imeth emit builds at compile_call_expr.
Tests cover the two-level case from the issue (Base#reload -> consume(row), Sub#consume overrides) and a three-level chain (Parent -> Mid -> Leaf) so the propagation through multiple override layers stays correct.
Local: 488 tests pass, optcarrot checksum 59662.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 7f94e2c65514
- Parent
- 33694cdeee7d
spinel_analyze.rbmodifiedView file
widen_ptypes_from_args(arg_ids, pnames, ptypes) cls_meth_ptypes_put(ci, midx, ptypes) end # Subclass-overridden methods are reached via the same # implicit-self call at runtime (the cls_id-switch # dispatch at compile_call_expr's imeth arm). Widen each # descendant's same-name override from the same call site # so their C signatures agree on the param types. Issue # #516: without this, Sub#consume's `row` stayed at the # int default and `row["id"]` failed to dispatch. ovr_ci = 0 while ovr_ci < @cls_names.length if ovr_ci != ci && cls_is_descendant(ovr_ci, ci) == 1 ovr_midx = cls_find_method_direct(ovr_ci, mname) if ovr_midx >= 0 ovr_ptypes = cls_meth_ptypes_get(ovr_ci, ovr_midx) if ovr_ptypes.length > 0 ovr_pnames = cls_meth_pnames_get(ovr_ci, ovr_midx) widen_ptypes_from_args(arg_ids, ovr_pnames, ovr_ptypes) cls_meth_ptypes_put(ovr_ci, ovr_midx, ovr_ptypes) end end end ovr_ci = ovr_ci + 1 end end end endtest/subclass_override_param_propagate.rbaddedView file
Loading diff…
test/subclass_override_param_propagate.rb.expectedaddedView file
Loading diff…