Commit 33d74c06
33d74c06a5aaMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
analyze: propagate arg types to descendant overrides at all call sites
Extend the three param-type propagation paths to also push arg types into descendant class direct overrides, so every override in a cls_id dispatch switch has a consistent C signature.
Implicit-self calls (recv < 0):
- After binding the base-class method's params, also walk descendant classes and bind the same args to any direct override there. Covers both instance and class methods (is_cmethod flag).
- When a class method is not found in the base chain at all (only defined in descendants), still propagate args to each descendant.
Typed-object receiver calls (ty_is_object):
- After binding the base-class method's params, propagate to descendant direct overrides (same logic as implicit-self).
Return type inference:
- For both call sites above, unify the inferred return type with every descendant direct override's return type so the caller's type matches what the cls_id switch actually produces.
Exports comp_cmethod_in_class from compiler.c (was static) so analyze.c can use it in the new propagation loops.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 33d74c06a5aa
- Parent
- 470ad6b907df
src/analyze.cmodifiedView file
@@ -602,7 +602,20 @@
602602
}603603
}604604
int mi = comp_method_in_chain(c, cid, name, NULL);605-
if (mi >= 0) return method_call_ret(c, mi, id);605+
if (mi >= 0) {606+
TyKind r = method_call_ret(c, mi, id);607+
/* Unify with descendant direct overrides: codegen dispatch emits a608+
cls_id switch over all overrides, so the result type must cover all. */609+
for (int k = 0; k < c->nclasses; k++) {610+
int is_desc = 0;611+
for (int p = c->classes[k].parent; p >= 0; p = c->classes[p].parent)612+
if (p == cid) { is_desc = 1; break; }613+
if (!is_desc) continue;614+
int dmi = comp_method_in_class(c, k, name);615+
if (dmi >= 0) r = ty_unify(r, (TyKind)c->scopes[dmi].ret);616+
}617+
return r;618+
}606619
if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) return TY_STRING;607620
}608621
@@ -629,11 +642,28 @@
629642
for (int p = c->classes[k].parent; p >= 0; p = c->classes[p].parent)630643
if (p == self->class_id) { is_desc = 1; break; }631644
if (!is_desc) continue;632-
int dmi = comp_method_in_class(c, k, name);645+
int dmi = self->is_cmethod ? comp_cmethod_in_class(c, k, name) :646+
comp_method_in_class(c, k, name);633647
if (dmi >= 0) r = ty_unify(r, (TyKind)c->scopes[dmi].ret);634648
}635649
return r;636650
}651+
/* Method defined only in descendants (not in base chain):652+
unify return types of all descendant implementations. */653+
if (self->is_cmethod) {654+
TyKind r = TY_UNKNOWN; int found = 0;655+
for (int k = 0; k < c->nclasses; k++) {656+
int is_desc = 0;657+
for (int p = c->classes[k].parent; p >= 0; p = c->classes[p].parent)658+
if (p == self->class_id) { is_desc = 1; break; }659+
if (!is_desc) continue;660+
int dmi = comp_cmethod_in_class(c, k, name);661+
if (dmi < 0) continue;662+
r = found ? ty_unify(r, (TyKind)c->scopes[dmi].ret) : (TyKind)c->scopes[dmi].ret;663+
found = 1;664+
}665+
if (found) return r;666+
}637667
}638668
}639669
@@ -3360,15 +3390,21 @@
33603390
changed |= bind_call_params(c, id, mi);33613391
/* Propagate to descendant classes that directly override the same method.33623392
When Base#foo calls bar(arg), and Sub overrides bar, Sub#bar must also3363-
receive the same arg types so the cls_id-switch dispatch is type-safe. */3364-
if (mi >= 0 && caller_cid >= 0) {3393+
receive the same arg types so the cls_id-switch dispatch is type-safe.3394+
Also handles the case where only descendants define the method (mi < 03395+
from base chain, e.g. Base.find calls adapter_find defined only in3396+
Article and Comment descendants). */3397+
if (caller_cid >= 0) {3398+
Scope *caller_sc = comp_scope_of(c, id);3399+
int is_cm = caller_sc ? caller_sc->is_cmethod : 0;33653400
for (int k = 0; k < c->nclasses; k++) {33663401
if (k == caller_cid) continue;33673402
int is_desc = 0;33683403
for (int p = c->classes[k].parent; p >= 0; p = c->classes[p].parent)33693404
if (p == caller_cid) { is_desc = 1; break; }33703405
if (!is_desc) continue;3371-
int dmi = comp_method_in_class(c, k, name);3406+
int dmi = is_cm ? comp_cmethod_in_class(c, k, name) :3407+
comp_method_in_class(c, k, name);33723408
if (dmi >= 0) changed |= bind_call_params(c, id, dmi);33733409
}33743410
}@@ -3436,6 +3472,17 @@
34363472
!strcmp(name, "<=") || !strcmp(name, ">=")))34373473
mi3 = comp_method_in_chain(c, cid3, "<=>", NULL);34383474
changed |= bind_call_params(c, id, mi3);3475+
/* Also propagate to descendant overrides: codegen will emit a cls_id3476+
switch that calls each override, so each must have the right param3477+
types. */3478+
for (int k = 0; k < c->nclasses; k++) {3479+
int is_desc = 0;3480+
for (int p = c->classes[k].parent; p >= 0; p = c->classes[p].parent)3481+
if (p == cid3) { is_desc = 1; break; }3482+
if (!is_desc) continue;3483+
int dmi3 = comp_method_in_class(c, k, name);3484+
if (dmi3 >= 0) changed |= bind_call_params(c, id, dmi3);3485+
}34393486
}34403487
else if (rt == TY_POLY) {34413488
/* poly receiver: the call may dispatch to any user method of this name,src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…