Add tests for collection expressions targeting read-only collections#2478
Open
Sergio0694 wants to merge 2 commits into
Open
Add tests for collection expressions targeting read-only collections#2478Sergio0694 wants to merge 2 commits into
Sergio0694 wants to merge 2 commits into
Conversation
…ion interfaces Collection expressions targeting a read-only collection interface (IEnumerable<T>, IReadOnlyList<T>, IReadOnlyCollection<T>) lower to compiler synthesized backing types (<>z__ReadOnlyArray<T> and <>z__ReadOnlySingleElementList<T>) that are marshalled across the WinRT ABI as CCWs. Add CoreCLR unit tests covering these scenarios, validating that the CCW for the synthesized type exposes IIterable<T>, IVectorView<T>, and IVector<T> (not just IIterable<T>), for both the multi-element and single-element backing types. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…lection interfaces Mirror the collection-expression read-only collection interface scenarios in the Collections functional test so the interop generator's CCW discovery for the compiler synthesized backing types (<>z__ReadOnlyArray<T> and <>z__ReadOnlySingleElementList<T>) is exercised under trimming and Native AOT, including validating that the synthesized type's CCW exposes IIterable<T>, IVectorView<T>, and IVector<T>. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
a1e154c to
6faef2f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Add CoreCLR unit tests and trim/AOT functional tests covering collection expressions whose target type is a read-only collection interface (
IEnumerable<T>,IReadOnlyList<T>,IReadOnlyCollection<T>), and validate that they marshal correctly across the WinRT ABI.Motivation
Collection expressions targeting a read-only collection interface are lowered by the C# compiler to internal synthesized backing types:
<>z__ReadOnlyArray<T>for multiple elements and<>z__ReadOnlySingleElementList<T>for a single element. When such an object crosses the WinRT ABI it needs a COM Callable Wrapper (CCW).In CsWinRT 2.x the source generator could not see these compiler-synthesized types (they are produced during lowering, after generators run), so marshalling them emitted AOT warnings and fell back to runtime vtable generation, which is not Native AOT safe. In CsWinRT 3.0 the interop generator discovers them directly from IL (for example, the
newobjin the lowered collection expression) and generates their CCWs ahead of time, so they are fully trim and AOT safe.There was no existing test coverage for this scenario anywhere in the suite (existing collection-expression usages only ever target concrete types such as
List<T>or arrays). This PR adds regression coverage for both normal CoreCLR execution and trimmed/Native AOT publishing. Because both synthesized types implementIEnumerable<T>,IReadOnlyList<T>, andIList<T>, the tests also assert that the CCW exposesIIterable<T>,IVectorView<T>, andIVector<T>(not justIIterable<T>), for both the multi-element and single-element backing types.Changes
src/Tests/UnitTest/TestComponentCSharp_Tests.cs: newTestCollectionExpressionReadOnlyInterfaceMarshallingtest. Marshals collection expressions typed as each read-only interface (multi-element and single-element) throughClass.GetIteratorForCollection(nativeIIterable<T>consumption) andBindableIterableProperty(bindable round-trip), and usesConvertToUnmanagedplusMarshal.QueryInterfaceto assert the synthesized type's CCW exposesIIterable<int>,IVectorView<int>, andIVector<int>.src/Tests/UnitTest/TestComponent_Tests.cs: newCollections_ReadOnly_List_Call_CollectionExpressiontest. ReturnsIReadOnlyList<string>collection expressions fromCollection6Callso the synthesized backing type is marshalled back to native as a CCW exposingIVectorView<string>.src/Tests/FunctionalTests/Collections/Program.cs: mirrors the above scenarios so the interop generator's CCW discovery for the synthesized backing types is exercised under trimming and Native AOT, including aQueryInterface-based check that the synthesized type's CCW exposesIIterable<int>,IVectorView<int>, andIVector<int>.