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
end end end if @nd_type[sid] == "ClassNode" collect_class_with_prefix(sid, cname) end if @nd_type[sid] == "ModuleNode" collect_module_with_prefix(sid, cname) end } body_stmts.each { |sid| if @nd_type[sid] == "CallNode" end end end # Nested class / module inside class. Mirroring the # nested-in-module path, the inner type is registered at top # level under its outer-class–prefixed name (e.g. `A::B` → # `A_B`) so a `A::B.new` call resolves via the same flat lookup. if @nd_type[sid] == "ClassNode" collect_class_with_prefix(sid, cname) end if @nd_type[sid] == "ModuleNode" collect_module_with_prefix(sid, cname) end } # Second pass: handle includes (after all own methods are known) body_stmts.each { |sid|test/nested_class_in_class.rbaddedView file
Loading diff…