Commit 831ee0e7

831ee0e7e53dMerged

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

Description

Nullable param: a still-unknown param with a nil default becomes poly

Post-fixpoint backstop -- a parameter constrained only by = nil is nullable, represented as poly so it can carry nil or a passed value.

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

Details

Commit
831ee0e7e53d
Parent
e7a064e33677

src/analyze.cmodifiedView file

@@ -1363,6 +1363,21 @@
13631363 if (!ch) break;
13641364 }
13651365
1366+ /* Backstop: a parameter still unknown but with a `= nil` default is a
1367+ nullable param -- represent it as poly so it can hold nil or a value. */
1368+ for (int s = 0; s < c->nscopes; s++) {
1369+ Scope *sc = &c->scopes[s];
1370+ for (int i = 0; i < sc->nparams; i++) {
1371+ if (sc->pdefault[i] < 0) continue;
1372+ const char *dty = nt_type(c->nt, sc->pdefault[i]);
1373+ if (!dty || strcmp(dty, "NilNode")) continue;
1374+ LocalVar *p = scope_local(sc, sc->pnames[i]);
1375+ if (p && p->type == TY_UNKNOWN) p->type = TY_POLY;
1376+ }
1377+ }
1378+ /* recompute returns: a method returning such a param is now poly */
1379+ for (int iter = 0; iter < 8; iter++) if (!infer_return_types(c)) break;
1380+
13661381 /* Reachability: an instance/free method is live only if its name is
13671382 referenced somewhere -- as a call name, an alias target, or a symbol
13681383 literal (covering send/method/define_method). Names never mentioned