From 314da0c0b6b7f0f2df1b4720a3a332eb724c295a Mon Sep 17 00:00:00 2001 From: Manodasan Wignarajah Date: Sat, 7 Sep 2024 10:56:50 -0700 Subject: [PATCH] Detect when property expressions are used. --- src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs | 9 +++++++++ .../WinRT.SourceGenerator/WinRTAotCodeFixer.cs | 13 +++++++++++++ src/Tests/FunctionalTests/Collections/Program.cs | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs b/src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs index 1888f7eb5..0b473f14d 100644 --- a/src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs +++ b/src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs @@ -1136,6 +1136,15 @@ private static EquatableArray GetVtableAttributesToAddOnLookupT AddVtableAttributesForType(instantiatedType, namedType); } } + else if (propertyDeclaration.ExpressionBody != null) + { + var leftSymbol = context.SemanticModel.GetSymbolInfo(propertyDeclaration.Type).Symbol; + if (leftSymbol is INamedTypeSymbol namedType) + { + var instantiatedType = context.SemanticModel.GetTypeInfo(propertyDeclaration.ExpressionBody.Expression); + AddVtableAttributesForType(instantiatedType, namedType); + } + } } // Detect scenarios where the method or property being returned from is doing a box or cast of the type // in the return statement. diff --git a/src/Authoring/WinRT.SourceGenerator/WinRTAotCodeFixer.cs b/src/Authoring/WinRT.SourceGenerator/WinRTAotCodeFixer.cs index 5568b2aab..c06f6c167 100644 --- a/src/Authoring/WinRT.SourceGenerator/WinRTAotCodeFixer.cs +++ b/src/Authoring/WinRT.SourceGenerator/WinRTAotCodeFixer.cs @@ -232,6 +232,19 @@ public override void Initialize(AnalysisContext context) } } } + else if (propertyDeclaration.ExpressionBody != null) + { + var leftSymbol = context.SemanticModel.GetSymbolInfo(propertyDeclaration.Type).Symbol; + if (leftSymbol is INamedTypeSymbol namedType) + { + var instantiatedType = context.SemanticModel.GetTypeInfo(propertyDeclaration.ExpressionBody.Expression); + if (IsTypeOnLookupTable(instantiatedType, namedType, out var implementsOnlyCustomMappedInterface)) + { + ReportEnableUnsafeDiagnostic(context.ReportDiagnostic, instantiatedType.Type, propertyDeclaration.GetLocation(), !implementsOnlyCustomMappedInterface); + return; + } + } + } } // Detect scenarios where the method or property being returned from is doing a box or cast of the type // in the return statement. diff --git a/src/Tests/FunctionalTests/Collections/Program.cs b/src/Tests/FunctionalTests/Collections/Program.cs index 9f60c7800..ad8c5e73b 100644 --- a/src/Tests/FunctionalTests/Collections/Program.cs +++ b/src/Tests/FunctionalTests/Collections/Program.cs @@ -125,6 +125,9 @@ return 101; } +instance.BindableIterableProperty = CustomClass.DictionaryInstance; +instance.BindableIterableProperty = CustomClass.DictionaryInstance2; + var customObservableCollection = new CustomObservableCollection(); instance.BindableIterableProperty = customObservableCollection; if (customObservableCollection != instance.BindableIterableProperty) @@ -190,6 +193,16 @@ sealed partial class CustomClass : INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; public static IReadOnlyList Instances { get; } = new CustomClass[] { }; + + public static IReadOnlyDictionary DictionaryInstance => new Dictionary(); + + public static IReadOnlyDictionary DictionaryInstance2 + { + get + { + return new Dictionary(); + } + } } sealed partial class CustomObservableCollection : System.Collections.ObjectModel.ObservableCollection