Commit d9c69d5c
d9c69d5c5db5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #190 from mame/pr/pin-lexical-scope
fix(codegen): pin lexical scope while walking class bodies in scan_new_calls
Conflicts:
spinel_codegen.rb
Details
- Commit
- d9c69d5c5db5
- Parent
- a2066642ba48
- Merge parent
- 83d8f369a85f
spinel_codegen.rbmodifiedView file
@@ -1030,7 +1030,9 @@
10301030
return ""10311031
end10321032
idx = name.rindex("_")1033-
if idx < 01033+
# CRuby returns nil when not found; spinel runtime returns -1.1034+
# Treat both as "no underscore — root scope".1035+
if idx == nil || idx < 010341036
return ""10351037
end10361038
name[0, idx]@@ -6787,10 +6789,94 @@
67876789
val67886790
end67896791
6792+
# Resolve a ClassNode AST id to its registered index in6793+
# @cls_names, walking the same module-prefix chain that6794+
# `resolve_const_read_name` / `find_class_idx` use at emit6795+
# time. Returns -1 when the class isn't in @cls_names (e.g. a6796+
# nested `class << self` body that hasn't been registered as a6797+
# regular class).6798+
def class_node_to_idx(nid)6799+
cp = @nd_constant_path[nid]6800+
if cp < 06801+
return -16802+
end6803+
flat = const_ref_flat_name(cp)6804+
if flat == ""6805+
return -16806+
end6807+
resolved = resolve_const_read_name(flat)6808+
ci = find_class_idx(resolved)6809+
if ci >= 06810+
return ci6811+
end6812+
find_class_idx(flat)6813+
end6814+
6815+
# Set @current_class_idx / @current_lexical_scope to the class6816+
# introduced by `nid` (a ClassNode). No-op when the class isn't6817+
# registered. Caller is responsible for save/restore.6818+
def enter_class_scope_from_node(nid)6819+
ci = class_node_to_idx(nid)6820+
if ci >= 06821+
@current_class_idx = ci6822+
@current_lexical_scope = @cls_names[ci]6823+
end6824+
end6825+
6826+
# Append the module name introduced by `nid` (a ModuleNode) to6827+
# `@current_lexical_scope`. Caller is responsible for save/6828+
# restore. Mirrors the prefix pattern that6829+
# `collect_module_with_prefix` uses for nested module names.6830+
def enter_module_scope_from_node(nid)6831+
cp = @nd_constant_path[nid]6832+
if cp < 06833+
return6834+
end6835+
mname_s = const_ref_flat_name(cp)6836+
if mname_s == ""6837+
return6838+
end6839+
if @current_lexical_scope != ""6840+
@current_lexical_scope = @current_lexical_scope + "_" + mname_s6841+
else6842+
@current_lexical_scope = mname_s6843+
end6844+
end6845+
67906846
def scan_new_calls(nid)67916847
if nid < 067926848
return67936849
end6850+
# When we descend into a class body, pin @current_class_idx so6851+
# any `infer_type(@ivar)` in the args of a nested .new call6852+
# resolves against this class's ivar table. Without this scope6853+
# set-up, arguments like `@cpu` inside `Foo.initialize`'s6854+
# body that contains `Bar.new(@cpu, ...)` infer as "int" (the6855+
# default for an InstanceVariableReadNode with no scope), which6856+
# then wedges Bar.initialize's first param at int even after6857+
# multiple iterations of the fixpoint loop.6858+
if @nd_type[nid] == "ClassNode"6859+
saved_ci = @current_class_idx6860+
saved_scope = @current_lexical_scope6861+
enter_class_scope_from_node(nid)6862+
body = @nd_body[nid]6863+
if body >= 06864+
scan_new_calls(body)6865+
end6866+
@current_class_idx = saved_ci6867+
@current_lexical_scope = saved_scope6868+
return6869+
end6870+
if @nd_type[nid] == "ModuleNode"6871+
saved_scope2 = @current_lexical_scope6872+
enter_module_scope_from_node(nid)6873+
body = @nd_body[nid]6874+
if body >= 06875+
scan_new_calls(body)6876+
end6877+
@current_lexical_scope = saved_scope26878+
return6879+
end67946880
if @nd_type[nid] == "CallNode"67956881
# Also infer top-level method param types from call sites67966882
mname = @nd_name[nid]test/scan_new_calls_class_scope.rbaddedView file
Loading diff…