Commit b726681b

b726681b7180Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · +1 added · ~1 modified

Description

Collect a poly-scalar map block into a PolyArray (object-array receiver)

arr.map { |o| o.poly_field } over an array of objects, where the block returns a polymorphic field (sp_RbVal), built a sp_PtrArray accumulator and pushed the element through (void *)(sp_RbVal) — a hard C error — while the analyzer typed the result poly_array, so the result var was a mismatched sp_PolyArray *.

Add a poly arm to the ptr_array-receiver map: when the block returns a poly scalar, accumulate into a sp_PolyArray and push the sp_RbVal directly (no cast, no re-box). Surfaced in roundhouse's real-blog results.map { |a| a.id } where Article#id widens to poly.

Closes #1076

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

Details

Commit
b726681b7180
Parent
1c1f48a59e16

spinel_codegen.rbmodifiedView file

@@ -44267,8 +44267,16 @@
4426744267 @needs_bigint = 1
4426844268 emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
4426944269 p_container = "poly_array"
44270+ elsif block_ret_p == "poly"
44271+ # Block returns a poly scalar (sp_RbVal), e.g. `arr.map { |o|
44272+ # o.poly_field }`. analyze types the result poly_array, so the
44273+ # accumulator must be a PolyArray; a PtrArray would push the
44274+ # sp_RbVal through `(void *)` -- a hard C error. Issue #1076.
44275+ @needs_rb_value = 1
44276+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
44277+ p_container = "poly_array"
4427044278 else
44271- # obj/ptr/poly block return: keep a PtrArray of the result.
44279+ # obj/ptr block return: keep a PtrArray of the result.
4427244280 emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
4427344281 p_container = "ptr_array"
4427444282 end
@@ -44299,6 +44307,10 @@
4429944307 elsif block_ret_p == "bigint"
4430044308 @needs_bigint = 1
4430144309 emit(" sp_PolyArray_push(" + tmp_arr + ", sp_box_int(sp_bigint_to_int((sp_Bigint *)" + lastv_p + ")));")
44310+ elsif block_ret_p == "poly"
44311+ # lastv_p is already an sp_RbVal; push it straight into the
44312+ # PolyArray (no (void *) cast, no re-box). Issue #1076.
44313+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + lastv_p + ");")
4430244314 else
4430344315 emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + lastv_p + "));")
4430444316 end