Commit a9bec255

a9bec255b494Merged

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

Description

Add nil?/frozen? coverage for arrays, hashes, scalars, and nil-default params

  • codegen: nil? for array/hash types emits NULL-pointer check
  • codegen: frozen? for TY_INT/FLOAT/SYMBOL/BOOL/NIL always returns true
  • codegen: freeze/frozen? for hash types via sp_gc_freeze/sp_gc_is_frozen
  • codegen: hash []= raises FrozenError when hash is frozen
  • codegen: nil default for TY_INT params emits SP_INT_NIL (not 0) so that nil? checks work correctly on optional int params
  • codegen: nil default for TY_FLOAT/TY_STRING params emit the nil sentinel value (sp_float_nil() / NULL) for consistency
  • analyze: backstop widens nil-default TY_SYMBOL/TY_BOOL params to TY_POLY since those types have no nil sentinel

Unlocks: i918 (hash freeze/frozen?), nil_default_param_widens_to_poly (nil? on symbol param), and three more.

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

Details

Commit
a9bec255b494
Parent
a25ecbf674c9

src/analyze.cmodifiedView file

@@ -2547,7 +2547,9 @@
25472547 }
25482548
25492549 /* Backstop: a parameter still unknown but with a `= nil` default is a
2550- nullable param -- represent it as poly so it can hold nil or a value. */
2550+ nullable param -- represent it as poly so it can hold nil or a value.
2551+ Also widen TY_SYMBOL/TY_BOOL params: those types have no nil sentinel
2552+ and must be boxed into poly when the nil default is reachable. */
25512553 for (int s = 0; s < c->nscopes; s++) {
25522554 Scope *sc = &c->scopes[s];
25532555 for (int i = 0; i < sc->nparams; i++) {
@@ -2555,7 +2557,9 @@
25552557 const char *dty = nt_type(c->nt, sc->pdefault[i]);
25562558 if (!dty || strcmp(dty, "NilNode")) continue;
25572559 LocalVar *p = scope_local(sc, sc->pnames[i]);
2558- if (p && p->type == TY_UNKNOWN) p->type = TY_POLY;
2560+ if (!p) continue;
2561+ if (p->type == TY_UNKNOWN || p->type == TY_SYMBOL || p->type == TY_BOOL)
2562+ p->type = TY_POLY;
25592563 }
25602564 }
25612565