Commit bdf7a484

bdf7a4845e46Merged

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

Description

feat(codegen): collect nested class / module declarations

class A; class B; ... end; end produced

/tmp/_t.c: error: unknown type name 'sp_A_B'

because collect_class_with_prefix only walked DefNode / ConstantWriteNode / CallNode (attr_*) inside the class body — nested ClassNode / ModuleNode were skipped, so B never made it into @cls_names and no struct or constructor was emitted. The user-side reference A::B.new then resolved to a class index that didn't exist.

Mirror the existing nested-in-module path: in both the new-class and the class-reopen branches, also recurse into nested ClassNode / ModuleNode, calling collect_class_with_prefix(sid, cname) / collect_module_with_prefix(sid, cname) so the inner type is registered under its outer-prefixed flat name (A::B → A_B). That matches how module M; class C already becomes M_C, and lets the existing resolve_const_ref_name lookup on A::B.new find the registered class without further changes.

Adds test/nested_class_in_class.rb covering direct A::B.new, indirect construction through an outer-instance method, and a 3-deep Outer::Mid::Inner chain.

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

Details

Commit
bdf7a4845e46
Parent
c98048e104bd

spinel_codegen.rbmodifiedView file

@@ -3745,6 +3745,12 @@
37453745 end
37463746 end
37473747 end
3748+ if @nd_type[sid] == "ClassNode"
3749+ collect_class_with_prefix(sid, cname)
3750+ end
3751+ if @nd_type[sid] == "ModuleNode"
3752+ collect_module_with_prefix(sid, cname)
3753+ end
37483754 }
37493755 body_stmts.each { |sid|
37503756 if @nd_type[sid] == "CallNode"
@@ -3896,6 +3902,16 @@
38963902 end
38973903 end
38983904 end
3905+ # Nested class / module inside class. Mirroring the
3906+ # nested-in-module path, the inner type is registered at top
3907+ # level under its outer-class–prefixed name (e.g. `A::B` →
3908+ # `A_B`) so a `A::B.new` call resolves via the same flat lookup.
3909+ if @nd_type[sid] == "ClassNode"
3910+ collect_class_with_prefix(sid, cname)
3911+ end
3912+ if @nd_type[sid] == "ModuleNode"
3913+ collect_module_with_prefix(sid, cname)
3914+ end
38993915 }
39003916 # Second pass: handle includes (after all own methods are known)
39013917 body_stmts.each { |sid|