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 end
10321032 idx = name.rindex("_")
1033- if idx < 0
1033+ # CRuby returns nil when not found; spinel runtime returns -1.
1034+ # Treat both as "no underscore — root scope".
1035+ if idx == nil || idx < 0
10341036 return ""
10351037 end
10361038 name[0, idx]
@@ -6787,10 +6789,94 @@
67876789 val
67886790 end
67896791
6792+ # Resolve a ClassNode AST id to its registered index in
6793+ # @cls_names, walking the same module-prefix chain that
6794+ # `resolve_const_read_name` / `find_class_idx` use at emit
6795+ # time. Returns -1 when the class isn't in @cls_names (e.g. a
6796+ # nested `class << self` body that hasn't been registered as a
6797+ # regular class).
6798+ def class_node_to_idx(nid)
6799+ cp = @nd_constant_path[nid]
6800+ if cp < 0
6801+ return -1
6802+ end
6803+ flat = const_ref_flat_name(cp)
6804+ if flat == ""
6805+ return -1
6806+ end
6807+ resolved = resolve_const_read_name(flat)
6808+ ci = find_class_idx(resolved)
6809+ if ci >= 0
6810+ return ci
6811+ end
6812+ find_class_idx(flat)
6813+ end
6814+
6815+ # Set @current_class_idx / @current_lexical_scope to the class
6816+ # introduced by `nid` (a ClassNode). No-op when the class isn't
6817+ # 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 >= 0
6821+ @current_class_idx = ci
6822+ @current_lexical_scope = @cls_names[ci]
6823+ end
6824+ end
6825+
6826+ # Append the module name introduced by `nid` (a ModuleNode) to
6827+ # `@current_lexical_scope`. Caller is responsible for save/
6828+ # restore. Mirrors the prefix pattern that
6829+ # `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 < 0
6833+ return
6834+ end
6835+ mname_s = const_ref_flat_name(cp)
6836+ if mname_s == ""
6837+ return
6838+ end
6839+ if @current_lexical_scope != ""
6840+ @current_lexical_scope = @current_lexical_scope + "_" + mname_s
6841+ else
6842+ @current_lexical_scope = mname_s
6843+ end
6844+ end
6845+
67906846 def scan_new_calls(nid)
67916847 if nid < 0
67926848 return
67936849 end
6850+ # When we descend into a class body, pin @current_class_idx so
6851+ # any `infer_type(@ivar)` in the args of a nested .new call
6852+ # resolves against this class's ivar table. Without this scope
6853+ # set-up, arguments like `@cpu` inside `Foo.initialize`'s
6854+ # body that contains `Bar.new(@cpu, ...)` infer as "int" (the
6855+ # default for an InstanceVariableReadNode with no scope), which
6856+ # then wedges Bar.initialize's first param at int even after
6857+ # multiple iterations of the fixpoint loop.
6858+ if @nd_type[nid] == "ClassNode"
6859+ saved_ci = @current_class_idx
6860+ saved_scope = @current_lexical_scope
6861+ enter_class_scope_from_node(nid)
6862+ body = @nd_body[nid]
6863+ if body >= 0
6864+ scan_new_calls(body)
6865+ end
6866+ @current_class_idx = saved_ci
6867+ @current_lexical_scope = saved_scope
6868+ return
6869+ end
6870+ if @nd_type[nid] == "ModuleNode"
6871+ saved_scope2 = @current_lexical_scope
6872+ enter_module_scope_from_node(nid)
6873+ body = @nd_body[nid]
6874+ if body >= 0
6875+ scan_new_calls(body)
6876+ end
6877+ @current_lexical_scope = saved_scope2
6878+ return
6879+ end
67946880 if @nd_type[nid] == "CallNode"
67956881 # Also infer top-level method param types from call sites
67966882 mname = @nd_name[nid]