Commit d45fae1a

d45fae1a24a7Merged

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

Description

Unify override family param/return types post-fixpoint for virtual dispatch

When a subclass overrides a method and widens a parameter from a scalar to TY_POLY, the C function signatures of the parent and child disagree. The virtual dispatch switch in emit_dispatch then generates a type mismatch: it evaluates args once using the base method's param type (scalar), but passes them to the override which expects sp_RbVal.

Fix: after the inference fixpoint, walk all pairs of user instance method scopes that share the same name and are in a parent-child class relationship. When one member has TY_POLY for a param slot and the other has a scalar type, widen both to TY_POLY so the C signatures match and the dispatch switch can call any member with the same arg temps. Likewise unify return types so the statement-expression can capture a result of the right C type from any arm.

initialize is excluded because it is never virtually dispatched (each call goes directly to the concrete class's ClassName.new).

Fixes bundle_class_13: imeth_override_widen_to_poly FAIL -> PASS; implicit_self_param_poly also now resolves correctly.

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

Details

Commit
d45fae1a24a7
Parent
136c30b19e91

src/analyze.cmodifiedView file

@@ -4253,6 +4253,49 @@
42534253 /* recompute returns: a method returning such a param is now poly */
42544254 for (int iter = 0; iter < 8; iter++) if (!infer_return_types(c)) break;
42554255
4256+ /* Post-fixpoint: unify param types across method override families.
4257+ When an override widens a param to TY_POLY but the parent (or
4258+ sibling) keeps it scalar, the generated C signatures disagree and
4259+ virtual dispatch can't call both with the same arg temps. Walk all
4260+ scope pairs that are overrides of the same instance method in a
4261+ parent-child class pair and widen any differing slot to TY_POLY. */
4262+ for (int s1 = 0; s1 < c->nscopes; s1++) {
4263+ Scope *sc1 = &c->scopes[s1];
4264+ if (sc1->class_id < 0 || !sc1->name || sc1->is_cmethod || sc1->nparams == 0) continue;
4265+ /* initialize is never virtually dispatched (always via ClassName.new), so
4266+ each override may have fully independent param types. */
4267+ if (!strcmp(sc1->name, "initialize")) continue;
4268+ for (int s2 = s1 + 1; s2 < c->nscopes; s2++) {
4269+ Scope *sc2 = &c->scopes[s2];
4270+ if (sc2->class_id < 0 || !sc2->name || sc2->is_cmethod || sc2->nparams == 0) continue;
4271+ if (strcmp(sc1->name, sc2->name) != 0) continue;
4272+ /* check ancestor relationship: one class must be an ancestor of the other */
4273+ int c1 = sc1->class_id, c2 = sc2->class_id;
4274+ int related = 0;
4275+ for (int k = c1; k >= 0; k = c->classes[k].parent) if (k == c2) { related = 1; break; }
4276+ if (!related)
4277+ for (int k = c2; k >= 0; k = c->classes[k].parent) if (k == c1) { related = 1; break; }
4278+ if (!related) continue;
4279+ int np = sc1->nparams < sc2->nparams ? sc1->nparams : sc2->nparams;
4280+ for (int k = 0; k < np; k++) {
4281+ LocalVar *p1 = scope_local(sc1, sc1->pnames[k]);
4282+ LocalVar *p2 = scope_local(sc2, sc2->pnames[k]);
4283+ if (!p1 || !p2) continue;
4284+ if (p1->type != p2->type && (p1->type == TY_POLY || p2->type == TY_POLY)) {
4285+ p1->type = TY_POLY;
4286+ p2->type = TY_POLY;
4287+ }
4288+ }
4289+ /* Also unify return types: if one member returns poly and another void/nil,
4290+ make both return poly so the dispatch statement-expression can capture
4291+ a scalar result from any arm. */
4292+ if (sc1->ret != sc2->ret && (sc1->ret == TY_POLY || sc2->ret == TY_POLY)) {
4293+ sc1->ret = TY_POLY;
4294+ sc2->ret = TY_POLY;
4295+ }
4296+ }
4297+ }
4298+
42564299 /* Promote loop-multiplication variables to bigint */
42574300 detect_bigint_loop_vars(c);
42584301 propagate_bigint_cascade(c);