Commit e919dec1
e919dec18869Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: coerce a poly value into a typed-value hash setter
Storing a poly value into a typed-value hash -- Hash[String, String] /
Hash[String, Integer] -- passed the raw sp_RbVal to a setter whose value
parameter is const char * / mrb_int, so the generated C failed to compile.
This surfaces when an ivar/value the type system widened to poly (e.g. a
String? guarded non-nil) is written into a hash whose value type is pinned
concrete (by an RBS return/param seed, or a method-return hash that can't
re-widen).
Coerce the poly to the hash's value representation (sp_poly_to_s / _to_i /
_to_f) at both the statement and expression []= setters, mirroring what the
typed-array []= path already does. The value genuinely holds that type at
runtime (the program would otherwise be ill-typed), so the unbox is sound; the
expression form boxes the result back to keep the []= expression's poly type.
Refs #1417
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- e919dec18869
- Parent
- ef0074272cc5
src/codegen_call.cmodifiedView file
if (hn) { int tv = ++g_tmp; int is_poly_hash = (rt == TY_SYM_POLY_HASH || rt == TY_STR_POLY_HASH || rt == TY_POLY_POLY_HASH); TyKind hvt = ty_hash_val(rt); /* A poly value into a typed-value hash (e.g. a String? guarded non-nil stored into a Hash[String, String]): unbox to the value type. */ int unbox_poly_val = (!is_poly_hash && vt == TY_POLY && (hvt == TY_STRING || hvt == TY_INT || hvt == TY_FLOAT)); buf_puts(b, "({ "); /* For poly hashes with scalar values, store the scalar and box it for the hash call. */ TyKind decl_type = (is_poly_hash && vt != TY_UNKNOWN && vt != TY_POLY) ? vt : (vt != TY_UNKNOWN ? vt : TY_POLY); TyKind decl_type = unbox_poly_val ? hvt : (is_poly_hash && vt != TY_UNKNOWN && vt != TY_POLY) ? vt : (vt != TY_UNKNOWN ? vt : TY_POLY); emit_ctype(c, decl_type, b); buf_printf(b, " _t%d = ", tv); /* When the slot is poly but the rhs has no type yet (e.g. `{}`), emit a boxed value so the sp_RbVal temp initialises correctly. */ if (decl_type == TY_POLY) emit_boxed(c, argv[1], b); if (unbox_poly_val) { const char *fn = hvt == TY_STRING ? "sp_poly_to_s" : hvt == TY_INT ? "sp_poly_to_i" : "sp_poly_to_f"; buf_printf(b, "%s(", fn); emit_expr(c, argv[1], b); buf_puts(b, ")"); } else if (decl_type == TY_POLY) emit_boxed(c, argv[1], b); else emit_expr(c, argv[1], b); buf_printf(b, "; if (sp_gc_is_frozen("); emit_expr(c, recv, b); buf_puts(b, ")) sp_raise_frozen_hash(); "); buf_printf(b, "sp_%sHash_set(", hn); emit_expr(c, recv, b); buf_puts(b, ", "); if (decl_type == TY_POLY) buf_printf(b, "_t%d; })", tv); else { Buf _bx; memset(&_bx, 0, sizeof _bx); emit_boxed_text(c, decl_type, tvn, &_bx); buf_printf(b, "%s; })", _bx.p ? _bx.p : tvn); free(_bx.p); } } else if (unbox_poly_val) { /* value was poly (the `[]=` expression's value type); box the unboxed temp back so the expression result stays poly. */ buf_puts(b, "); "); emit_boxed_text(c, hvt, tvn, b); buf_puts(b, "; })"); } else { buf_printf(b, "); _t%d; })", tv); } if (rt == TY_POLY_POLY_HASH) emit_boxed(c, argv[0], b); else emit_hash_key(c, argv[0], ty_hash_key(rt), b); buf_puts(b, ", "); if (rt == TY_SYM_POLY_HASH || rt == TY_STR_POLY_HASH || rt == TY_POLY_POLY_HASH) emit_boxed(c, argv[1], b); else emit_expr(c, argv[1], b); else { /* A poly value (holds the hash's value type at runtime, e.g. a String? guarded non-nil) into a typed-value hash: coerce to its element representation, as the typed-array `[]=` path does. */ TyKind hvt = ty_hash_val(rt), vt = comp_ntype(c, argv[1]); if (vt == TY_POLY && hvt == TY_STRING) { buf_puts(b, "sp_poly_to_s("); emit_expr(c, argv[1], b); buf_puts(b, ")"); } else if (vt == TY_POLY && hvt == TY_INT) { buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[1], b); buf_puts(b, ")"); } else if (vt == TY_POLY && hvt == TY_FLOAT) { buf_puts(b, "sp_poly_to_f("); emit_expr(c, argv[1], b); buf_puts(b, ")"); } else emit_expr(c, argv[1], b); } buf_puts(b, ");\n"); return 1; }test/hash_str_value_poly_coerce.rbaddedView file
Loading diff…
test/hash_str_value_poly_coerce.rb.expectedaddedView file
Loading diff…