🍒[cxx-interop][Runtime] Initialize metadata of a Swift array of C++ references correctly #73671
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Explanation: This fixes a crash at runtime when destroying a Swift array of values of a C++ foreign reference type. Swift optimizes the amount of metadata emitted for
_ContiguousArrayStorage<Element>
by reusing_ContiguousArrayStorage<AnyObject>
whenever possible (seegetContiguousArrayStorageType
). However, C++ foreign reference types are notAnyObject
s, since they have custom retain/release operations. This change disables the_ContiguousArrayStorage
metadata optimization for C++ reference types, which makes sure thatswift_arrayDestroy
will call the correct release operation for elements of[MyCxxRefType]
.Scope: Changes the runtime handling of C++ foreign reference types – they will no longer be treated as AnyObjects by the runtime. Also changes the SIL logic that handles the
_ContiguousArrayStorage<AnyObject>
optimization.Risk: Low, only affects C++ foreign reference types.
Testing: Added a compile test.
Issue: rdar://127154770 / #72827
Reviewer: @mikeash @aschwaighofer @zoecarver
Original PR: #73615