Commit f92cfe82

f92cfe82e654Merged

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

Description

fix(analyze,codegen): promote-mode int/bigint compatibility in array element infer

Three layered fixes for (0...3).map { |i| [i, i * 10] } under promote:

analyze:

  • post-promote @nd_inferred_type sweep now also widens an arith / bitop CallNode whose recv is bigint (no obj_X required). The earlier sweep only re-resolved obj-recv methods, leaving i * 10 cached at the pre-promote "int".

codegen:

  • The codegen-side infer_array_elem_type_from_ids mirror gets the same int<>bigint tolerance as the analyzer's: when one element surfaces as bigint (LV with widened scope_type) and a sibling surfaces as int (stale-int cache on arith), don't widen to poly_array. The compile_array_literal_from_ids int_array arm already unboxes per-element bigints via sp_bigint_to_int.

array #take / #drop:

  • Inline loop count param goes through compile_arg0_as_int so a promote-mode bigint count operand is unboxed before being used as the mrb_int loop bound.

destructure for .each { |(a, b)| ... } on int_array_ptr_array:

  • Numbered-param slots _1, _2, ... now declare as bigint in promote mode and emit sp_bigint_new_int boxing per element so the block body's arithmetic dispatches via sp_bigint_* consistently with the slot's scope type.

Picks up ptr_array, ptr_array_null_guards, map_range_recv_array_block, each_cons_with_index_map_chain_fusion. Three failures left: gc_root_ptr_array_literal (GC pressure under bigint), bundle_misc_b (_2.nil? ternary), each_cons_with_index_map_no_double_gets (top-level LV slot reinfer).

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

Details

Commit
f92cfe82e654
Parent
99731a4b9a34

spinel_analyze.rbmodifiedView file

@@ -2558,7 +2558,16 @@
25582558 while k < elems.length
25592559 et2 = infer_type(elems[k])
25602560 if et2 != et
2561- return "poly_array"
2561+ # Under promote mode, int / bigint are numerically interchangeable
2562+ # (the literal codegen unboxes via sp_bigint_to_int for IntArray
2563+ # storage). Stale-int cache on an arith CallNode whose actual
2564+ # value is bigint causes a spurious mismatch here -- recognize
2565+ # int<>bigint as compatible.
2566+ if @int_overflow_mode == "promote" && (et == "int" || et == "bigint") && (et2 == "int" || et2 == "bigint")
2567+ # compatible; continue
2568+ else
2569+ return "poly_array"
2570+ end
25622571 end
25632572 k = k + 1
25642573 end
@@ -21909,6 +21918,14 @@
2190921918 @nd_inferred_type[ni_p] = "bigint"
2191021919 end
2191121920 end
21921+ # Arith/bitop on bigint recv produces bigint. The cache was
21922+ # filled before promote_int_to_bigint_globally widened the
21923+ # recv's scope_type, so the call's cached return is stale "int".
21924+ elsif rcv_t_pn == "bigint"
21925+ mn_pn = @nd_name[ni_p]
21926+ if mn_pn == "+" || mn_pn == "-" || mn_pn == "*" || mn_pn == "/" || mn_pn == "%" || mn_pn == "**" || mn_pn == "&" || mn_pn == "|" || mn_pn == "^" || mn_pn == "<<" || mn_pn == ">>" || mn_pn == "~" || mn_pn == "-@"
21927+ @nd_inferred_type[ni_p] = "bigint"
21928+ end
2191221929 end
2191321930 end
2191421931 end