Commit 0af422de

0af422deae50Merged

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

Description

Specialize inherited class methods that call subclass-specific methods

Base.find calling a bare adapter_find (or all) that a subclass defines or overrides is now specialized per calling subclass, so the inherited body's bare class-method calls resolve to that subclass's versions -- generalizing the existing bare-new specialization.

cmethod_needs_specialization replaces cmethod_has_bare_new: it triggers on a bare new, or a bare cmethod call that resolves to a different method for the calling subclass than for the defining class (an add or override). Only the bare-new create shape pins the return type to the subclass; other specializations let normal return inference type the cloned, subclass-attributed body.

Fixes bundle_class_14.

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

Details

Commit
0af422deae50
Parent
e4d3158536d3

src/analyze.cmodifiedView file

@@ -4010,6 +4010,31 @@
40104010 return 0;
40114011 }
40124012
4013+/* Does the inherited cls method `mi` (defined on def_cls) contain a bare call
4014+ that would resolve differently when run as a class method of `ci`? That is:
4015+ a bare `new` (constructs ci), or a bare cmethod call `m` that ci defines but
4016+ def_cls does not (so the inherited body's `m` means ci's version). */
4017+static int cmethod_needs_specialization(Compiler *c, int mi, int ci, int def_cls, int *has_new) {
4018+ const NodeTable *nt = c->nt;
4019+ int need = 0;
4020+ if (has_new) *has_new = 0;
4021+ for (int id = 0; id < nt->count; id++) {
4022+ if (c->nscope[id] != mi) continue;
4023+ const char *ty = nt_type(nt, id);
4024+ if (!ty || strcmp(ty, "CallNode")) continue;
4025+ if (nt_ref(nt, id, "receiver") >= 0) continue; /* receiverless only */
4026+ const char *nm = nt_str(nt, id, "name");
4027+ if (!nm) continue;
4028+ if (!strcmp(nm, "new")) { if (has_new) *has_new = 1; need = 1; continue; }
4029+ /* a bare cmethod call that resolves to a different method for ci than for
4030+ the defining class (ci adds or overrides it) */
4031+ int mci = comp_cmethod_in_chain(c, ci, nm, NULL);
4032+ int mdef = comp_cmethod_in_chain(c, def_cls, nm, NULL);
4033+ if (mci >= 0 && mci != mdef) need = 1;
4034+ }
4035+ return need;
4036+}
4037+
40134038 /* `Subclass.create` where `create` is an inherited class method whose body
40144039 does `new(...)`: Ruby's bare `new` constructs the *calling* class, so copy
40154040 the inherited cls method into each calling subclass (the copy's class_id
@@ -4036,7 +4061,8 @@
40364061 int def_cls = -1;
40374062 int mi = comp_cmethod_in_chain(c, ci, mname, &def_cls);
40384063 if (mi < 0 || def_cls == ci || mi >= snap) continue; /* not inherited */
4039- if (!cmethod_has_bare_new(c, mi)) continue;
4064+ int has_new = 0;
4065+ if (!cmethod_needs_specialization(c, mi, ci, def_cls, &has_new)) continue;
40404066
40414067 /* Copy the inherited cls method scope, owned by ci, with its OWN clone of
40424068 the body AST so locals/dispatches inside resolve against ci (the bare
@@ -4059,10 +4085,14 @@
40594085 dst->nrequired = src->nrequired;
40604086 dst->rest_idx = src->rest_idx;
40614087 dst->kwrest_idx = src->kwrest_idx;
4062- /* The bare `new` returns the specialized subclass, so a create-style
4063- method that returns its instance is typed as that subclass. */
4064- dst->ret = ty_object(ci);
4065- dst->ret_specialized = 1;
4088+ /* A bare-`new` create method returns the specialized subclass instance, so
4089+ pin its return type. Other specializations (a body calling a subclass-
4090+ specific class method) let the normal return inference compute the type
4091+ from the cloned, ci-attributed body. */
4092+ if (has_new) {
4093+ dst->ret = ty_object(ci);
4094+ dst->ret_specialized = 1;
4095+ }
40664096 if (src->blk_param) dst->blk_param = strdup(src->blk_param);
40674097 dst->nparams = src->nparams;
40684098 if (src->nparams > 0) {