Commit 9bc171d1

9bc171d1da21Merged

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

Description

Bind poly-dispatched method params after ivar up-propagation

Three codegen/inference fixes uncovered while compiling optcarrot:

  • Poly ivar bitwise op-assign (@x &= v, @x >>= v, ...) emitted a raw C operator on sp_RbVal; coerce to int and re-box like the local path.
  • The zero-arg poly method dispatch declared its result temp as sp_RbVal when the call feeds a poly context, but the length-like and builtin-array arms assigned raw ints; box them when the result type is poly.
  • A method reached only through a poly receiver (e.g. @pads[0].poke(data), @envelope.write(data)) had its params left TY_UNKNOWN and was dropped, leaving the poly-dispatch call site referencing an undefined function. Split the method(:sym) backstop so untyped params are pinned to int first, then re-run param binding after the inheritance ivar up-propagation (which is what widens the base ivar to poly) so the bound method's params finally bind, then drop only what still has no type.

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

Details

Commit
9bc171d1da21
Parent
2b1e8fda8d42

src/analyze.cmodifiedView file

@@ -1252,32 +1252,29 @@
12521252 }
12531253 }
12541254
1255- /* Backstop: a reachable method still has TY_UNKNOWN params if it was never
1256- called with typed arguments (BFS marked it live by name, but no typed call
1257- site bound its args). Mark such methods unreachable so codegen skips them
1258- rather than exit(1)ing on the unknown type. */
1255+ /* Backstop step 1: a method reached only via method(:sym) is invoked through
1256+ the bound Method ABI, which passes mrb_int args -- default its untyped
1257+ params/ret to int rather than dropping it (which would leave it undeclared).
1258+ Done before the drop decision below so the freshly-typed params can
1259+ propagate through poly-dispatch param binding (e.g. a poke dispatch table
1260+ entry typed here flows into `@pads[0].poke(data)` -> Pad#poke). */
12591261 for (int s = 0; s < c->nscopes; s++) {
12601262 Scope *sc = &c->scopes[s];
1261- if (!sc->reachable || sc->nparams == 0) continue;
1263+ if (!sc->reachable || sc->nparams == 0 || !sc->name) continue;
12621264 int has_unknown = 0;
12631265 for (int i = 0; i < sc->nparams; i++) {
12641266 LocalVar *p = sc->pnames[i] ? scope_local(sc, sc->pnames[i]) : NULL;
12651267 if (p && p->type == TY_UNKNOWN) { has_unknown = 1; break; }
12661268 }
12671269 if (!has_unknown) continue;
1268- /* A method reached only via method(:sym) is invoked through the bound
1269- Method ABI, which passes mrb_int args -- default its untyped params to
1270- int rather than dropping the method (which would leave it undeclared). */
12711270 int taken = 0;
1272- if (sc->name) {
1273- for (int id = 0; id < c->nt->count && !taken; id++) {
1274- const char *nty = nt_type(c->nt, id);
1275- if (!nty || strcmp(nty, "CallNode")) continue;
1276- const char *nm = nt_str(c->nt, id, "name");
1277- if (!nm || strcmp(nm, "method")) continue;
1278- const char *msym = method_sym_arg(c, id);
1279- if (msym && !strcmp(msym, sc->name)) taken = 1;
1280- }
1271+ for (int id = 0; id < c->nt->count && !taken; id++) {
1272+ const char *nty = nt_type(c->nt, id);
1273+ if (!nty || strcmp(nty, "CallNode")) continue;
1274+ const char *nm = nt_str(c->nt, id, "name");
1275+ if (!nm || strcmp(nm, "method")) continue;
1276+ const char *msym = method_sym_arg(c, id);
1277+ if (msym && !strcmp(msym, sc->name)) taken = 1;
12811278 }
12821279 if (taken) {
12831280 for (int i = 0; i < sc->nparams; i++) {
@@ -1286,9 +1283,9 @@
12861283 }
12871284 if (sc->ret == TY_UNKNOWN) sc->ret = TY_INT;
12881285 }
1289- else sc->reachable = 0;
12901286 }
12911287
1288+
12921289 /* Propagate ivar types up the inheritance chain: a base-class method runs on
12931290 subclass instances, so an ivar it reads must carry the union of every
12941291 subclass's assignments. Without this, an abstract base whose @x is only set
@@ -1316,6 +1313,29 @@
13161313 if (!prop_changed) break;
13171314 }
13181315
1316+ /* Re-run param binding now that method(:sym) targets are int-typed (step 1
1317+ above) and ivars carry their inheritance-unioned types: a base method
1318+ calling `@x.foo(arg)` on a poly @x (only widened to poly by the up-propagation
1319+ just above) can finally bind foo's params. Without this the bound method
1320+ would stay TY_UNKNOWN and be dropped below, leaving an undefined function at
1321+ the poly-dispatch call site. */
1322+ build_ie_map(c);
1323+ for (int it = 0; it < 8; it++) { if (!infer_param_types(c)) break; }
1324+
1325+ /* Backstop step 2: a reachable method that STILL has TY_UNKNOWN params was
1326+ never bound by any typed call site -- mark it unreachable so codegen skips
1327+ it rather than exit(1)ing on the unknown type. */
1328+ for (int s = 0; s < c->nscopes; s++) {
1329+ Scope *sc = &c->scopes[s];
1330+ if (!sc->reachable || sc->nparams == 0) continue;
1331+ int has_unknown = 0;
1332+ for (int i = 0; i < sc->nparams; i++) {
1333+ LocalVar *p = sc->pnames[i] ? scope_local(sc, sc->pnames[i]) : NULL;
1334+ if (p && p->type == TY_UNKNOWN) { has_unknown = 1; break; }
1335+ }
1336+ if (has_unknown) sc->reachable = 0;
1337+ }
1338+
13191339 /* The method-reference backstop and ivar up-propagation above changed param
13201340 and ivar types after the main fixpoint, so re-run local write-type inference:
13211341 a local like `xfine = 8 - (data & 0x7)` only becomes int once its method's