Commit b6007f86

b6007f86940cMerged

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

Description

Merge pull request #183 from mame/pr/namespace-prefix-types

fix(codegen): preserve namespace prefix in ivar / local types

Details

Commit
b6007f86940c
Parent
5cbc5877096d
Merge parent
d58826368fef

spinel_codegen.rbmodifiedView file

@@ -5039,7 +5039,12 @@
50395039 end
50405040 end
50415041 }
5042+ # Pin lexical scope while collecting ivars. See longer comment
5043+ # at the other collect_ivars call site below for rationale.
5044+ saved_idx = @current_class_idx
5045+ @current_class_idx = ci
50425046 collect_ivars(ci)
5047+ @current_class_idx = saved_idx
50435048 return
50445049 end
50455050
@@ -5214,8 +5219,17 @@
52145219 end
52155220 }
52165221
5217- # Collect ivars
5222+ # Collect ivars. Pin the lexical scope to this class so any
5223+ # `@x = Foo.new(...)` inside its methods resolves `Foo` against
5224+ # the same scope chain the call site sees — without this,
5225+ # current_lexical_scope_name returns "" and a `Config` reference
5226+ # inside `Optcarrot::NES` resolves to bare "Config" (no
5227+ # Optcarrot_ prefix), poisoning the ivar's recorded type and the
5228+ # eventual `sp_Config *` field declaration that fails to compile.
5229+ saved_idx = @current_class_idx
5230+ @current_class_idx = ci
52185231 collect_ivars(ci)
5232+ @current_class_idx = saved_idx
52195233 end
52205234
52215235 def collect_module_methods_into_class(ci, mod_name)
@@ -7616,6 +7630,14 @@
76167630 i = 0
76177631 while i < @meth_names.length
76187632 push_scope
7633+ # Pin @current_method_name so current_lexical_scope_name can pull
7634+ # the module prefix out of `<Mod>_cls_<m>` style names. Without
7635+ # this, a `Foo.new` inside e.g. `Optcarrot::Driver.load` resolves
7636+ # `Foo` against the empty scope and lands on bare `Foo` instead
7637+ # of `Optcarrot_Foo`, which then poisons the local variable's
7638+ # recorded type.
7639+ saved_meth = @current_method_name
7640+ @current_method_name = @meth_names[i]
76197641 pnames = @meth_param_names[i].split(",")
76207642 ptypes = @meth_param_types[i].split(",")
76217643 j = 0
@@ -7639,6 +7661,7 @@
76397661 end
76407662 scan_writer_calls(@meth_body_ids[i])
76417663 end
7664+ @current_method_name = saved_meth
76427665 pop_scope
76437666 i = i + 1
76447667 end
@@ -14080,7 +14103,17 @@
1408014103 if @nd_type[sid] != "DefNode"
1408114104 if @nd_type[sid] != "ClassNode"
1408214105 if @nd_type[sid] != "ConstantWriteNode"
14083- scan_locals(sid, lnames, ltypes, empty_params)
14106+ # Skip ModuleNode bodies — their nested classes/methods are
14107+ # collected separately, and recursing into them here would
14108+ # pull every nested method's locals into main()'s frame and
14109+ # also mis-resolve constants (no lexical scope is in effect
14110+ # at this scan, so e.g. `Video.new(...)` inside a module
14111+ # function lands on bare `Video` instead of the namespaced
14112+ # form). The other passes below already filter ModuleNode;
14113+ # this one was the outlier.
14114+ if @nd_type[sid] != "ModuleNode"
14115+ scan_locals(sid, lnames, ltypes, empty_params)
14116+ end
1408414117 end
1408514118 end
1408614119 end