Commit 8505715f

8505715f8e0fMerged

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

Description

Poly method dispatch with arguments

When a call's receiver is poly, the target user method is not statically known, so its params were never bound and stayed unknown, failing to compile ("param has unsupported type unknown").

Inference: for a poly receiver, bind the params of every user method matching the call name, so candidate methods get typed from the arg types.

Codegen: extend the poly cls_id switch to argument-bearing calls. Evaluate the receiver and args once into temps, then dispatch to the matching user method per class (and to sp_IntArray_get for a builtin-array []).

Enables a.call(x) / a[i] where a may be one of several classes.

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

Details

Commit
8505715f8e0f
Parent
9f59e4bd4906

src/analyze.cmodifiedView file

@@ -1541,6 +1541,13 @@
15411541 TyKind rt = infer_type(c, recv);
15421542 if (ty_is_object(rt))
15431543 changed |= bind_call_params(c, id, comp_method_in_chain(c, ty_object_class(rt), name, NULL));
1544+ else if (rt == TY_POLY) {
1545+ /* poly receiver: the call may dispatch to any user method of this name,
1546+ so bind every candidate's params (they would otherwise stay UNKNOWN
1547+ and fail to compile). */
1548+ for (int k = 0; k < c->nclasses; k++)
1549+ changed |= bind_call_params(c, id, comp_method_in_chain(c, k, name, NULL));
1550+ }
15441551 }
15451552 return changed;
15461553 }