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

@@ -8085,14 +8085,25 @@
80858085 if (hn) {
80868086 int tv = ++g_tmp;
80878087 int is_poly_hash = (rt == TY_SYM_POLY_HASH || rt == TY_STR_POLY_HASH || rt == TY_POLY_POLY_HASH);
8088+ TyKind hvt = ty_hash_val(rt);
8089+ /* A poly value into a typed-value hash (e.g. a String? guarded non-nil
8090+ stored into a Hash[String, String]): unbox to the value type. */
8091+ int unbox_poly_val = (!is_poly_hash && vt == TY_POLY &&
8092+ (hvt == TY_STRING || hvt == TY_INT || hvt == TY_FLOAT));
80888093 buf_puts(b, "({ ");
80898094 /* For poly hashes with scalar values, store the scalar and box it for the hash call. */
8090- TyKind decl_type = (is_poly_hash && vt != TY_UNKNOWN && vt != TY_POLY) ? vt : (vt != TY_UNKNOWN ? vt : TY_POLY);
8095+ TyKind decl_type = unbox_poly_val ? hvt
8096+ : (is_poly_hash && vt != TY_UNKNOWN && vt != TY_POLY) ? vt
8097+ : (vt != TY_UNKNOWN ? vt : TY_POLY);
80918098 emit_ctype(c, decl_type, b);
80928099 buf_printf(b, " _t%d = ", tv);
80938100 /* When the slot is poly but the rhs has no type yet (e.g. `{}`),
80948101 emit a boxed value so the sp_RbVal temp initialises correctly. */
8095- if (decl_type == TY_POLY) emit_boxed(c, argv[1], b);
8102+ if (unbox_poly_val) {
8103+ const char *fn = hvt == TY_STRING ? "sp_poly_to_s" : hvt == TY_INT ? "sp_poly_to_i" : "sp_poly_to_f";
8104+ buf_printf(b, "%s(", fn); emit_expr(c, argv[1], b); buf_puts(b, ")");
8105+ }
8106+ else if (decl_type == TY_POLY) emit_boxed(c, argv[1], b);
80968107 else emit_expr(c, argv[1], b);
80978108 buf_printf(b, "; if (sp_gc_is_frozen("); emit_expr(c, recv, b); buf_puts(b, ")) sp_raise_frozen_hash(); ");
80988109 buf_printf(b, "sp_%sHash_set(", hn); emit_expr(c, recv, b); buf_puts(b, ", ");
@@ -8112,6 +8123,11 @@
81128123 if (decl_type == TY_POLY) buf_printf(b, "_t%d; })", tv);
81138124 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); }
81148125 }
8126+ else if (unbox_poly_val) {
8127+ /* value was poly (the `[]=` expression's value type); box the
8128+ unboxed temp back so the expression result stays poly. */
8129+ buf_puts(b, "); "); emit_boxed_text(c, hvt, tvn, b); buf_puts(b, "; })");
8130+ }
81158131 else {
81168132 buf_printf(b, "); _t%d; })", tv);
81178133 }
@@ -8679,7 +8695,16 @@
86798695 if (rt == TY_POLY_POLY_HASH) emit_boxed(c, argv[0], b); else emit_hash_key(c, argv[0], ty_hash_key(rt), b);
86808696 buf_puts(b, ", ");
86818697 if (rt == TY_SYM_POLY_HASH || rt == TY_STR_POLY_HASH || rt == TY_POLY_POLY_HASH) emit_boxed(c, argv[1], b);
8682- else emit_expr(c, argv[1], b);
8698+ else {
8699+ /* A poly value (holds the hash's value type at runtime, e.g. a String?
8700+ guarded non-nil) into a typed-value hash: coerce to its element
8701+ representation, as the typed-array `[]=` path does. */
8702+ TyKind hvt = ty_hash_val(rt), vt = comp_ntype(c, argv[1]);
8703+ if (vt == TY_POLY && hvt == TY_STRING) { buf_puts(b, "sp_poly_to_s("); emit_expr(c, argv[1], b); buf_puts(b, ")"); }
8704+ else if (vt == TY_POLY && hvt == TY_INT) { buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[1], b); buf_puts(b, ")"); }
8705+ else if (vt == TY_POLY && hvt == TY_FLOAT) { buf_puts(b, "sp_poly_to_f("); emit_expr(c, argv[1], b); buf_puts(b, ")"); }
8706+ else emit_expr(c, argv[1], b);
8707+ }
86838708 buf_puts(b, ");\n");
86848709 return 1;
86858710 }