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
@implicit_new_site_classes = "".split(",", -1) @implicit_new_local_names = "".split(",", -1) @implicit_new_local_classes = "".split(",", -1) # Enclosing DefNode id for each implicit-new local binding, so the # name-keyed lookup can stay method-scoped. A local's type never # comes from another method's local, so resolving `conn` in one # method to an implicit-new class registered for a same-named # `conn` in a different method is always wrong. @implicit_new_local_defs = [] # Current method being walked by scan_new_calls (-1 = main / module # body / outside any def). Used to method-scope implicit_new_local_class. @snc_cur_def = -1 @implicit_new_signature_keys = "".split(",", -1) @implicit_new_signature_classes = "".split(",", -1) @implicit_new_site_arg_type_ids = [] result = "" k = 0 while k < @implicit_new_local_names.length if @implicit_new_local_names[k] == name # Method-scope the match: when scan_new_calls has pinned the # current def (@snc_cur_def >= 0), only consider bindings from # that same method. Outside a def context (-1) fall back to the # name-only match (load-time / other passes that already carry # proper local scope via @scope_*). if @implicit_new_local_names[k] == name && (@snc_cur_def < 0 || @implicit_new_local_defs[k] == @snc_cur_def) cls = @implicit_new_local_classes[k] if result == "" result = cls @cls_cmeth_params_version = @cls_cmeth_params_version + 1 end def collect_implicit_new_local_bindings(nid) def collect_implicit_new_local_bindings(nid, cur_def = -1) if nid < 0 return end if @nd_type[nid] == "DefNode" cur_def = nid end if @nd_type[nid] == "LocalVariableWriteNode" expr = @nd_expression[nid] cls = implicit_new_site_class(expr) if cls != "" @implicit_new_local_names.push(@nd_name[nid]) @implicit_new_local_classes.push(cls) @implicit_new_local_defs.push(cur_def) end end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length collect_implicit_new_local_bindings(cs[k]) collect_implicit_new_local_bindings(cs[k], cur_def) k = k + 1 end end @current_lexical_scope = saved_scope2 return end # Pin the current method while walking its body so the name-keyed # implicit_new_local_class lookup stays method-scoped: a local # `conn` here must not resolve to a same-named `conn` bound to an # implicit-new class in a sibling method (which would mis-widen a # callee param against the wrong class -- e.g. a Fiber-typed arg # read as a WebSocket::Connection). if @nd_type[nid] == "DefNode" snc_saved_def = @snc_cur_def @snc_cur_def = nid def_body = @nd_body[nid] if def_body >= 0 scan_new_calls(def_body) end @snc_cur_def = snc_saved_def return end # IfNode (incl. ternary). When the predicate is # `var.is_a?(C)` / `kind_of?(C)`, push a narrow on `var` while # walking the then-arm so a recursive call inside the arm seestest/implicit_new_local_method_scope.rbaddedView file
Loading diff…
test/implicit_new_local_method_scope.rb.expectedaddedView file
Loading diff…