Commit fc61a949

fc61a94918a8Merged

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

Description

Merge pull request #1502 from ryanseys/rs-super-into-module

Support super into an included-module method

Details

Commit
fc61a94918a8
Parent
b1e4d47607fd
Merge parent
925c77a94fdf

src/analyze_scope.cmodifiedView file

@@ -1568,6 +1568,18 @@
15681568 }
15691569 }
15701570
1571+/* True if the method scope's body contains a `super` (an explicit-arg SuperNode
1572+ or a bare ForwardingSuperNode). */
1573+static int scope_body_has_super(Compiler *c, int scope_idx) {
1574+ const NodeTable *nt = c->nt;
1575+ for (int id = 0; id < nt->count; id++) {
1576+ if (c->nscope[id] != scope_idx) continue;
1577+ const char *ty = nt_type(nt, id);
1578+ if (ty && (!strcmp(ty, "SuperNode") || !strcmp(ty, "ForwardingSuperNode"))) return 1;
1579+ }
1580+ return 0;
1581+}
1582+
15711583 /* Process include calls in a single class body, creating scope copies for each
15721584 included module method. We copy (not mutate) so multiple classes can include
15731585 the same module independently. */
@@ -1598,17 +1610,52 @@
15981610 for (int ms = 0; ms < snap; ms++) {
15991611 Scope *src = &c->scopes[ms];
16001612 if (src->class_id != mod_id || src->is_cmethod || !src->name) continue;
1601- if (comp_method_in_class(c, ci, src->name) >= 0) continue;
1613+ const char *dst_name = src->name;
1614+ char inc_shadow[256];
1615+ int own = comp_method_in_class(c, ci, src->name);
1616+ if (own >= 0) {
1617+ /* The class overrides the module method. If the override calls super,
1618+ the module method is the super target: copy it under a shadow name
1619+ and chain to it so emit_super reaches it via the prepend-super path.
1620+ Otherwise the module method is simply shadowed -- nothing to emit. */
1621+ if (!scope_body_has_super(c, own)) continue;
1622+ const char *existing = comp_prep_chain_target(c, ci, src->name);
1623+ snprintf(inc_shadow, sizeof inc_shadow, "__inc_%d_%s",
1624+ c->classes[ci].prep_shadow_count++, src->name);
1625+ if (existing) {
1626+ /* Another included module already supplies the super target for this
1627+ method. A later include takes precedence (Ruby MRO: C -> Mlast ->
1628+ ... -> Mfirst), so retarget the class's super to this module's copy
1629+ and chain this copy to the previously included one:
1630+ name -> new_shadow -> earlier_shadow. */
1631+ char *prev = strdup(existing); /* stable copy: the slot is freed below */
1632+ ClassInfo *cif = &c->classes[ci];
1633+ for (int kk = 0; kk < cif->nprep_chain; kk++)
1634+ if (!strcmp(cif->prep_from[kk], src->name)) {
1635+ free(cif->prep_to[kk]);
1636+ cif->prep_to[kk] = strdup(inc_shadow);
1637+ break;
1638+ }
1639+ comp_prep_chain_add(cif, inc_shadow, prev);
1640+ free(prev);
1641+ } else {
1642+ comp_prep_chain_add(&c->classes[ci], src->name, inc_shadow);
1643+ }
1644+ dst_name = inc_shadow;
1645+ }
16021646 /* Create a new scope sharing the same AST nodes but owned by ci. */
1603- Scope *dst = comp_scope_new(c, src->name, src->def_node);
1647+ Scope *dst = comp_scope_new(c, dst_name, src->def_node);
16041648 int dst_idx = c->nscopes - 1;
16051649 /* comp_scope_new may realloc c->scopes; re-derive src pointer. */
16061650 src = &c->scopes[ms];
1607- /* When the target is a built-in class, `self` has a different (scalar)
1608- type than the module's object self, so clone the body and re-attribute
1609- it to the target -- otherwise the shared SelfNode resolves to the
1610- module and e.g. `self.to_s` mis-dispatches. */
1611- if (is_builtin_class_name(c->classes[ci].name) && src->body >= 0) {
1651+ /* Clone the body and re-attribute it to the target when either:
1652+ (a) the target is a built-in class, where `self` has a different
1653+ (scalar) type than the module's object self -- otherwise the shared
1654+ SelfNode resolves to the module and e.g. `self.to_s` mis-dispatches; or
1655+ (b) the copied method itself calls `super` (a multi-module chain), so
1656+ its super node resolves to this shadow scope (and thus the class's prep
1657+ chain) rather than to the source module, where the chain isn't set. */
1658+ if ((is_builtin_class_name(c->classes[ci].name) || scope_body_has_super(c, ms)) && src->body >= 0) {
16121659 int nb = nt_clone_subtree((NodeTable *)nt, src->body);
16131660 if (nb >= 0) {
16141661 comp_grow_node_arrays(c);