Commit f4869a2e

f4869a2e1761Merged

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

Description

Fix empty {} hash literal with unknown type in emit_expr and emit_arg_or_default

When a hash literal {} has TY_UNKNOWN type (not yet inferred), emit_expr would hit unsupported(). Two fixes:

  1. emit_arg_or_default: when the default or provided argument is an empty {} literal with unknown type, emit the constructor matching the parameter's declared type (mirroring the existing [] array fix).

  2. emit_expr: when a HashNode/KeywordHashNode has TY_UNKNOWN and zero elements, fall back to sp_StrPolyHash_new() rather than unsupported().

Fixes bundle_class_11, hash_param_widen_from_callee_slot, ivar_assign_and_expr, and lv_widen_from_callee_arg_slot.

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

Details

Commit
f4869a2e1761
Parent
87bb556ca4b0

src/codegen.cmodifiedView file

@@ -2084,7 +2084,19 @@
20842084 if (pt == TY_POLY_ARRAY) buf_puts(out, "sp_PolyArray_new()");
20852085 else { const char *k = array_kind(pt); if (k) buf_printf(out, "sp_%sArray_new()", k); else emit_expr(c, provided, out); }
20862086 }
2087- else emit_expr(c, provided, out);
2087+ /* empty hash literal `{}` with unknown type: use the parameter's type */
2088+ else {
2089+ int phn = 0;
2090+ int is_empty_hash = pty_node && (!strcmp(pty_node, "HashNode") || !strcmp(pty_node, "KeywordHashNode")) &&
2091+ (nt_arr(c->nt, provided, "elements", &phn), phn == 0);
2092+ TyKind at2 = comp_ntype(c, provided);
2093+ if (is_empty_hash && at2 == TY_UNKNOWN && ty_is_hash(pt)) {
2094+ const char *hn = ty_hash_cname(pt);
2095+ if (hn) buf_printf(out, "sp_%sHash_new()", hn);
2096+ else emit_expr(c, provided, out);
2097+ }
2098+ else emit_expr(c, provided, out);
2099+ }
20882100 }
20892101 return;
20902102 }
@@ -2110,7 +2122,19 @@
21102122 if (pt == TY_POLY_ARRAY) buf_puts(out, "sp_PolyArray_new()");
21112123 else { const char *k = array_kind(pt); if (k) buf_printf(out, "sp_%sArray_new()", k); else emit_expr(c, dv, out); }
21122124 }
2113- else emit_expr(c, dv, out);
2125+ /* Default empty `{}` literal: emit the correct hash constructor for the
2126+ parameter type, avoiding the "unsupported hash literal" fallback. */
2127+ else {
2128+ int dhn = 0;
2129+ int is_empty_hash_dv = dty && (!strcmp(dty, "HashNode") || !strcmp(dty, "KeywordHashNode")) &&
2130+ (nt_arr(c->nt, dv, "elements", &dhn), dhn == 0);
2131+ if (is_empty_hash_dv && ty_is_hash(pt)) {
2132+ const char *hn = ty_hash_cname(pt);
2133+ if (hn) buf_printf(out, "sp_%sHash_new()", hn);
2134+ else emit_expr(c, dv, out);
2135+ }
2136+ else emit_expr(c, dv, out);
2137+ }
21142138 }
21152139 }
21162140
@@ -9535,7 +9559,12 @@
95359559 if (!strcmp(ty, "HashNode") || !strcmp(ty, "KeywordHashNode")) {
95369560 TyKind ht = comp_ntype(c, id);
95379561 const char *hn = ty_hash_cname(ht);
9538- if (!hn) unsupported(c, id, "hash literal (key/value type)");
9562+ if (!hn) {
9563+ /* Empty `{}` with unknown type: fall back to StrPolyHash */
9564+ int ne2 = 0; nt_arr(nt, id, "elements", &ne2);
9565+ if (ne2 == 0) hn = "StrPoly";
9566+ else unsupported(c, id, "hash literal (key/value type)");
9567+ }
95399568 int n = 0;
95409569 const int *els = nt_arr(nt, id, "elements", &n);
95419570 int t = ++g_tmp;