Commit 66e9388d
66e9388d2e97Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified
Description
fix: merge class reopens into existing class entry, close #5
Spinel was unconditionally pushing a new entry to @cls_names for every ClassNode in the AST, so reopening a class produced duplicate sp_X_s struct definitions and sp_X_new constructors that the C compiler rejected as redefinitions.
When a class name already exists in @cls_names, reuse that index and collect the new body's methods, attrs, includes, and ivars into the existing entry. The append_cls_meth and collect_ivars helpers were already idempotent on existing classes, so only the registration step needed to branch.
Includes a regression test (test/bm_reopen_class.rb) covering empty reopen and reopen that adds attr_accessor, method, and ivar usage that spans the two bodies.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 66e9388d2e97
- Parent
- c636e1d6ace1
spinel_codegen.rbmodifiedView file
return end # Class reopening: if the class was already registered (in an # earlier `class Foo ... end` block), reuse the existing entry # so methods and attrs from this body get appended rather than # producing a duplicate C struct/constructor. existing_ci = find_class_idx(cname) if existing_ci >= 0 ci = existing_ci body = @nd_body[nid] if body < 0 return end body_stmts = get_stmts(body) body_stmts.each { |sid| if @nd_type[sid] == "DefNode" collect_class_method(ci, sid) end if @nd_type[sid] == "CallNode" cn = @nd_name[sid] if cn != "include" if cn != "private" collect_attr_call(ci, sid) end end end } body_stmts.each { |sid| if @nd_type[sid] == "CallNode" if @nd_name[sid] == "include" inc_args = @nd_arguments[sid] if inc_args >= 0 inc_ids = get_args(inc_args) ik = 0 while ik < inc_ids.length if @nd_type[inc_ids[ik]] == "ConstantReadNode" mod_name = @nd_name[inc_ids[ik]] collect_module_methods_into_class(ci, mod_name) end ik = ik + 1 end end end end } collect_ivars(ci) return end parent = "" sp = @nd_superclass[nid] struct_fields = "".split(",")test/bm_reopen_class.rbaddedView file
Loading diff…