Skip to content

Add tests for collection expressions targeting read-only collections#2478

Open
Sergio0694 wants to merge 2 commits into
staging/3.0from
user/sergiopedri/collection-expr-readonly-aot-tests
Open

Add tests for collection expressions targeting read-only collections#2478
Sergio0694 wants to merge 2 commits into
staging/3.0from
user/sergiopedri/collection-expr-readonly-aot-tests

Conversation

@Sergio0694

Copy link
Copy Markdown
Member

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 newobj in 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 implement IEnumerable<T>, IReadOnlyList<T>, and IList<T>, the tests also assert that the CCW exposes IIterable<T>, IVectorView<T>, and IVector<T> (not just IIterable<T>), for both the multi-element and single-element backing types.

Changes

  • src/Tests/UnitTest/TestComponentCSharp_Tests.cs: new TestCollectionExpressionReadOnlyInterfaceMarshalling test. Marshals collection expressions typed as each read-only interface (multi-element and single-element) through Class.GetIteratorForCollection (native IIterable<T> consumption) and BindableIterableProperty (bindable round-trip), and uses ConvertToUnmanaged plus Marshal.QueryInterface to assert the synthesized type's CCW exposes IIterable<int>, IVectorView<int>, and IVector<int>.
  • src/Tests/UnitTest/TestComponent_Tests.cs: new Collections_ReadOnly_List_Call_CollectionExpression test. Returns IReadOnlyList<string> collection expressions from Collection6Call so the synthesized backing type is marshalled back to native as a CCW exposing IVectorView<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 a QueryInterface-based check that the synthesized type's CCW exposes IIterable<int>, IVectorView<int>, and IVector<int>.

@Sergio0694 Sergio0694 added testing Related to testing infrastructure trimming AOT CsWinRT 3.0 labels Jun 30, 2026
@Sergio0694 Sergio0694 requested a review from manodasanW June 30, 2026 22:15
Sergio0694 and others added 2 commits June 30, 2026 15:25
…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>
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/collection-expr-readonly-aot-tests branch from a1e154c to 6faef2f Compare June 30, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AOT CsWinRT 3.0 testing Related to testing infrastructure trimming

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant