Commit b7a8f2ae
b7a8f2ae3c80Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Unbox TY_POLY hash values in hash-pattern destructuring
{name: "Alice", age: 30} => {name:, age:} uses sp_SymPolyHash_get
which returns sp_RbVal. When the local variable has a concrete type
(string, int, float, etc.) the value is now unboxed via .v.s / .v.i
etc. before assignment, fixing the gcc type-mismatch error.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- b7a8f2ae3c80
- Parent
- 3f89227634c8
src/codegen.cmodifiedView file
@@ -10194,8 +10194,28 @@
1019410194
const char *lnm = nt_str(nt, ptgt, "name");1019510195
if (!lnm || !hn) continue;1019610196
emit_indent(b, indent);10197-
buf_printf(b, "lv_%s = sp_%sHash_get(_t%d, ", lnm, hn, thash);10198-
emit_expr(c, pkey, b); buf_puts(b, ");\n");10197+
/* unbox TY_POLY hash value to the local's concrete type */10198+
TyKind hvt = ty_hash_val(vt);10199+
Scope *hpsc = comp_scope_of(c, id);10200+
LocalVar *hplv = hpsc ? scope_local(hpsc, lnm) : NULL;10201+
TyKind hpltype = hplv ? hplv->type : TY_UNKNOWN;10202+
if (hvt == TY_POLY && hpltype != TY_UNKNOWN && hpltype != TY_POLY) {10203+
int htmp = ++g_tmp;10204+
buf_printf(b, "{ sp_RbVal _t%d = sp_%sHash_get(_t%d, ", htmp, hn, thash);10205+
emit_expr(c, pkey, b); buf_puts(b, "); ");10206+
if (hpltype == TY_STRING) buf_printf(b, "lv_%s = _t%d.v.s; }\n", lnm, htmp);10207+
else if (hpltype == TY_INT) buf_printf(b, "lv_%s = _t%d.v.i; }\n", lnm, htmp);10208+
else if (hpltype == TY_FLOAT) buf_printf(b, "lv_%s = _t%d.v.f; }\n", lnm, htmp);10209+
else if (hpltype == TY_BOOL) buf_printf(b, "lv_%s = _t%d.v.b; }\n", lnm, htmp);10210+
else if (hpltype == TY_SYMBOL) buf_printf(b, "lv_%s = (sp_sym)_t%d.v.i; }\n", lnm, htmp);10211+
else { buf_puts(b, "}\n"); emit_indent(b, indent);10212+
buf_printf(b, "lv_%s = sp_%sHash_get(_t%d, ", lnm, hn, thash);10213+
emit_expr(c, pkey, b); buf_puts(b, ");\n"); }10214+
}10215+
else {10216+
buf_printf(b, "lv_%s = sp_%sHash_get(_t%d, ", lnm, hn, thash);10217+
emit_expr(c, pkey, b); buf_puts(b, ");\n");10218+
}1019910219
}1020010220
}1020110221
return;