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

@@ -9907,7 +9907,26 @@
99079907 enter_module_scope_from_node(nid)
99089908 body = @nd_body[nid]
99099909 if body >= 0
9910+ # Module-body top-level statements form a load-time scope just
9911+ # like main. Seed calls like `Foo::Wrapper.new(_seed)` where
9912+ # `_seed = "hello"` lives at module body need their argument
9913+ # types pinned, but the cascade-1 guard in scan_locals stops
9914+ # `infer_main_call_types` from descending here. Mirror main's
9915+ # locals-then-calls shape on a fresh scope so the seed call's
9916+ # arg infers off the local's literal type. The same guard keeps
9917+ # nested def/class/module bodies from leaking back out.
9918+ push_scope
9919+ lnames = "".split(",")
9920+ ltypes = "".split(",")
9921+ empty_p = "".split(",")
9922+ scan_locals(body, lnames, ltypes, empty_p)
9923+ k = 0
9924+ while k < lnames.length
9925+ declare_var(lnames[k], ltypes[k])
9926+ k = k + 1
9927+ end
99109928 scan_new_calls(body)
9929+ pop_scope
99119930 end
99129931 @current_lexical_scope = saved_scope2
99139932 return