Commit be113bd2

be113bd22ea2Merged

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

Description

codegen: fix poly↔typed ivar writes, hash []= init, and kwargs→hash param

Three codegen fixes:

  1. Ivar poly-to-typed unbox: when a poly-typed rhs (e.g. attrs[:id] || 0 from a SymPolyHash lookup) is assigned to a concrete-typed ivar (mrb_int), wrap the expression with emit_unbox_text instead of emitting raw. Covers both emit_expr (expression form) and compile_stmt (statement form).

  2. Hash []= poly-slot init: when into[k] = {} targets a poly-hash slot (decl_type == TY_POLY) and the rhs has no type yet (TY_UNKNOWN), use emit_boxed so the sp_RbVal temp is initialised from a boxed hash pointer rather than directly from a raw hash pointer (invalid initialiser).

  3. Implicit kwargs-as-hash param: C.new(debug: true) where initialize takes a single hash-typed optional param o = {} was erroneously looking for a keyword named "o" in the call-site KWH, failing, and falling back to the empty-hash default. Fix: when no positional arg and no keyword match, treat the entire KWH as the hash argument if the param is hash-typed and has a default.

Fixes ivar_assign_and_expr (626 pass, was 625).

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

Details

Commit
be113bd22ea2
Parent
3137b1c5a316

src/codegen.cmodifiedView file

@@ -2400,10 +2400,22 @@
24002400 } else {
24012401 /* Check if this param has a keyword match (lookup by param name in kwh). */
24022402 int kv = kwh >= 0 ? kwh_lookup(nt, kwh, m->pnames[i]) : -1;
2403- if (kv >= 0)
2403+ if (kv >= 0) {
24042404 emit_arg_or_default(c, m, i, kv, out);
2405- else
2406- emit_arg_or_default(c, m, i, i < pos_argc ? argv[i] : -1, out);
2405+ }
2406+ else if (i < pos_argc) {
2407+ emit_arg_or_default(c, m, i, argv[i], out);
2408+ }
2409+ else {
2410+ /* No positional arg and no keyword match. If the param is a hash-typed
2411+ optional (e.g. `def f(opts = {})`) and the call site passed a
2412+ KeywordHashNode (e.g. `f(key: val)`), treat the whole kwh as the
2413+ implicit hash argument rather than using the default. */
2414+ LocalVar *p = scope_local(m, m->pnames[i]);
2415+ TyKind pt = p ? p->type : TY_INT;
2416+ int use_kwh = (kwh >= 0 && ty_is_hash(pt) && m->pdefault[i] >= 0);
2417+ emit_arg_or_default(c, m, i, use_kwh ? kwh : -1, out);
2418+ }
24072419 }
24082420 }
24092421 }
@@ -7549,7 +7561,10 @@
75497561 TyKind decl_type = (is_poly_hash && vt != TY_UNKNOWN && vt != TY_POLY) ? vt : (vt != TY_UNKNOWN ? vt : TY_POLY);
75507562 emit_ctype(c, decl_type, b);
75517563 buf_printf(b, " _t%d = ", tv);
7552- emit_expr(c, argv[1], b);
7564+ /* When the slot is poly but the rhs has no type yet (e.g. `{}`),
7565+ emit a boxed value so the sp_RbVal temp initialises correctly. */
7566+ if (decl_type == TY_POLY) emit_boxed(c, argv[1], b);
7567+ else emit_expr(c, argv[1], b);
75537568 buf_printf(b, "; if (sp_gc_is_frozen("); emit_expr(c, recv, b); buf_puts(b, ")) sp_raise_frozen_hash(); ");
75547569 buf_printf(b, "sp_%sHash_set(", hn); emit_expr(c, recv, b); buf_puts(b, ", ");
75557570 if (rt == TY_POLY_POLY_HASH) emit_boxed(c, argv[0], b); else emit_expr(c, argv[0], b);
@@ -9175,6 +9190,13 @@
91759190 else emit_expr(c, v, b);
91769191 }
91779192 else if (ivt2 == TY_POLY && comp_ntype(c, v) != TY_POLY) emit_boxed(c, v, b);
9193+ else if (ivt2 != TY_POLY && ivt2 != TY_UNKNOWN && comp_ntype(c, v) == TY_POLY) {
9194+ /* poly rhs assigned to a typed ivar: unbox to the concrete type */
9195+ Buf _rb; memset(&_rb, 0, sizeof _rb);
9196+ emit_expr(c, v, &_rb);
9197+ emit_unbox_text(c, ivt2, _rb.p ? _rb.p : "sp_box_nil()", b);
9198+ free(_rb.p);
9199+ }
91789200 else emit_expr(c, v, b);
91799201 buf_printf(b, "; %s; })", ref2e);
91809202 return;
@@ -11423,6 +11445,13 @@
1142311445 /* a poly ivar slot needs a boxed RHS */
1142411446 emit_boxed(c, v, b);
1142511447 }
11448+ else if (ivt != TY_POLY && ivt != TY_UNKNOWN && comp_ntype(c, v) == TY_POLY) {
11449+ /* poly rhs assigned to a typed ivar: unbox to the concrete type */
11450+ Buf _rb; memset(&_rb, 0, sizeof _rb);
11451+ emit_expr(c, v, &_rb);
11452+ emit_unbox_text(c, ivt, _rb.p ? _rb.p : "sp_box_nil()", b);
11453+ free(_rb.p);
11454+ }
1142611455 else {
1142711456 emit_expr(c, v, b);
1142811457 }