Commit 470ad6b9

470ad6b907dfMerged

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

Description

analyze: propagate param/return types to descendant overrides

Two related fixes for implicit-self dispatch type inference:

  1. In infer_param_types, when a parent-class method calls another method via implicit-self, also propagate argument types to any descendant class that directly overrides that method. Previously only the base-class override received the widened param type, leaving sibling overrides at TY_UNKNOWN and causing C compile errors in the dispatch switch.

  2. In infer_call for implicit-self calls, unify the inferred return type across all descendant direct overrides. Codegen emits a cls_id switch over every override, so the return type of the call must accommodate every arm. Without this, a caller method (e.g. kick) could be typed as TY_STRING while the dispatch switch body returns TY_POLY, producing a C type mismatch.

  3. In infer_operator_type, string + poly operand now returns TY_POLY to match codegen's sp_poly_add path (previously the mismatch silently produced wrong C code).

Fixes bundle_classd_45 (subclass_inherited_attr_reader + subclass_override_param_propagate).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
470ad6b907df
Parent
65ddbe5541df

src/analyze.cmodifiedView file

@@ -619,7 +619,21 @@
619619 int mi = comp_method_in_chain(c, self->class_id, name, NULL);
620620 if (mi < 0 && self->is_cmethod)
621621 mi = comp_cmethod_in_chain(c, self->class_id, name, NULL);
622- if (mi >= 0) return method_call_ret(c, mi, id);
622+ if (mi >= 0) {
623+ TyKind r = method_call_ret(c, mi, id);
624+ /* Unify with descendant direct overrides: codegen dispatch will
625+ emit a cls_id switch over all overrides, so the return type
626+ must accommodate every override's return type. */
627+ for (int k = 0; k < c->nclasses; k++) {
628+ int is_desc = 0;
629+ for (int p = c->classes[k].parent; p >= 0; p = c->classes[p].parent)
630+ if (p == self->class_id) { is_desc = 1; break; }
631+ if (!is_desc) continue;
632+ int dmi = comp_method_in_class(c, k, name);
633+ if (dmi >= 0) r = ty_unify(r, (TyKind)c->scopes[dmi].ret);
634+ }
635+ return r;
636+ }
623637 }
624638 }
625639
@@ -1270,7 +1284,10 @@
12701284 if (recv >= 0 && argc == 1 && is_arith_op(name)) {
12711285 if (rt == TY_STRING) {
12721286 if (!strcmp(name, "%")) return TY_STRING; /* sprintf (array or single value) */
1273- if (!strcmp(name, "+") || !strcmp(name, "*")) return TY_STRING;
1287+ if (!strcmp(name, "+") || !strcmp(name, "*")) {
1288+ if (a0 == TY_POLY) return TY_POLY; /* codegen uses sp_poly_add for poly operand */
1289+ return TY_STRING;
1290+ }
12741291 return TY_UNKNOWN;
12751292 }
12761293 /* array + (same kind) -> same kind */
@@ -3326,10 +3343,14 @@
33263343
33273344 if (recv < 0) {
33283345 int mi = comp_method_index(c, name);
3346+ int caller_cid = -1;
33293347 if (mi < 0) {
33303348 Scope *self = comp_scope_of(c, id);
33313349 if (self->class_id >= 0) {
3332- mi = comp_method_in_chain(c, self->class_id, name, NULL);
3350+ caller_cid = self->class_id;
3351+ int def_cid = -1;
3352+ mi = comp_method_in_chain(c, self->class_id, name, &def_cid);
3353+ if (mi >= 0 && def_cid >= 0) caller_cid = def_cid;
33333354 /* inside a class method: also check sibling class methods */
33343355 if (mi < 0 && self->is_cmethod)
33353356 mi = comp_cmethod_in_chain(c, self->class_id, name, NULL);
@@ -3337,6 +3358,20 @@
33373358 }
33383359 if (mi < 0) mi = comp_included_method_index(c, name);
33393360 changed |= bind_call_params(c, id, mi);
3361+ /* Propagate to descendant classes that directly override the same method.
3362+ When Base#foo calls bar(arg), and Sub overrides bar, Sub#bar must also
3363+ receive the same arg types so the cls_id-switch dispatch is type-safe. */
3364+ if (mi >= 0 && caller_cid >= 0) {
3365+ for (int k = 0; k < c->nclasses; k++) {
3366+ if (k == caller_cid) continue;
3367+ int is_desc = 0;
3368+ for (int p = c->classes[k].parent; p >= 0; p = c->classes[p].parent)
3369+ if (p == caller_cid) { is_desc = 1; break; }
3370+ if (!is_desc) continue;
3371+ int dmi = comp_method_in_class(c, k, name);
3372+ if (dmi >= 0) changed |= bind_call_params(c, id, dmi);
3373+ }
3374+ }
33403375 continue;
33413376 }
33423377 /* Class.new -> initialize params; Class.cmethod -> cmethod params */