Commit 413652fd
413652fd6ed6Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(analyze): scan module-body locals before walking seed calls
Cascade 1 of #450 (commit 3b170a3) early-returns from scan_locals on DefNode / ClassNode / ModuleNode so a parent context's local scan does not pull a nested scope's writes into its own merge table. That kept def / class bodies isolated correctly but also stopped infer_main_call_types from seeing locals at module body, which is a load-time scope just like main.
A seed call like Foo::Wrapper.new(_seed) where
_seed = "hello" sits above it in the module body now
saw _seed default to int, and widen_ptypes_from_args
left initialize(s) typed mrb_int lv_s. The pattern is
the canonical type-seeder for module-scoped public APIs
(Ori Pekelman's tep lib/tep.rb ships a long block of
seed calls that pin every public method's param type so
user apps do not have to), and reducing every seed param
to mrb_int cascades through ivar slots and s.split /
a[i] shapes — 181/181 tep tests broke at C compile.
Fix: scan_new_calls' ModuleNode branch now pushes a fresh scope, runs scan_locals on the module body to collect its top-level writes, declares them, then walks the body for calls. The DefNode / ClassNode / ModuleNode guards in scan_locals still keep nested scopes from leaking back up — cascade-1's anti-leak invariant is preserved (test/scan_locals_def_boundary.rb still pins Db.configure's path param as const char *).
Closes #467.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 413652fd6ed6
- Parent
- 337eeefbbbfa
spinel_analyze.rbmodifiedView file
enter_module_scope_from_node(nid) body = @nd_body[nid] if body >= 0 # Module-body top-level statements form a load-time scope just # like main. Seed calls like `Foo::Wrapper.new(_seed)` where # `_seed = "hello"` lives at module body need their argument # types pinned, but the cascade-1 guard in scan_locals stops # `infer_main_call_types` from descending here. Mirror main's # locals-then-calls shape on a fresh scope so the seed call's # arg infers off the local's literal type. The same guard keeps # nested def/class/module bodies from leaking back out. push_scope lnames = "".split(",") ltypes = "".split(",") empty_p = "".split(",") scan_locals(body, lnames, ltypes, empty_p) k = 0 while k < lnames.length declare_var(lnames[k], ltypes[k]) k = k + 1 end scan_new_calls(body) pop_scope end @current_lexical_scope = saved_scope2 return