Commit 83d8f369

83d8f369a85fMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): pin lexical scope while walking class bodies in scan_new_calls

scan_new_calls walks the entire AST looking for Foo.new(args) patterns and propagates each arg's inferred type into Foo's initialize param-type table. The arg inference uses infer_type, which for an InstanceVariableReadNode consults @current_class_idx — and the scan never set it.

Result: Bar.new(@cpu, ...) inside Foo#initialize's body inferred @cpu against an empty scope (returns "int" — the default), then unify_call_types decided int + int = int and Bar.initialize's first param stayed at mrb_int. The fixpoint loop ran multiple iterations but each one re-discovered the same int via the same scopeless lookup.

Recurse explicitly into ClassNode / ModuleNode bodies, setting @current_class_idx and @current_lexical_scope to the enclosing class's registered name first.

Also: trim_const_scope_once hardened against name.rindex("_") returning nil under CRuby (the spinel runtime returns -1). The two-arg form if idx == nil || idx < 0 covers both.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
83d8f369a85f
Parent
78de785dc7bf

spinel_codegen.rbmodifiedView file

@@ -1029,7 +1029,9 @@
10291029 return ""
10301030 end
10311031 idx = name.rindex("_")
1032- if idx < 0
1032+ # CRuby returns nil when not found; spinel runtime returns -1.
1033+ # Treat both as "no underscore — root scope".
1034+ if idx == nil || idx < 0
10331035 return ""
10341036 end
10351037 name[0, idx]
@@ -6344,10 +6346,94 @@
63446346 "poly"
63456347 end
63466348
6349+ # Resolve a ClassNode AST id to its registered index in
6350+ # @cls_names, walking the same module-prefix chain that
6351+ # `resolve_const_read_name` / `find_class_idx` use at emit
6352+ # time. Returns -1 when the class isn't in @cls_names (e.g. a
6353+ # nested `class << self` body that hasn't been registered as a
6354+ # regular class).
6355+ def class_node_to_idx(nid)
6356+ cp = @nd_constant_path[nid]
6357+ if cp < 0
6358+ return -1
6359+ end
6360+ flat = const_ref_flat_name(cp)
6361+ if flat == ""
6362+ return -1
6363+ end
6364+ resolved = resolve_const_read_name(flat)
6365+ ci = find_class_idx(resolved)
6366+ if ci >= 0
6367+ return ci
6368+ end
6369+ find_class_idx(flat)
6370+ end
6371+
6372+ # Set @current_class_idx / @current_lexical_scope to the class
6373+ # introduced by `nid` (a ClassNode). No-op when the class isn't
6374+ # registered. Caller is responsible for save/restore.
6375+ def enter_class_scope_from_node(nid)
6376+ ci = class_node_to_idx(nid)
6377+ if ci >= 0
6378+ @current_class_idx = ci
6379+ @current_lexical_scope = @cls_names[ci]
6380+ end
6381+ end
6382+
6383+ # Append the module name introduced by `nid` (a ModuleNode) to
6384+ # `@current_lexical_scope`. Caller is responsible for save/
6385+ # restore. Mirrors the prefix pattern that
6386+ # `collect_module_with_prefix` uses for nested module names.
6387+ def enter_module_scope_from_node(nid)
6388+ cp = @nd_constant_path[nid]
6389+ if cp < 0
6390+ return
6391+ end
6392+ mname_s = const_ref_flat_name(cp)
6393+ if mname_s == ""
6394+ return
6395+ end
6396+ if @current_lexical_scope != ""
6397+ @current_lexical_scope = @current_lexical_scope + "_" + mname_s
6398+ else
6399+ @current_lexical_scope = mname_s
6400+ end
6401+ end
6402+
63476403 def scan_new_calls(nid)
63486404 if nid < 0
63496405 return
63506406 end
6407+ # When we descend into a class body, pin @current_class_idx so
6408+ # any `infer_type(@ivar)` in the args of a nested .new call
6409+ # resolves against this class's ivar table. Without this scope
6410+ # set-up, arguments like `@cpu` inside `Foo.initialize`'s
6411+ # body that contains `Bar.new(@cpu, ...)` infer as "int" (the
6412+ # default for an InstanceVariableReadNode with no scope), which
6413+ # then wedges Bar.initialize's first param at int even after
6414+ # multiple iterations of the fixpoint loop.
6415+ if @nd_type[nid] == "ClassNode"
6416+ saved_ci = @current_class_idx
6417+ saved_scope = @current_lexical_scope
6418+ enter_class_scope_from_node(nid)
6419+ body = @nd_body[nid]
6420+ if body >= 0
6421+ scan_new_calls(body)
6422+ end
6423+ @current_class_idx = saved_ci
6424+ @current_lexical_scope = saved_scope
6425+ return
6426+ end
6427+ if @nd_type[nid] == "ModuleNode"
6428+ saved_scope2 = @current_lexical_scope
6429+ enter_module_scope_from_node(nid)
6430+ body = @nd_body[nid]
6431+ if body >= 0
6432+ scan_new_calls(body)
6433+ end
6434+ @current_lexical_scope = saved_scope2
6435+ return
6436+ end
63516437 if @nd_type[nid] == "CallNode"
63526438 # Also infer top-level method param types from call sites
63536439 mname = @nd_name[nid]