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 Obj stayed value-typed and sp_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 map whose result type is obj_<C>_ptr_array as 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

@@ -26336,6 +26336,24 @@
2633626336 end
2633726337 end
2633826338 end
26339+ # `arr.map { Foo.new }` over a typed-array/range receiver: the map
26340+ # result is typed `obj_<C>_ptr_array` and compile_map_expr collects
26341+ # the objects into a sp_PtrArray (its `_push` takes `void *`). A
26342+ # value-type `<C>` would return a struct by value from `<C>.new` and
26343+ # mismatch that void* slot, so keep it heap. Scoped to `map` because
26344+ # that is the only block-returning enumerator whose codegen has the
26345+ # object-collection arm.
26346+ if mname == "map"
26347+ map_rt = infer_type(nid)
26348+ if is_ptr_array_type(map_rt) == 1
26349+ map_elem = ptr_array_elem_type(map_rt)
26350+ if is_obj_type(map_elem) == 1
26351+ if not_in(map_elem, @ptr_array_stored_types) == 1
26352+ @ptr_array_stored_types.push(map_elem)
26353+ end
26354+ end
26355+ end
26356+ end
2633926357 end
2634026358 nid = nid + 1
2634126359 end