Commit 2557203a

2557203aec85Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Method-scope the implicit-new local class lookup

A class method's Fiber-typed parameter degraded to poly, then was passed unconverted into a Fiber-typed constructor, breaking the generated C (OriPekelman/tep: Tep::Scheduler.spawn_fiber).

Root cause: implicit_new_local_class(name) matched implicit-new local bindings by variable name across the whole program, with no method scope. scan_new_calls walks method bodies from the program root without setting up per-method local scope, so find_var_type returns "" there and infer_type falls back to this name-keyed lookup. When two sibling methods both bind a local conn -- one a Fiber.new, the other a WebSocket::Connection.new (an implicit-new specialization) -- the Fiber conn resolved to the WebSocket class. That mis-widened spawn_fiber's f param (unify(fiber, obj_WebSocket) -> poly) while the forwarded FiberSlot.new(f) stayed Fiber*, so the call passed a sp_RbVal into a sp_Fiber* slot.

A local's type can never come from a same-named local in another method, so the name-global match is unsound. Fix: record each implicit-new local binding's enclosing DefNode, pin the current method while scan_new_calls descends a body, and require the lookup to match the current method (falling back to name-only outside any def context -- load-time and the already-scoped per-method scans, which resolve locals via @scope_* and never reach this fallback). The properly-scoped bid-callers are unaffected.

Not GC-related and not the #1303 cross-key-family-hash family: MRI and self-host produce identical output, so it is a deterministic inference bug, reproducible under the MRI harness.

Closes #1340

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
2557203aec85
Parent
80910267e8c4

spinel_analyze.rbmodifiedView file

@@ -330,6 +330,15 @@
330330 @implicit_new_site_classes = "".split(",", -1)
331331 @implicit_new_local_names = "".split(",", -1)
332332 @implicit_new_local_classes = "".split(",", -1)
333+ # Enclosing DefNode id for each implicit-new local binding, so the
334+ # name-keyed lookup can stay method-scoped. A local's type never
335+ # comes from another method's local, so resolving `conn` in one
336+ # method to an implicit-new class registered for a same-named
337+ # `conn` in a different method is always wrong.
338+ @implicit_new_local_defs = []
339+ # Current method being walked by scan_new_calls (-1 = main / module
340+ # body / outside any def). Used to method-scope implicit_new_local_class.
341+ @snc_cur_def = -1
333342 @implicit_new_signature_keys = "".split(",", -1)
334343 @implicit_new_signature_classes = "".split(",", -1)
335344 @implicit_new_site_arg_type_ids = []
@@ -810,7 +819,12 @@
810819 result = ""
811820 k = 0
812821 while k < @implicit_new_local_names.length
813- if @implicit_new_local_names[k] == name
822+ # Method-scope the match: when scan_new_calls has pinned the
823+ # current def (@snc_cur_def >= 0), only consider bindings from
824+ # that same method. Outside a def context (-1) fall back to the
825+ # name-only match (load-time / other passes that already carry
826+ # proper local scope via @scope_*).
827+ if @implicit_new_local_names[k] == name && (@snc_cur_def < 0 || @implicit_new_local_defs[k] == @snc_cur_def)
814828 cls = @implicit_new_local_classes[k]
815829 if result == ""
816830 result = cls
@@ -10601,23 +10615,27 @@
1060110615 @cls_cmeth_params_version = @cls_cmeth_params_version + 1
1060210616 end
1060310617
10604- def collect_implicit_new_local_bindings(nid)
10618+ def collect_implicit_new_local_bindings(nid, cur_def = -1)
1060510619 if nid < 0
1060610620 return
1060710621 end
10622+ if @nd_type[nid] == "DefNode"
10623+ cur_def = nid
10624+ end
1060810625 if @nd_type[nid] == "LocalVariableWriteNode"
1060910626 expr = @nd_expression[nid]
1061010627 cls = implicit_new_site_class(expr)
1061110628 if cls != ""
1061210629 @implicit_new_local_names.push(@nd_name[nid])
1061310630 @implicit_new_local_classes.push(cls)
10631+ @implicit_new_local_defs.push(cur_def)
1061410632 end
1061510633 end
1061610634 cs = []
1061710635 push_child_ids(nid, cs)
1061810636 k = 0
1061910637 while k < cs.length
10620- collect_implicit_new_local_bindings(cs[k])
10638+ collect_implicit_new_local_bindings(cs[k], cur_def)
1062110639 k = k + 1
1062210640 end
1062310641 end
@@ -16548,6 +16566,22 @@
1654816566 @current_lexical_scope = saved_scope2
1654916567 return
1655016568 end
16569+ # Pin the current method while walking its body so the name-keyed
16570+ # implicit_new_local_class lookup stays method-scoped: a local
16571+ # `conn` here must not resolve to a same-named `conn` bound to an
16572+ # implicit-new class in a sibling method (which would mis-widen a
16573+ # callee param against the wrong class -- e.g. a Fiber-typed arg
16574+ # read as a WebSocket::Connection).
16575+ if @nd_type[nid] == "DefNode"
16576+ snc_saved_def = @snc_cur_def
16577+ @snc_cur_def = nid
16578+ def_body = @nd_body[nid]
16579+ if def_body >= 0
16580+ scan_new_calls(def_body)
16581+ end
16582+ @snc_cur_def = snc_saved_def
16583+ return
16584+ end
1655116585 # IfNode (incl. ternary). When the predicate is
1655216586 # `var.is_a?(C)` / `kind_of?(C)`, push a narrow on `var` while
1655316587 # walking the then-arm so a recursive call inside the arm sees