Commit 0146c4d1

0146c4d1a8edMerged

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

Description

fix(codegen): inlined each-yield body coerces inner / outer bp on bigint slot

compile_each_with_yield_inline (the path used when an inlined yield body's def each is itself @data.each do |x| yield x end) emitted:

  • lv_<inner_bp> = sp_IntArray_get(...) — int rhs into possibly- bigint inner bp slot.
  • lv_<outer_bp> = <yield_arg> — yield arg's static type may not match outer bp's promoted slot.

Probe find_var_type for both slots and route int <-> bigint through sp_bigint_new_int / sp_bigint_to_int as needed.

Also push a block-param scope inside body_yield_arg_types so the csig builder's infer_type on a yield x inside a nested @data. each do |x| sees the inner block param's type rather than the outer same-named (often promoted) local.

Promote-mode test count: 527 pass / 50 fail / 41 error -> 528 pass / 50 fail / 40 error. Default raise mode unchanged at 618 / 0 / 0.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
0146c4d1a8ed
Parent
0f1fdab035e5

spinel_codegen.rbmodifiedView file

@@ -8659,6 +8659,41 @@
86598659 if @nd_type[nid] == "DefNode"
86608660 return
86618661 end
8662+ # When descending into a CallNode that holds a block (each, map,
8663+ # etc.), the block's params shadow outer same-named locals.
8664+ # Without pushing them into scope, `yield x` inside the block sees
8665+ # outer x (often promoted to bigint) and infers wrongly. Push a
8666+ # frame with the block param types resolved against the iterator's
8667+ # element type so the yield arm's infer_type picks them up.
8668+ pushed_byat_scope = 0
8669+ if @nd_type[nid] == "CallNode" && @nd_block[nid] >= 0
8670+ blk_byat = @nd_block[nid]
8671+ bp_byat = @nd_parameters[blk_byat]
8672+ if bp_byat >= 0
8673+ inner_byat = @nd_parameters[bp_byat]
8674+ if inner_byat >= 0
8675+ reqs_byat = parse_id_list(@nd_requireds[inner_byat])
8676+ if reqs_byat.length > 0
8677+ recv_byat = @nd_receiver[nid]
8678+ recv_t_byat = recv_byat >= 0 ? infer_type(recv_byat) : ""
8679+ elem_t_byat = recv_t_byat == "" ? "int" : iter_elem_type(recv_t_byat)
8680+ if elem_t_byat == "" || elem_t_byat == "void"
8681+ elem_t_byat = "int"
8682+ end
8683+ push_scope
8684+ pushed_byat_scope = 1
8685+ bpi_byat = 0
8686+ while bpi_byat < reqs_byat.length
8687+ bn_byat = @nd_name[reqs_byat[bpi_byat]]
8688+ if bn_byat != "" && bn_byat != "_"
8689+ declare_var(bn_byat, elem_t_byat)
8690+ end
8691+ bpi_byat = bpi_byat + 1
8692+ end
8693+ end
8694+ end
8695+ end
8696+ end
86628697 cs = []
86638698 push_child_ids(nid, cs)
86648699 k = 0
@@ -8666,6 +8701,9 @@
86668701 body_yield_arg_types(cs[k], types)
86678702 k = k + 1
86688703 end
8704+ if pushed_byat_scope == 1
8705+ pop_scope
8706+ end
86698707 end
86708708
86718709 def cls_method_has_yield(ci, midx)
@@ -35482,7 +35520,13 @@
3548235520
3548335521 if rt == "int_array"
3548435522 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_IntArray_length(" + recv_expr + "); " + tmp + "++) {")
35485- emit(" lv_" + inner_bp_remapped + " = sp_IntArray_get(" + recv_expr + ", " + tmp + ");")
35523+ inner_bp_t_eyi = find_var_type(inner_bp_remapped)
35524+ if inner_bp_t_eyi == "bigint"
35525+ @needs_bigint = 1
35526+ emit(" lv_" + inner_bp_remapped + " = sp_bigint_new_int(sp_IntArray_get(" + recv_expr + ", " + tmp + "));")
35527+ else
35528+ emit(" lv_" + inner_bp_remapped + " = sp_IntArray_get(" + recv_expr + ", " + tmp + ");")
35529+ end
3548635530 @indent = @indent + 1
3548735531 redo_label = push_redo_label
3548835532 emit_redo_label(redo_label)
@@ -35504,7 +35548,17 @@
3550435548 yk = 0
3550535549 while yk < yaids.length
3550635550 if yk < outer_bp_names.length
35507- emit(" lv_" + outer_bp_names[yk] + " = " + compile_expr_remap(yaids[yk], map_from, map_to) + ";")
35551+ yarg_v_eyi = compile_expr_remap(yaids[yk], map_from, map_to)
35552+ yarg_t_eyi = infer_type(yaids[yk])
35553+ outer_bp_t_eyi = find_var_type(outer_bp_names[yk])
35554+ if outer_bp_t_eyi == "bigint" && yarg_t_eyi == "int"
35555+ @needs_bigint = 1
35556+ yarg_v_eyi = "sp_bigint_new_int(" + yarg_v_eyi + ")"
35557+ elsif outer_bp_t_eyi == "int" && yarg_t_eyi == "bigint"
35558+ @needs_bigint = 1
35559+ yarg_v_eyi = "sp_bigint_to_int((sp_Bigint *)" + yarg_v_eyi + ")"
35560+ end
35561+ emit(" lv_" + outer_bp_names[yk] + " = " + yarg_v_eyi + ";")
3550835562 end
3550935563 yk = yk + 1
3551035564 end