Commit f45958d5

f45958d5a509Merged

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

Description

fix(codegen): sort_by + Array.new(N){} block-tail peek for bigint arith

Two infer_type(stmts.last) == "bigint" checks were too strict and missed common promote-mode shapes:

  • arr.sort_by { |x| -x } — -@ on a bigint local emits sp_bigint _sub, but infer_type's CallNode arm caches "int". Detect unary - / -@ and check recv.
  • Array.new(5) { |i| i * 2 } — bigint i arith mul emits bigint but cached "int". Detect arith CallNode and check operand types.

Promote-mode test count: 548 pass / 47 fail / 23 error -> 548 pass / 48 fail / 22 error. Default raise mode 618 / 0 / 0.

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

Details

Commit
f45958d5a509
Parent
95fdb5334a7f

spinel_codegen.rbmodifiedView file

@@ -16038,8 +16038,20 @@
1603816038 emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")
1603916039 else
1604016040 # promote-mode block tail may emit a bigint expr; unbox
16041- # for the int_array accumulator.
16042- if infer_type(arrnew_stmts2.last) == "bigint"
16041+ # for the int_array accumulator. Peek arith operands too —
16042+ # `Array.new(N) { i * 2 }` with bigint i caches "int" but
16043+ # emits sp_bigint_mul.
16044+ arrnew_tail_t = infer_type(arrnew_stmts2.last)
16045+ if arrnew_tail_t != "bigint" && @nd_type[arrnew_stmts2.last] == "CallNode"
16046+ an_mn = @nd_name[arrnew_stmts2.last]
16047+ if an_mn == "+" || an_mn == "-" || an_mn == "*" || an_mn == "/" || an_mn == "%" || an_mn == "**"
16048+ an_recv = @nd_receiver[arrnew_stmts2.last]
16049+ if an_recv >= 0 && base_type(infer_type(an_recv)) == "bigint"
16050+ arrnew_tail_t = "bigint"
16051+ end
16052+ end
16053+ end
16054+ if arrnew_tail_t == "bigint"
1604316055 @needs_bigint = 1
1604416056 arrnew_lastv = "sp_bigint_to_int((sp_Bigint *)" + arrnew_lastv + ")"
1604516057 end
@@ -18184,6 +18196,19 @@
1818418196 if bs.length > 0
1818518197 bexpr = compile_expr(bs.last)
1818618198 bexpr_t_sb = infer_type(bs.last)
18199+ # Peek: unary `-x` (CallNode `-` no args) on a bigint recv emits
18200+ # sp_bigint_sub. infer_type's CallNode arm caches it as "int" by
18201+ # default, so probe the recv side.
18202+ if bexpr_t_sb != "bigint" && @nd_type[bs.last] == "CallNode"
18203+ un_mn = @nd_name[bs.last]
18204+ un_args_sb = @nd_arguments[bs.last]
18205+ if (un_mn == "-" || un_mn == "-@") && (un_args_sb < 0 || get_args(un_args_sb).length == 0)
18206+ un_recv = @nd_receiver[bs.last]
18207+ if un_recv >= 0 && base_type(infer_type(un_recv)) == "bigint"
18208+ bexpr_t_sb = "bigint"
18209+ end
18210+ end
18211+ end
1818718212 end
1818818213 end
1818918214 ka_init_sb = bexpr