Commit 42b8ceee

42b8ceee90e7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 6 files · ~6 modified

Description

Inherited bare-new class methods: per-subclass specialization (#224, #229)

A class method with a bare 'new' inherited by a subclass must construct that subclass (Ruby's open-class new). Specialize such methods per calling subclass by cloning the method body AST (new nt_clone_subtree) into a fresh scope owned by the subclass, so the bare 'new', the 'instance = new' local's type, and 'instance.' dispatch all resolve against the subclass. The defining-class source is DCE'd unless called directly, and bare new(args) binds its args to the subclass initialize.

Adds nt_clone_subtree + comp_grow_node_arrays + Scope.ret_specialized. Fixes issue224_bare_new_inherited and issue229_uncalled_cls_method.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
42b8ceee90e7
Parent
342013d41088

src/analyze.cmodifiedView file

@@ -3873,6 +3873,113 @@
38733873 }
38743874 }
38753875
3876+/* True if class method scope `mi`'s body contains a bare `new` call (which
3877+ must rebind to the calling subclass, not the defining class). */
3878+static int cmethod_has_bare_new(Compiler *c, int mi) {
3879+ const NodeTable *nt = c->nt;
3880+ for (int id = 0; id < nt->count; id++) {
3881+ if (c->nscope[id] != mi) continue;
3882+ const char *ty = nt_type(nt, id);
3883+ if (ty && !strcmp(ty, "CallNode") && nt_ref(nt, id, "receiver") < 0 &&
3884+ nt_str(nt, id, "name") && !strcmp(nt_str(nt, id, "name"), "new"))
3885+ return 1;
3886+ }
3887+ return 0;
3888+}
3889+
3890+/* `Subclass.create` where `create` is an inherited class method whose body
3891+ does `new(...)`: Ruby's bare `new` constructs the *calling* class, so copy
3892+ the inherited cls method into each calling subclass (the copy's class_id
3893+ makes codegen's `new` resolve to that subclass). The defining-class source
3894+ is DCE'd unless it is itself called directly. Covers #224 / #229. */
3895+static void specialize_inherited_cls_new(Compiler *c) {
3896+ NodeTable *nt = (NodeTable *)c->nt;
3897+ int snap = c->nscopes;
3898+ int node_count = nt->count; /* don't scan nodes appended by cloning */
3899+ int did_clone = 0;
3900+ for (int id = 0; id < node_count; id++) {
3901+ const char *ty = nt_type(nt, id);
3902+ if (!ty || strcmp(ty, "CallNode")) continue;
3903+ int recv = nt_ref(nt, id, "receiver");
3904+ if (recv < 0) continue;
3905+ const char *rty = nt_type(nt, recv);
3906+ if (!rty || (strcmp(rty, "ConstantReadNode") && strcmp(rty, "ConstantPathNode"))) continue;
3907+ const char *cn = nt_str(nt, recv, "name");
3908+ int ci = cn ? comp_class_index(c, cn) : -1;
3909+ if (ci < 0) continue;
3910+ const char *mname = nt_str(nt, id, "name");
3911+ if (!mname || !strcmp(mname, "new")) continue;
3912+ if (comp_cmethod_in_class(c, ci, mname) >= 0) continue; /* defined on ci */
3913+ int def_cls = -1;
3914+ int mi = comp_cmethod_in_chain(c, ci, mname, &def_cls);
3915+ if (mi < 0 || def_cls == ci || mi >= snap) continue; /* not inherited */
3916+ if (!cmethod_has_bare_new(c, mi)) continue;
3917+
3918+ /* Copy the inherited cls method scope, owned by ci, with its OWN clone of
3919+ the body AST so locals/dispatches inside resolve against ci (the bare
3920+ `new` constructs ci, and `instance = new` is typed obj_ci). */
3921+ int src_body = c->scopes[mi].body;
3922+ int new_body = src_body >= 0 ? nt_clone_subtree(nt, src_body) : -1;
3923+ if (src_body >= 0 && new_body < 0) continue; /* clone failed: skip */
3924+ comp_grow_node_arrays(c);
3925+ did_clone = 1;
3926+ Scope *src = &c->scopes[mi];
3927+ Scope *dst = comp_scope_new(c, src->name, src->def_node);
3928+ src = &c->scopes[mi]; /* realloc-safe */
3929+ int dst_idx = c->nscopes - 1;
3930+ dst->body = new_body;
3931+ /* attribute the cloned subtree to the new scope */
3932+ if (new_body >= 0) walk_scope(c, new_body, dst_idx, ci);
3933+ dst->class_id = ci;
3934+ dst->is_cmethod = 1;
3935+ dst->yields = src->yields;
3936+ dst->nrequired = src->nrequired;
3937+ dst->rest_idx = src->rest_idx;
3938+ dst->kwrest_idx = src->kwrest_idx;
3939+ /* The bare `new` returns the specialized subclass, so a create-style
3940+ method that returns its instance is typed as that subclass. */
3941+ dst->ret = ty_object(ci);
3942+ dst->ret_specialized = 1;
3943+ if (src->blk_param) dst->blk_param = strdup(src->blk_param);
3944+ dst->nparams = src->nparams;
3945+ if (src->nparams > 0) {
3946+ dst->pnames = malloc(sizeof(char *) * (size_t)src->nparams);
3947+ dst->pdefault = malloc(sizeof(int) * (size_t)src->nparams);
3948+ for (int p = 0; p < src->nparams; p++) {
3949+ dst->pnames[p] = src->pnames[p] ? strdup(src->pnames[p]) : NULL;
3950+ dst->pdefault[p] = src->pdefault ? src->pdefault[p] : -1;
3951+ if (dst->pnames[p]) { LocalVar *lv = scope_local_intern(dst, dst->pnames[p]); lv->is_param = 1; }
3952+ }
3953+ }
3954+ }
3955+ /* DCE the now-shadowed source cls methods that are never called on their
3956+ own defining class. */
3957+ for (int s = 0; s < snap; s++) {
3958+ Scope *src = &c->scopes[s];
3959+ if (!src->is_cmethod || !src->name || src->class_id < 0) continue;
3960+ /* did we specialize this one into a subclass? (a fresh cmethod copy with
3961+ the same name was appended) */
3962+ int specialized = 0;
3963+ for (int d = snap; d < c->nscopes; d++)
3964+ if (c->scopes[d].is_cmethod && c->scopes[d].name &&
3965+ !strcmp(c->scopes[d].name, src->name)) { specialized = 1; break; }
3966+ if (!specialized) continue;
3967+ /* keep it if called directly as <DefiningClass>.<name> */
3968+ int called_direct = 0;
3969+ for (int id = 0; id < nt->count && !called_direct; id++) {
3970+ if (!nt_type(nt, id) || strcmp(nt_type(nt, id), "CallNode")) continue;
3971+ if (!nt_str(nt, id, "name") || strcmp(nt_str(nt, id, "name"), src->name)) continue;
3972+ int r = nt_ref(nt, id, "receiver");
3973+ if (r < 0 || !nt_type(nt, r)) continue;
3974+ if (strcmp(nt_type(nt, r), "ConstantReadNode") && strcmp(nt_type(nt, r), "ConstantPathNode")) continue;
3975+ if (comp_class_index(c, nt_str(nt, r, "name")) == src->class_id) called_direct = 1;
3976+ }
3977+ if (!called_direct) src->is_transplanted_source = 1;
3978+ }
3979+ /* The cloned bodies introduced new local/ivar nodes; intern them. */
3980+ if (did_clone) register_locals(c);
3981+}
3982+
38763983 /* For each class, find `prepend M` declarations and transplant M's instance
38773984 methods into the class with shadow-chain renaming so `super` can route
38783985 from M's body to the original (now renamed) class body. */
@@ -5357,6 +5464,17 @@
53575464 }
53585465
53595466 if (recv < 0) {
5467+ /* bare `new(args)` inside a class method constructs the enclosing
5468+ (possibly specialized) class -> bind args to that class's
5469+ initialize, so the subclass constructor's params get typed. */
5470+ if (name && !strcmp(name, "new")) {
5471+ Scope *s = comp_scope_of(c, id);
5472+ if (s && s->is_cmethod && s->class_id >= 0) {
5473+ int initmi = comp_method_in_chain(c, s->class_id, "initialize", NULL);
5474+ if (initmi >= 0) changed |= bind_call_params(c, id, initmi);
5475+ }
5476+ continue;
5477+ }
53605478 int mi = comp_method_index(c, name);
53615479 int caller_cid = -1;
53625480 /* bare call inside an instance_eval/exec block: dispatch on the
@@ -6501,6 +6619,9 @@
65016619 /* implicit return: the body's value */
65026620 for (int s = 1; s < c->nscopes; s++) {
65036621 Scope *sc = &c->scopes[s];
6622+ /* Specialized inherited-cls-new copies keep their fixed subclass return
6623+ type (the shared body's bare `new` would otherwise infer the base). */
6624+ if (sc->ret_specialized) continue;
65046625 /* An empty method body returns nil; if its value is used at all it must
65056626 be poly (a void C function yields nothing to read). */
65066627 int empty_body = sc->body < 0;
@@ -7003,6 +7124,7 @@
70037124 register_includes(c);
70047125 register_extends(c);
70057126 register_prepends(c);
7127+ specialize_inherited_cls_new(c);
70067128
70077129 /* collect top-level `include <Mod>` calls so bare method calls can
70087130 resolve to module_function methods in those modules. */