Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,7 @@ namespace WinRT.{{escapedAssemblyName}}GenericHelpers
private static bool NeedVtableOnLookupTable(SyntaxNode node)
{
return (node is InvocationExpressionSyntax invocation && invocation.ArgumentList.Arguments.Count != 0) ||
(node is BaseObjectCreationExpressionSyntax objectCreation && objectCreation.ArgumentList?.Arguments.Count > 0) ||
node is AssignmentExpressionSyntax ||
node is VariableDeclarationSyntax ||
node is PropertyDeclarationSyntax ||
Expand Down Expand Up @@ -1252,6 +1253,33 @@ private static EquatableArray<VtableAttribute> GetVtableAttributesToAddOnLookupT
}
}
}
// Same as the InvocationExpressionSyntax case above, but for object creation expressions
// (i.e., constructor calls like 'new WinRTType(args)'). Without this, when a managed type
// (e.g. List<KeyValuePair<string, string>>) is passed as an argument to a WinRT constructor,
// the source generator would not detect it and would not generate the necessary CCW vtable entries.
else if (context.Node is BaseObjectCreationExpressionSyntax objectCreation &&
objectCreation.ArgumentList is { Arguments.Count: > 0 })
{
var creationSymbol = context.SemanticModel.GetSymbolInfo(objectCreation).Symbol;
if (creationSymbol is IMethodSymbol methodSymbol &&
(isWinRTClassOrInterface(methodSymbol.ContainingSymbol, true) ||
SymbolEqualityComparer.Default.Equals(methodSymbol.ContainingAssembly, context.SemanticModel.Compilation.Assembly)))
{
for (int idx = 0, paramsIdx = 0; idx < objectCreation.ArgumentList.Arguments.Count; idx++)
{
if (methodSymbol.Parameters[paramsIdx].RefKind != RefKind.Out)
{
var argumentType = context.SemanticModel.GetTypeInfo(objectCreation.ArgumentList.Arguments[idx].Expression);
AddVtableAttributesForType(argumentType, methodSymbol.Parameters[paramsIdx].Type);
}

if (!methodSymbol.Parameters[paramsIdx].IsParams)
{
paramsIdx++;
}
}
}
}
else if (context.Node is AssignmentExpressionSyntax assignment)
{
var isGeneratedBindableCustomPropertyClass = false;
Expand Down
32 changes: 32 additions & 0 deletions src/Tests/FunctionalTests/Collections/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using test_component_derived.Nested;
using TestComponentCSharp;
using Windows.Foundation;
using Windows.Web.Http;

var instance = new Class();

Expand Down Expand Up @@ -351,6 +352,37 @@
return 101;
}

// Regression test for https://github.com/microsoft/CsWinRT/issues/2337:
// passing a generic managed collection to a WinRT constructor should
// generate the necessary CCW vtable entries even when the local is 'var'.
sum = 0;

var intLinkedList = new LinkedList<int>();
intLinkedList.AddLast(3);
intLinkedList.AddLast(5);
intLinkedList.AddLast(7);
var customIterableTest4 = new CustomIterableTest(intLinkedList);
foreach (var i in customIterableTest4)
{
sum += i;
}

if (sum != 15)
{
return 101;
}

// Exact regression test for https://github.com/microsoft/CsWinRT/issues/2337:
// HttpFormUrlEncodedContent takes IIterable<IKeyValuePair<String, String>> in
// its constructor. Passing a List<KeyValuePair<string, string>> declared with
// 'var' must generate the required CCW vtable entries for the AOT scenario.
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("aaa", "1234"),
new KeyValuePair<string, string>("bbb", "5678")
};
using var bodyContent = new HttpFormUrlEncodedContent(pairs);

var nullableDoubleList = new List<double?>() { 1, 2, null, 3, 4, null};
var result = instance.Calculate(nullableDoubleList);
if (result != 10)
Expand Down