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
} else if (cname) { child_class = comp_class_index(c, cname); /* reopened class/module */ /* A class can be opened bare first (no superclass -- e.g. just to hold a nested class) and reopened later with `< Super`. The parent link is read from def_node's "superclass" ref, so prefer the opening that declares one; otherwise the superclass is lost and a subclass's overrides aren't dispatched against the right ancestor chain (matz/spinel#1477). */ if (child_class >= 0 && nt_ref(c->nt, id, "superclass") >= 0 && c->classes[child_class].def_node >= 0 && nt_ref(c->nt, c->classes[child_class].def_node, "superclass") < 0) { c->classes[child_class].def_node = id; } } } else if (ty && !strcmp(ty, "DefNode")) {test/reopen_split_superclass_dispatch.rbaddedView file
Loading diff…
test/reopen_split_superclass_dispatch.rb.expectedaddedView file
Loading diff…