Commit 96875f62

96875f620fbfMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Resolve a reopened class's superclass from the opening that declares it

A class can be opened bare first (no superclass -- e.g. just to hold a nested class) and reopened later with < Super. Collection recorded the class's def_node at the first opening and never updated it on reopen, so the parent link -- read from def_node's "superclass" ref -- came from the bare opening and was lost. The class then had no parent, its ancestor chain stopped short, and a subclass's method overrides were not dispatched against it (and inherited methods failed to resolve through the broken chain).

On reopen, if the reopening declares a superclass and the recorded def_node does not, switch def_node to the reopening. Both openings' bodies are still collected (the walk visits each ClassNode), so a nested class defined in the bare opening is unaffected; only the superclass-bearing metadata is corrected. Harmless for consistent declarations (a def_node that already has a superclass is kept) and for the legal class Foo < Bar then bare class Foo order.

This pattern is rejected by MRI as a superclass mismatch but arises in spinel from wholesale-inlined libraries (the tep proxy block-DSL), so the regression test's expected output is checked in rather than regenerated from CRuby.

964 test (gcc + clang) / 57 bench / optcarrot 59662; repro clean under clang -fsanitize=address,undefined.

Refs #1477

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
96875f620fbf
Parent
6eeb7cf1b818

src/analyze_scope.cmodifiedView file

@@ -602,6 +602,16 @@
602602 }
603603 else if (cname) {
604604 child_class = comp_class_index(c, cname); /* reopened class/module */
605+ /* A class can be opened bare first (no superclass -- e.g. just to hold a
606+ nested class) and reopened later with `< Super`. The parent link is read
607+ from def_node's "superclass" ref, so prefer the opening that declares one;
608+ otherwise the superclass is lost and a subclass's overrides aren't
609+ dispatched against the right ancestor chain (matz/spinel#1477). */
610+ if (child_class >= 0 && nt_ref(c->nt, id, "superclass") >= 0 &&
611+ c->classes[child_class].def_node >= 0 &&
612+ nt_ref(c->nt, c->classes[child_class].def_node, "superclass") < 0) {
613+ c->classes[child_class].def_node = id;
614+ }
605615 }
606616 }
607617 else if (ty && !strcmp(ty, "DefNode")) {