Skip to content
Open
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
14 changes: 11 additions & 3 deletions src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,21 +1319,25 @@ private static EquatableArray<VtableAttribute> GetVtableAttributesToAddOnLookupT
}
else if (context.Node is PropertyDeclarationSyntax propertyDeclaration)
{
var isGeneratedBindableCustomPropertyClass =
context.SemanticModel.GetDeclaredSymbol(propertyDeclaration) is IPropertySymbol propertySymbol &&
GeneratorHelper.IsGeneratedBindableCustomPropertyClass(context.SemanticModel.Compilation, propertySymbol.ContainingSymbol);

// Detect scenarios where the property declaration has an initializer and is to a boxed or cast type during initialization.
if (propertyDeclaration.Initializer != null)
{
var leftSymbol = context.SemanticModel.GetSymbolInfo(propertyDeclaration.Type).Symbol;
if (leftSymbol is INamedTypeSymbol namedType)
{
AddVtableAttributesForExpression(propertyDeclaration.Initializer.Value, namedType);
AddVtableAttributesForExpression(propertyDeclaration.Initializer.Value, namedType, isGeneratedBindableCustomPropertyClass);
}
}
else if (propertyDeclaration.ExpressionBody != null)
{
var leftSymbol = context.SemanticModel.GetSymbolInfo(propertyDeclaration.Type).Symbol;
if (leftSymbol is INamedTypeSymbol namedType)
{
AddVtableAttributesForExpression(propertyDeclaration.ExpressionBody.Expression, namedType);
AddVtableAttributesForExpression(propertyDeclaration.ExpressionBody.Expression, namedType, isGeneratedBindableCustomPropertyClass);
}
}
}
Expand All @@ -1355,7 +1359,11 @@ private static EquatableArray<VtableAttribute> GetVtableAttributesToAddOnLookupT
var propertyTypeSymbol = context.SemanticModel.GetSymbolInfo(propertyDeclarationSyntax.Type).Symbol;
if (propertyTypeSymbol is ITypeSymbol typeSymbol)
{
AddVtableAttributesForExpression(returnDeclaration.Expression, typeSymbol);
var isGeneratedBindableCustomPropertyClass =
context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax) is IPropertySymbol propertySymbol &&
GeneratorHelper.IsGeneratedBindableCustomPropertyClass(context.SemanticModel.Compilation, propertySymbol.ContainingSymbol);

AddVtableAttributesForExpression(returnDeclaration.Expression, typeSymbol, isGeneratedBindableCustomPropertyClass);
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions src/Tests/SourceGeneratorTest/AotOptimizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,59 @@ public List<int> M()
Assert.IsFalse(generated.Contains("System.Collections.Generic.List`1[System.Int32]"));
}

[TestMethod]
public void GeneratedBindableCustomProperty_DiscoversPropertyTypes()
{
const string source = """
using System.Collections.ObjectModel;
using WinRT;

[GeneratedBindableCustomProperty]
internal partial class ViewModel
{
private readonly ObservableCollection<string> items = new();

public ObservableCollection<string> Items
{
get { return items; }
}

public ObservableCollection<int> Numbers { get; } = new();

public ObservableCollection<double> Values => new();
}
""";

string generated = RunAotOptimizer(source);

Assert.IsTrue(generated.Contains("System.Collections.ObjectModel.ObservableCollection`1[System.String]"));
Assert.IsTrue(generated.Contains("System.Collections.ObjectModel.ObservableCollection`1[System.Int32]"));
Assert.IsTrue(generated.Contains("System.Collections.ObjectModel.ObservableCollection`1[System.Double]"));
Assert.IsTrue(generated.Contains("Windows.Foundation.Collections.IVector`1<String>"));
}

[TestMethod]
public void NonBindableProperties_DoNotDiscoverConcreteTypes()
{
const string source = """
using System.Collections.ObjectModel;

internal class ViewModel
{
private readonly ObservableCollection<string> items = new();

public ObservableCollection<string> Items
{
get { return items; }
}
}
""";

string generated = RunAotOptimizer(source);

Assert.IsFalse(generated.Contains("System.Collections.ObjectModel.ObservableCollection`1[System.String]"));
}

private static string RunAotOptimizer(string source)
{
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.Latest));
Expand Down