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
end50405040
end50415041
}5042+
# Pin lexical scope while collecting ivars. See longer comment5043+
# at the other collect_ivars call site below for rationale.5044+
saved_idx = @current_class_idx5045+
@current_class_idx = ci50425046
collect_ivars(ci)5047+
@current_class_idx = saved_idx50435048
return50445049
end50455050
@@ -5214,8 +5219,17 @@
52145219
end52155220
}52165221
5217-
# Collect ivars5222+
# Collect ivars. Pin the lexical scope to this class so any5223+
# `@x = Foo.new(...)` inside its methods resolves `Foo` against5224+
# the same scope chain the call site sees — without this,5225+
# current_lexical_scope_name returns "" and a `Config` reference5226+
# inside `Optcarrot::NES` resolves to bare "Config" (no5227+
# Optcarrot_ prefix), poisoning the ivar's recorded type and the5228+
# eventual `sp_Config *` field declaration that fails to compile.5229+
saved_idx = @current_class_idx5230+
@current_class_idx = ci52185231
collect_ivars(ci)5232+
@current_class_idx = saved_idx52195233
end52205234
52215235
def collect_module_methods_into_class(ci, mod_name)@@ -7616,6 +7630,14 @@
76167630
i = 076177631
while i < @meth_names.length76187632
push_scope7633+
# Pin @current_method_name so current_lexical_scope_name can pull7634+
# the module prefix out of `<Mod>_cls_<m>` style names. Without7635+
# this, a `Foo.new` inside e.g. `Optcarrot::Driver.load` resolves7636+
# `Foo` against the empty scope and lands on bare `Foo` instead7637+
# of `Optcarrot_Foo`, which then poisons the local variable's7638+
# recorded type.7639+
saved_meth = @current_method_name7640+
@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
end76407662
scan_writer_calls(@meth_body_ids[i])76417663
end7664+
@current_method_name = saved_meth76427665
pop_scope76437666
i = i + 176447667
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 are14107+
# collected separately, and recursing into them here would14108+
# pull every nested method's locals into main()'s frame and14109+
# also mis-resolve constants (no lexical scope is in effect14110+
# at this scan, so e.g. `Video.new(...)` inside a module14111+
# function lands on bare `Video` instead of the namespaced14112+
# 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+
end1408414117
end1408514118
end1408614119
endtest/nested_class_module_const_resolution.rbaddedView file
Loading diff…