Commit cfe1307a
cfe1307a274dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Poly-receiver [] / fetch: box args and add hash-storage arms
Two fixes to the argc>0 poly dispatch:
- box a call-site argument when the dispatched candidate's parameter is poly (a widened def / def fetch param) instead of passing a raw concrete value.
- when the poly value is actually a string-keyed hash,
[]/fetchdispatch to the matching hash storage (str_str / str_int / str_poly), emitting the variant whose value type fits the result temp (boxing for a poly result), with the fetch default / KeyError semantics.
Clears poly_recv_fetch_hash_arms from the FAIL set.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- cfe1307a274d
- Parent
- a3bea7cc5b1f
src/codegen.cmodifiedView file
@@ -1951,7 +1951,16 @@
19511951
Buf cb; memset(&cb, 0, sizeof cb);19521952
buf_printf(&cb, "sp_%s_%s((sp_%s *)_t%d.v.p", c->classes[defcls].name,19531953
mc(c->scopes[mi].name), c->classes[defcls].name, tv);1954-
for (int a = 0; a < argc; a++) buf_printf(&cb, ", _t%d", atmp[a]);1954+
for (int a = 0; a < argc; a++) {1955+
/* box the call-site arg if this candidate's parameter is poly */1956+
TyKind pt = TY_UNKNOWN;1957+
if (a < c->scopes[mi].nparams) { LocalVar *pv = scope_local(&c->scopes[mi], c->scopes[mi].pnames[a]); if (pv) pt = pv->type; }1958+
TyKind at = infer_type(c, argv[a]);1959+
buf_puts(&cb, ", ");1960+
char tn[32]; snprintf(tn, sizeof tn, "_t%d", atmp[a]);1961+
if (pt == TY_POLY && at != TY_POLY) emit_boxed_text(c, at, tn, &cb);1962+
else buf_puts(&cb, tn);1963+
}19551964
buf_puts(&cb, ")");19561965
buf_printf(b, " case %d: ", k);19571966
if (mret == TY_VOID || mret == TY_NIL || method_is_void(&c->scopes[mi])) buf_puts(b, cb.p); /* no usable value */@@ -1966,6 +1975,36 @@
19661975
if (is_index) {19671976
buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_IntArray_get((sp_IntArray *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);19681977
}1978+
/* the poly value may actually be a string-keyed hash: dispatch `[]` /1979+
`fetch` to the matching hash storage, boxing the value into the poly1980+
result. */1981+
int is_aref = !strcmp(name, "[]") && argc == 1;1982+
int is_fetch = !strcmp(name, "fetch") && (argc == 1 || argc == 2);1983+
if ((is_aref || is_fetch) && infer_type(c, argv[0]) == TY_STRING) {1984+
TyKind trt = is_scalar_ret(ret) ? ret : TY_INT; /* the result temp's type */1985+
static const struct { const char *cls, *hn; TyKind vt; } HV[] = {1986+
{"SP_BUILTIN_STR_STR_HASH", "StrStr", TY_STRING},1987+
{"SP_BUILTIN_STR_INT_HASH", "StrInt", TY_INT},1988+
{"SP_BUILTIN_STR_POLY_HASH", "StrPoly", TY_POLY},1989+
};1990+
for (unsigned hvi = 0; hvi < sizeof HV / sizeof HV[0]; hvi++) {1991+
/* only a variant whose value fits the result temp can be emitted */1992+
if (ret != TY_POLY && HV[hvi].vt != trt) continue;1993+
char getx[200];1994+
snprintf(getx, sizeof getx, "sp_%sHash_get((sp_%sHash *)_t%d.v.p, _t%d)", HV[hvi].hn, HV[hvi].hn, tv, atmp[0]);1995+
buf_printf(b, " case %s: _t%d = sp_%sHash_has_key((sp_%sHash *)_t%d.v.p, _t%d) ? ",1996+
HV[hvi].cls, tr, HV[hvi].hn, HV[hvi].hn, tv, atmp[0]);1997+
if (ret == TY_POLY) emit_boxed_text(c, HV[hvi].vt, getx, b); else buf_puts(b, getx);1998+
buf_puts(b, " : ");1999+
if (is_fetch && argc == 2) {2000+
char dn[32]; snprintf(dn, sizeof dn, "_t%d", atmp[1]);2001+
if (ret == TY_POLY) emit_boxed_text(c, infer_type(c, argv[1]), dn, b); else buf_puts(b, dn);2002+
}2003+
else if (is_fetch) { buf_puts(b, "(sp_raise_cls(\"KeyError\", \"key not found\"), "); buf_puts(b, ret == TY_POLY ? "sp_box_nil()" : default_value(trt)); buf_puts(b, ")"); }2004+
else buf_puts(b, ret == TY_POLY ? "sp_box_nil()" : default_value(trt));2005+
buf_puts(b, "; break;");2006+
}2007+
}19692008
buf_printf(b, " } _t%d; })", tr);19702009
free(atmp);19712010
return;