Commit 77274f84

77274f842b45Merged

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

Description

fix(codegen,runtime): each_cons.with_index.map block emits poly_array under promote

The chain-fusion path in compile_each_cons_block was handling the common scalar-block cases (string / float / typed-array nesting / int fallback) but the bigint case fell to the int-fallback branch that wraps the value with sp_bigint_to_int -- which crashed when the compiled expr was actually a raw mrb_int (sp_int_add result).

Two-step fix:

codegen:

  • Block param destructure (|(a, b), i|) now declares each inner name + the with_index counter as sp_Bigint* in promote mode (matching the analyzer's scope_types widening), so the inner arithmetic dispatches via sp_bigint_* consistently.
  • The accumulator type splits poly_array / *_ptr_array out of is_array_type. Block returning a literal [bigint, bigint] caches as poly_array; the outer accumulator must be PolyArray boxing each inner poly_array via sp_box_poly_array. Block returning a bigint scalar likewise switches to PolyArray with sp_box_int(sp_bigint_to_int(...)).
  • Final IntArray push only sp_bigint_to_int-wraps when the compiled expr's textual prefix says it's actually an sp_Bigint * (sp_bigint_* / (sp_Bigint *)). The previous unconditional wrap on cached infer == bigint cast raw int to pointer.

runtime:

  • sp_poly_inspect now dispatches SP_BUILTIN_POLY_ARRAY via sp_PolyArray_inspect. Forward-declared since sp_PolyArray_inspect body lives below sp_poly_inspect.

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

Details

Commit
77274f842b45
Parent
0bdcf4eb9121

lib/sp_runtime.hmodifiedView file

@@ -1986,6 +1986,12 @@
19861986 static sp_PolyArray *sp_PolyArray_shuffle(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_dup(a); sp_PolyArray_shuffle_bang(b); return b; }
19871987 static sp_RbVal sp_PolyArray_sample(sp_PolyArray *a) { if (a->len <= 0) return sp_box_nil(); return a->data[(mrb_int)(rand()%a->len)]; }
19881988
1989+/* Forward decl: sp_poly_inspect dispatches into sp_PolyArray_inspect
1990+ for nested poly arrays (under promote, an `each_cons` chain's outer
1991+ accumulator boxes each inner poly_array element), but the
1992+ sp_PolyArray_inspect body lives a few lines below. */
1993+static const char *sp_PolyArray_inspect(sp_PolyArray *a);
1994+
19891995 /* Object#inspect for a tagged sp_RbVal. Dispatches on the runtime tag;
19901996 each branch reuses the matching primitive inspect helper. Falls back
19911997 to "#<Object>" for SP_TAG_OBJ because the runtime has no class-name
@@ -2012,6 +2018,7 @@
20122018 case SP_BUILTIN_STR_ARRAY: return sp_StrArray_inspect((sp_StrArray *)v.v.p);
20132019 case SP_BUILTIN_SYM_ARRAY: return sp_SymArray_inspect((sp_IntArray *)v.v.p);
20142020 case SP_BUILTIN_PTR_ARRAY: return sp_PtrArray_inspect((sp_PtrArray *)v.v.p);
2021+ case SP_BUILTIN_POLY_ARRAY: return sp_PolyArray_inspect((sp_PolyArray *)v.v.p);
20152022 case SP_BUILTIN_RANGE: return sp_Range_inspect((sp_Range *)v.v.p);
20162023 case SP_BUILTIN_TIME: return sp_Time_inspect((sp_Time *)v.v.p);
20172024 default: return SPL("#<Object>");