Commit d5882636
d58826368fefMerged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): preserve namespace prefix in ivar / local types
When a class lives inside a module and another class in the same module is referenced via a bare name from one of its method bodies, the type spinel records for the resulting ivar / local must carry the same namespace prefix the class is registered under. Three sites broke this:
-
collect_class_with_prefix(and its class-reopen path) did not set@current_class_idxbefore callingcollect_ivars. Without that,current_lexical_scope_namereturned "" and any@x = Foo.new(...)inside a method body resolvedFooto bare "Foo" — even when the actual class was registered underOptcarrot_Foo. The recorded ivar type was thenobj_Fooand c_type emitted asp_Foo *field that did not match thesp_Optcarrot_Footypedef. -
The top-level method body scan in
compile(line ~5975) did not pin@current_method_name. The existing_cls_parsing incurrent_lexical_scope_namealready extracts a module prefix from<Mod>_cls_<m>names, but only when the method name is set. Pinning it letsFoo.newinside e.g.Optcarrot::Driver.loadresolve against the same scope chain a class body sees. -
emit_main's first locals-scan pass was the only one of three passes missing theModuleNodefilter. It dragged every nested method's locals into main()'s frame and mis-resolved their constants (no lexical scope is in effect at the top-level scan).
Together these account for ~70 of the C-compile errors when
generating optcarrot — every sp_Config * / sp_CPU * /
sp_Triangle * / etc. field in the structs of Optcarrot::Pads,
Optcarrot::APU, Optcarrot::PPU, etc. and every local in
inlined module-function bodies.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- d58826368fef
- Parent
- a09a6b8b339b
spinel_codegen.rbmodifiedView file
end end } # Pin lexical scope while collecting ivars. See longer comment # at the other collect_ivars call site below for rationale. saved_idx = @current_class_idx @current_class_idx = ci collect_ivars(ci) @current_class_idx = saved_idx return end end } # Collect ivars # Collect ivars. Pin the lexical scope to this class so any # `@x = Foo.new(...)` inside its methods resolves `Foo` against # the same scope chain the call site sees — without this, # current_lexical_scope_name returns "" and a `Config` reference # inside `Optcarrot::NES` resolves to bare "Config" (no # Optcarrot_ prefix), poisoning the ivar's recorded type and the # eventual `sp_Config *` field declaration that fails to compile. saved_idx = @current_class_idx @current_class_idx = ci collect_ivars(ci) @current_class_idx = saved_idx end def collect_module_methods_into_class(ci, mod_name) i = 0 while i < @meth_names.length push_scope # Pin @current_method_name so current_lexical_scope_name can pull # the module prefix out of `<Mod>_cls_<m>` style names. Without # this, a `Foo.new` inside e.g. `Optcarrot::Driver.load` resolves # `Foo` against the empty scope and lands on bare `Foo` instead # of `Optcarrot_Foo`, which then poisons the local variable's # recorded type. saved_meth = @current_method_name @current_method_name = @meth_names[i] pnames = @meth_param_names[i].split(",") ptypes = @meth_param_types[i].split(",") j = 0 end scan_writer_calls(@meth_body_ids[i]) end @current_method_name = saved_meth pop_scope i = i + 1 end if @nd_type[sid] != "DefNode" if @nd_type[sid] != "ClassNode" if @nd_type[sid] != "ConstantWriteNode" scan_locals(sid, lnames, ltypes, empty_params) # Skip ModuleNode bodies — their nested classes/methods are # collected separately, and recursing into them here would # pull every nested method's locals into main()'s frame and # also mis-resolve constants (no lexical scope is in effect # at this scan, so e.g. `Video.new(...)` inside a module # function lands on bare `Video` instead of the namespaced # form). The other passes below already filter ModuleNode; # this one was the outlier. if @nd_type[sid] != "ModuleNode" scan_locals(sid, lnames, ltypes, empty_params) end end end endtest/nested_class_module_const_resolution.rbaddedView file
Loading diff…