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
if (_bn == 0) { const char *_rk = (res == TY_POLY_ARRAY) ? "Poly" : array_kind(res); buf_printf(b, "sp_%sArray_new()", _rk ? _rk : "Int"); } else emit_expr(c, nd, b); } else if (ty_is_hash(res) && _bty && (!strcmp(_bty, "HashNode") || !strcmp(_bty, "KeywordHashNode"))) { int _bn = 0; nt_arr(nt, nd, "elements", &_bn); const char *_hc = ty_hash_cname(res); if (_bn == 0 && _hc) buf_printf(b, "sp_%sHash_new()", _hc); else emit_expr(c, nd, b); } else emit_expr(c, nd, b); } buf_puts(b, " : "); { int nd = eb[0]; const char *_bty = nt_type(nt, nd); if (_bn == 0) { const char *_rk = (res == TY_POLY_ARRAY) ? "Poly" : array_kind(res); buf_printf(b, "sp_%sArray_new()", _rk ? _rk : "Int"); } else emit_expr(c, nd, b); } else if (ty_is_hash(res) && _bty && (!strcmp(_bty, "HashNode") || !strcmp(_bty, "KeywordHashNode"))) { int _bn = 0; nt_arr(nt, nd, "elements", &_bn); const char *_hc = ty_hash_cname(res); if (_bn == 0 && _hc) buf_printf(b, "sp_%sHash_new()", _hc); else emit_expr(c, nd, b); } else emit_expr(c, nd, b); } buf_puts(b, ")"); return;src/codegen_stmt.cmodifiedView file
Loading diff…
test/empty_hash_takes_return_type.rbaddedView file
Loading diff…
test/empty_hash_takes_return_type.rb.expectedaddedView file
Loading diff…