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
if nid < 0 return cur_t end # Nested DefNode / ClassNode / ModuleNode bodies introduce # new bindings that may shadow `pname`. Don't cross. if @nd_type[nid] == "DefNode" return cur_t end if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode" return cur_t end nt = @nd_type[nid] if nt == "LocalVariableWriteNode" if @nd_name[nid] == pname if nid < 0 return end # Don't cross nested DefNode / ClassNode / ModuleNode bodies — # their locals introduce new bindings that may shadow `pname`. # An inner method's `pname` local would falsely contribute its # called methods to the outer param's observation set # (#450 cascade 1's family of boundary bugs). if @nd_type[nid] == "DefNode" return end if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode" return end if @nd_type[nid] == "CallNode" recv = @nd_receiver[nid] if recv >= 0 if nid < 0 return end # Nested DefNode / ClassNode / ModuleNode bodies introduce # new bindings that may shadow `pname`. Don't cross. if @nd_type[nid] == "DefNode" return end if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode" return end if @nd_type[nid] == "CallNode" if @nd_name[nid] == "push" || @nd_name[nid] == "<<" recv = @nd_receiver[nid] if nid < 0 return end # Nested DefNode / ClassNode / ModuleNode bodies are separate # scopes (see #450 cascade 1 / scan_locals fix). Without these # guards, nested-def locals inside a module body leak into # main's poly-detection bucket and falsely tag main-level # locals as poly. if @nd_type[nid] == "DefNode" return end if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode" return end if @nd_type[nid] == "LocalVariableWriteNode" lname = @nd_name[nid] at = infer_type(@nd_expression[nid]) if nid < 0 return end # Nested DefNode / ClassNode / ModuleNode bodies are separate # scopes — their locals belong to the enclosed unit and must # not leak into the parent. Mirrors the scan_locals boundary # guard (see #450 cascade 1). if @nd_type[nid] == "DefNode" return end if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode" return end if @nd_type[nid] == "LocalVariableWriteNode" lname = @nd_name[nid] if not_in(lname, names) == 1test/scan_walkers_def_boundary.rbaddedView file
Loading diff…
test/scan_walkers_def_boundary.rb.expectedaddedView file
Loading diff…