Skip to content

Fix CCW detection for constructor parameters - #2339

Merged
manodasanW merged 2 commits into
masterfrom
fix/ccw-constructor-args-2337
Mar 17, 2026
Merged

Fix CCW detection for constructor parameters#2339
manodasanW merged 2 commits into
masterfrom
fix/ccw-constructor-args-2337

Conversation

@Sergio0694

Copy link
Copy Markdown
Member

Fixes #2337. We were not analyzing arguments to constructors specifically.

…r CCW generation

The AOT source generator's lookup table code path only handled
InvocationExpressionSyntax (regular method calls) when analyzing arguments
to detect managed types that need CCW vtable entries. It did not handle
BaseObjectCreationExpressionSyntax (constructor calls like 'new WinRTType(args)').

This meant that when a managed generic collection type (e.g.
List<KeyValuePair<string, string>>) was passed as an argument to a WinRT
constructor (e.g. new HttpFormUrlEncodedContent(pairs)), the source generator
would not detect it and would not generate the necessary CCW entries. At
runtime, this caused a 'Failed to create a CCW' error.

The fix adds BaseObjectCreationExpressionSyntax handling to both
NeedVtableOnLookupTable (syntactic filter) and
GetVtableAttributesToAddOnLookupTable (semantic analysis), mirroring the
existing InvocationExpressionSyntax logic.

Fixes #2337

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #2337 by updating the WinRT AOT source generator’s CCW/vtable detection logic to also analyze arguments passed to constructors (object creation expressions), not just regular method invocations. This ensures generic managed collections passed into WinRT constructors trigger generation of the required CCW vtable entries.

Changes:

  • Extend lookup-table detection to include constructor argument analysis (new SomeWinRTType(arg)).
  • Add regression coverage in functional collections tests to validate constructor-argument CCW detection.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Tests/FunctionalTests/Collections/Program.cs Adds a regression test that passes a generic managed collection into a WinRT runtimeclass constructor and validates enumeration works.
src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs Updates the AOT optimizer to treat object creation expressions with arguments like invocations and extract argument types for vtable attribute generation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…tors

Adds two regression tests for #2337 that exercise the new
BaseObjectCreationExpressionSyntax handling in the AOT source generator:

1. LinkedList<int> (declared with 'var') passed to CustomIterableTest
   constructor - uses a type not used elsewhere in the test, ensuring
   the CCW generation is only triggered through the constructor argument
   analysis path.

2. List<KeyValuePair<string, string>> (declared with 'var') passed to
   HttpFormUrlEncodedContent constructor - exact repro from the issue,
   matching the reported scenario.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Sergio0694
Sergio0694 force-pushed the fix/ccw-constructor-args-2337 branch from 59dd6c8 to ea4cc97 Compare March 17, 2026 00:04
@manodasanW
manodasanW merged commit d7bcbb0 into master Mar 17, 2026
13 checks passed
@manodasanW
manodasanW deleted the fix/ccw-constructor-args-2337 branch March 17, 2026 02:41
manodasanW added a commit that referenced this pull request Apr 30, 2026
Port of test additions from master commit d7bcbb0. While the 2.x source
fix (AotOptimizer.cs) doesn't apply to 3.0 (the interop generator does
whole-app IL analysis), these regression tests validate that generic
managed collections passed to WinRT constructors get proper CCW
marshalling support:

1. LinkedList<int> passed to CustomIterableTest constructor
2. List<KeyValuePair<string, string>> passed to HttpFormUrlEncodedContent

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Failed to create a CCW with List of KeyValuePair<string,string> for HttpFormUrlEncodedContent .ctor

3 participants