Commit 12c49fd1
12c49fd1bbe5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Collect value-type objects from a map into a heap PtrArray
arr.map { Obj.new } over a typed-array (or range) receiver, where
Obj is a value-type-candidate class (no subclass, immutable scalar
ivars), failed two ways:
- typed-array receiver: build error. detect_value_types never saw
the map result as a ptr_array store, so
Objstayed value-typed andsp_Obj_new(...)returned a struct by value that couldn't be pushed into the object accumulator (incompatible type for argument 2 of sp_IntArray_push). - range receiver: silent wrong result. compile_map_expr's range arm had no object branch and bailed to a "0" placeholder, so the map produced an empty array.
#1056's fix (3984615) added the object-collection arm but only for heap classes; the value-type shape is the literal repro from #1056 and fell through unchanged.
Fix in two parts:
- analyze: detect_ptr_array_stored_types now treats a
mapwhose result type isobj_<C>_ptr_arrayas a ptr_array store of<C>, forcing<C>heap (same reasoning as an array literal of objects). With<C>heap, the existing typed-array obj arm handles it. - codegen: the range arm grows an object branch mirroring the typed-array path, collecting object pointers into a PtrArray.
Value-type classes never reached by such a map keep the stack-struct
optimization. Scoped to map because it is the only block-returning
enumerator whose codegen has the object-collection arm (flat_map /
filter_map route elsewhere and forcing heap would not fix them).
Full suite green (703) and optcarrot checksum unchanged (59662).
Fixes #1072 Refs #1056
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 12c49fd1bbe5
- Parent
- f6a6d35cbbd0
spinel_analyze.rbmodifiedView file
end end end # `arr.map { Foo.new }` over a typed-array/range receiver: the map # result is typed `obj_<C>_ptr_array` and compile_map_expr collects # the objects into a sp_PtrArray (its `_push` takes `void *`). A # value-type `<C>` would return a struct by value from `<C>.new` and # mismatch that void* slot, so keep it heap. Scoped to `map` because # that is the only block-returning enumerator whose codegen has the # object-collection arm. if mname == "map" map_rt = infer_type(nid) if is_ptr_array_type(map_rt) == 1 map_elem = ptr_array_elem_type(map_rt) if is_obj_type(map_elem) == 1 if not_in(map_elem, @ptr_array_stored_types) == 1 @ptr_array_stored_types.push(map_elem) end end end end end nid = nid + 1 endspinel_codegen.rbmodifiedView file
Loading diff…
test/map_to_value_type_objects.rbaddedView file
Loading diff…
test/map_to_value_type_objects.rb.expectedaddedView file
Loading diff…