Commit d1264134

d1264134d353Merged

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

Description

fix(analyze): apply DefNode boundary guards to five recursive walkers

Audit follow-up to the scan_locals fix in 3b170a3 (#450 cascade 1). Searched spinel_analyze.rb for AST-walking helpers that recursively descend without resetting scope context. Five siblings of scan_locals showed the same shape — accumulating per-method state into a caller-supplied buffer while walking @nd_body / @nd_stmts / @nd_expression unconditionally:

  • scan_locals_first_type
  • scan_poly_assigns
  • collect_param_methods
  • collect_param_push_elem_types
  • scan_param_body_write_unify

Each crosses into nested DefNode / ClassNode / ModuleNode bodies when called from a top-level context (or any caller passing a body that happens to contain a nested def). The inner unit's locals / param-method observations / push elements then leak into the outer scope's bucket, conflating bindings that share a name and producing the cascade-1-style poly widening downstream.

The recursive walks in spinel had two intentional cross-boundary shapes:

  • scan_writer_calls / scan_ivars / scan_module_*_writes — these correctly track @current_class_idx and the ivar arms are guarded so cross-boundary recursion is a no-op for the state being recorded.
  • walk_and_cache — already has its own DefNode skip. The five walkers above had no such gating.

Add the same return on DefNode / ClassNode / ModuleNode guard at the head of each. scan_locals_first_type and scan_poly_assigns are called from top-level contexts (infer_lambda_param_types and detect_poly_locals respectively); the other three are per-method but a nested DefNode inside a method body would still corrupt their observations.

No regression in the real-blog archive (errors stable at 57 with or without these guards — the bugs are latent against the current repro). Tests 408 pass (+1 regression test pinning the shape: module-nested defs with shadowing param names), bench 52, optcarrot checksum 59662.

Refs #450

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
d1264134d353
Parent
d4a80dd7642c

spinel_analyze.rbmodifiedView file

@@ -11118,6 +11118,14 @@
1111811118 if nid < 0
1111911119 return cur_t
1112011120 end
11121+ # Nested DefNode / ClassNode / ModuleNode bodies introduce
11122+ # new bindings that may shadow `pname`. Don't cross.
11123+ if @nd_type[nid] == "DefNode"
11124+ return cur_t
11125+ end
11126+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
11127+ return cur_t
11128+ end
1112111129 nt = @nd_type[nid]
1112211130 if nt == "LocalVariableWriteNode"
1112311131 if @nd_name[nid] == pname
@@ -11264,6 +11272,17 @@
1126411272 if nid < 0
1126511273 return
1126611274 end
11275+ # Don't cross nested DefNode / ClassNode / ModuleNode bodies —
11276+ # their locals introduce new bindings that may shadow `pname`.
11277+ # An inner method's `pname` local would falsely contribute its
11278+ # called methods to the outer param's observation set
11279+ # (#450 cascade 1's family of boundary bugs).
11280+ if @nd_type[nid] == "DefNode"
11281+ return
11282+ end
11283+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
11284+ return
11285+ end
1126711286 if @nd_type[nid] == "CallNode"
1126811287 recv = @nd_receiver[nid]
1126911288 if recv >= 0
@@ -11358,6 +11377,14 @@
1135811377 if nid < 0
1135911378 return
1136011379 end
11380+ # Nested DefNode / ClassNode / ModuleNode bodies introduce
11381+ # new bindings that may shadow `pname`. Don't cross.
11382+ if @nd_type[nid] == "DefNode"
11383+ return
11384+ end
11385+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
11386+ return
11387+ end
1136111388 if @nd_type[nid] == "CallNode"
1136211389 if @nd_name[nid] == "push" || @nd_name[nid] == "<<"
1136311390 recv = @nd_receiver[nid]
@@ -14938,6 +14965,17 @@
1493814965 if nid < 0
1493914966 return
1494014967 end
14968+ # Nested DefNode / ClassNode / ModuleNode bodies are separate
14969+ # scopes (see #450 cascade 1 / scan_locals fix). Without these
14970+ # guards, nested-def locals inside a module body leak into
14971+ # main's poly-detection bucket and falsely tag main-level
14972+ # locals as poly.
14973+ if @nd_type[nid] == "DefNode"
14974+ return
14975+ end
14976+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
14977+ return
14978+ end
1494114979 if @nd_type[nid] == "LocalVariableWriteNode"
1494214980 lname = @nd_name[nid]
1494314981 at = infer_type(@nd_expression[nid])
@@ -15042,6 +15080,16 @@
1504215080 if nid < 0
1504315081 return
1504415082 end
15083+ # Nested DefNode / ClassNode / ModuleNode bodies are separate
15084+ # scopes — their locals belong to the enclosed unit and must
15085+ # not leak into the parent. Mirrors the scan_locals boundary
15086+ # guard (see #450 cascade 1).
15087+ if @nd_type[nid] == "DefNode"
15088+ return
15089+ end
15090+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
15091+ return
15092+ end
1504515093 if @nd_type[nid] == "LocalVariableWriteNode"
1504615094 lname = @nd_name[nid]
1504715095 if not_in(lname, names) == 1