Commit d4a80dd7

d4a80dd7642cMerged

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

Description

fix(analyze): scan_locals stops at DefNode / ClassNode / ModuleNode

#450 cascade 1. scan_locals had no boundary guard for nested DefNode / ClassNode / ModuleNode bodies: when called from infer_main_call_types (the top-level scan) it walked into every nested method body and recorded local = ... writes as if they were siblings of main's own locals. Two methods with the same local name then collided via scan_locals' repeated-write merge and widened the conflated slot to poly. The poly local poisoned every call-site arg-type read of that name, and the bare-recv / module-dispatch widening arms in scan_new_calls propagated poly into the callee's param types — the chain that left SqliteAdapter.configure(path) typed sp_RbVal lv_database_path in the archive even though every call passed a concrete const char *.

Repro shape boiled down:

module CgiIo def self.parse_request(env, stdin) path = env["PATH_INFO"] || "/" # path local : poly (string?|string) { path: path } end end

module Db def self.configure(path); ...; end # widens to sp_RbVal lv_path end

module Main def self.run_setup path = "/tmp/db" # path local : string Db.configure(path) # arg widened to poly via leakage end end

scan_locals from infer_main_call_types walked into CgiIo and Main both, recorded both writes against a SINGLE main-level path slot (string + poly → poly per repeated-write merge), and that poly then flowed into Db.configure's param via the module-dispatch widening arm in scan_new_calls.

Fix: early-return from scan_locals when nid is a DefNode / ClassNode / ModuleNode. Each of these introduces a fresh local scope; their locals must be collected by separate scan_locals calls scoped to the right outer context (which the infer_function_body_call_types / infer_ivar_types_from_writers / infer_class_body_call_types passes already do correctly).

Validation:

  • test 407 pass (+1 regression covering scan_locals stopping at module-internal def bodies, with the canonical Db.configure shape).
  • bench 52, optcarrot checksum 59662.
  • real-blog archive errors: 56 → 56 (cascade-1 sites are gone but several unrelated errors remain; total error count was already reduced from 61 to 58 by cascades 2 + 3, and cascade 1 closes two more lines for 56 total).
  • bootstrap 4-way fixpoint OK.

Closes #450 cascade 1 of 3 (all three cascades now resolved).

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

Details

Commit
d4a80dd7642c
Parent
c11200dd5775

spinel_analyze.rbmodifiedView file

@@ -17736,6 +17736,24 @@
1773617736 if nid < 0
1773717737 return
1773817738 end
17739+ # Nested DefNode / ClassNode / ModuleNode bodies are separate
17740+ # scopes — their locals belong to the enclosed method / class /
17741+ # module and must not leak into the parent scope. Without these
17742+ # guards, scan_locals called from infer_main_call_types (or any
17743+ # outer-context caller) walks into every nested method body and
17744+ # records each `local = ...` write as if it were a sibling of
17745+ # main's own locals. Two functions with the same local name
17746+ # but different inferred types then widen to "poly" via the
17747+ # repeated-write merge below, propagating poly into call-site
17748+ # arg types and (via the bare-recv / module-dispatch widening
17749+ # arms in scan_new_calls) into the callee's param types
17750+ # — the topology behind #450 cascade 1.
17751+ if @nd_type[nid] == "DefNode"
17752+ return
17753+ end
17754+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
17755+ return
17756+ end
1773917757 # Parallel to `names`: "1" if this local's current stored type was set
1774017758 # by an explicit literal write, "" otherwise. Reset when called with
1774117759 # a fresh (empty) names array.