Commit 7d670aec

7d670aecfa07Merged

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

Description

codegen: an empty {} literal takes its hash variant from context

An empty {} literal has no keys to infer from, so it defaults to a StrPolyHash. When the surrounding context is a different hash variant -- a SymPolyHash-returning method whose other branch is { a: 1 }, or a str-keyed ternary -- the defaulted StrPolyHash* is an incompatible pointer type against the expected one (only surfaced under the harness's -Werror; otherwise a silent pointer-type warning).

Context-type the empty literal the same way an empty [] already is:

  • emit_tail_value: in a hash-returning tail, emit the return type's sp_<variant>Hash_new() instead of the StrPolyHash default;
  • the value-ternary path: an empty {} branch of a hash-typed ?: uses the result type's variant, mirroring the existing empty-array arm.

Found while reducing #1438 (a c ? {} : { a: 1 } method poisoned the -Werror test build); independent of the poly empty? dispatch.

Details

Commit
7d670aecfa07
Parent
b6a36c3c7876

src/codegen_expr.cmodifiedView file

@@ -1121,6 +1121,11 @@
11211121 if (_bn == 0) { const char *_rk = (res == TY_POLY_ARRAY) ? "Poly" : array_kind(res);
11221122 buf_printf(b, "sp_%sArray_new()", _rk ? _rk : "Int"); }
11231123 else emit_expr(c, nd, b);
1124+ }
1125+ else if (ty_is_hash(res) && _bty && (!strcmp(_bty, "HashNode") || !strcmp(_bty, "KeywordHashNode"))) {
1126+ int _bn = 0; nt_arr(nt, nd, "elements", &_bn); const char *_hc = ty_hash_cname(res);
1127+ if (_bn == 0 && _hc) buf_printf(b, "sp_%sHash_new()", _hc);
1128+ else emit_expr(c, nd, b);
11241129 } else emit_expr(c, nd, b); }
11251130 buf_puts(b, " : ");
11261131 { int nd = eb[0]; const char *_bty = nt_type(nt, nd);
@@ -1130,6 +1135,11 @@
11301135 if (_bn == 0) { const char *_rk = (res == TY_POLY_ARRAY) ? "Poly" : array_kind(res);
11311136 buf_printf(b, "sp_%sArray_new()", _rk ? _rk : "Int"); }
11321137 else emit_expr(c, nd, b);
1138+ }
1139+ else if (ty_is_hash(res) && _bty && (!strcmp(_bty, "HashNode") || !strcmp(_bty, "KeywordHashNode"))) {
1140+ int _bn = 0; nt_arr(nt, nd, "elements", &_bn); const char *_hc = ty_hash_cname(res);
1141+ if (_bn == 0 && _hc) buf_printf(b, "sp_%sHash_new()", _hc);
1142+ else emit_expr(c, nd, b);
11331143 } else emit_expr(c, nd, b); }
11341144 buf_puts(b, ")");
11351145 return;