Commit 8b834b63

8b834b6364b4Merged

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

Description

Collect heap objects from a typed-array map into a PtrArray

arr.map { Obj.new } over a typed-array (or range) receiver build-errored: compile_map_expr's accumulator switch handled string/float/typed-array/poly-array block returns but not an object-typed return, so it fell to the IntArray default and the sp_* element push failed to compile (incompatible type for argument 2 of sp_IntArray_push). Analyze already types such a map obj_<C>_ptr_array.

Add an is_obj_type(block_ret) arm to the int_array-recv branch's accumulator and push: collect into a PtrArray of the object pointers. Strictly additive -- it only fires for a case that errored before, so no compiling program (optcarrot/bootstrap included) exercised it.

Scoped to heap (non-value-type) classes: a value-type class's constructor returns a struct by value, not a pointer, so it can't go in a void* PtrArray -- value-type-in-map needs value-type-collection support and falls through unchanged.

Fixes #1056

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
8b834b6364b4
Parent
72c292271321

spinel_codegen.rbmodifiedView file

@@ -43043,10 +43043,18 @@
4304343043 # so the cls_id chain stays tagged for `arr[i][j][k]...`.
4304443044 ret_is_arr2 = (block_ret == "int_array" || block_ret == "float_array" || block_ret == "str_array" || block_ret == "sym_array")
4304543045 ret_is_deep_arr2 = (is_ptr_array_type(block_ret) == 1 || block_ret == "poly_array")
43046+ # Block returns a heap user object (`arr.map { Obj.new }`): collect
43047+ # into a PtrArray of the object pointers (analyze types the result
43048+ # `obj_<C>_ptr_array`). Without this the accumulator defaulted to
43049+ # IntArray and the sp_<C>* push failed to compile. Value-type
43050+ # classes are excluded -- their constructor returns a struct by
43051+ # value, not a pointer, so they need value-type-collection support
43052+ # (separate work); they fall through unchanged.
43053+ ret_is_obj2 = (is_obj_type(block_ret) == 1 && is_value_type_class(block_ret[4, block_ret.length - 4]) == 0)
4304643054 if ret_is_deep_arr2
4304743055 @needs_rb_value = 1
4304843056 emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
43049- elsif ret_is_arr2
43057+ elsif ret_is_arr2 || ret_is_obj2
4305043058 @needs_ptr_array = 1
4305143059 emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
4305243060 else
@@ -43088,13 +43096,13 @@
4308843096 else
4308943097 emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret, val) + ");")
4309043098 end
43091- elsif ret_is_arr2
43099+ elsif ret_is_arr2 || ret_is_obj2
4309243100 emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + val + "));")
4309343101 else
4309443102 emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
4309543103 end
4309643104 else
43097- emit_map_default_push(tmp_arr, ret_is_arr2 ? "ptr_array" : "int_array")
43105+ emit_map_default_push(tmp_arr, (ret_is_arr2 || ret_is_obj2) ? "ptr_array" : "int_array")
4309843106 end
4309943107 end
4310043108 @indent = @indent - 1