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

@@ -17302,6 +17302,28 @@
1730217302 widen_ptypes_from_args(arg_ids, pnames, ptypes)
1730317303 cls_meth_ptypes_put(ci, midx, ptypes)
1730417304 end
17305+ # Subclass-overridden methods are reached via the same
17306+ # implicit-self call at runtime (the cls_id-switch
17307+ # dispatch at compile_call_expr's imeth arm). Widen each
17308+ # descendant's same-name override from the same call site
17309+ # so their C signatures agree on the param types. Issue
17310+ # #516: without this, Sub#consume's `row` stayed at the
17311+ # int default and `row["id"]` failed to dispatch.
17312+ ovr_ci = 0
17313+ while ovr_ci < @cls_names.length
17314+ if ovr_ci != ci && cls_is_descendant(ovr_ci, ci) == 1
17315+ ovr_midx = cls_find_method_direct(ovr_ci, mname)
17316+ if ovr_midx >= 0
17317+ ovr_ptypes = cls_meth_ptypes_get(ovr_ci, ovr_midx)
17318+ if ovr_ptypes.length > 0
17319+ ovr_pnames = cls_meth_pnames_get(ovr_ci, ovr_midx)
17320+ widen_ptypes_from_args(arg_ids, ovr_pnames, ovr_ptypes)
17321+ cls_meth_ptypes_put(ovr_ci, ovr_midx, ovr_ptypes)
17322+ end
17323+ end
17324+ end
17325+ ovr_ci = ovr_ci + 1
17326+ end
1730517327 end
1730617328 end
1730717329 end