Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IsPartial check and escaping of assembly name #1802

Merged
merged 5 commits into from
Sep 29, 2024
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
4 changes: 2 additions & 2 deletions src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static bool NeedVtableAttribute(SyntaxNode node)
{
return node is ClassDeclarationSyntax declaration &&
!declaration.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword) || m.IsKind(SyntaxKind.AbstractKeyword)) &&
declaration.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)) &&
GeneratorHelper.IsPartial(declaration) &&
!GeneratorHelper.IsWinRTType(declaration); // Making sure it isn't an RCW we are projecting.
}

Expand All @@ -140,7 +140,7 @@ private static bool NeedCustomPropertyImplementation(SyntaxNode node)
{
return node is ClassDeclarationSyntax declaration &&
!declaration.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword) || m.IsKind(SyntaxKind.AbstractKeyword)) &&
declaration.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)) &&
GeneratorHelper.IsPartial(declaration) &&
GeneratorHelper.HasBindableCustomPropertyAttribute(declaration);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@
<value>Class is not marked partial</value>
</data>
<data name="ClassNotMarkedPartial_Text" xml:space="preserve">
<value>Class '{0}' implements WinRT interfaces but isn't marked partial. Type should be marked partial for trimming and AOT compatibility if passed across the WinRT ABI.</value>
<value>Class '{0}' implements WinRT interfaces but it or a parent type isn't marked partial. Type and any parent types should be marked partial for trimming and AOT compatibility if passed across the WinRT ABI.</value>
</data>
<data name="BindableCustomPropertyClassNotMarkedPartial_Text" xml:space="preserve">
<value>Class '{0}' has attribute GeneratedBindableCustomProperty but it or a parent type isn't marked partial. Type and any parent types should be marked partial to allow source generation for trimming and AOT compatibility.</value>
</data>
<data name="DisjointNamespaceRule_Brief" xml:space="preserve">
<value>Namespace is disjoint from main (winmd) namespace</value>
</data>
Expand Down
21 changes: 19 additions & 2 deletions src/Authoring/WinRT.SourceGenerator/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,24 @@ static bool IsArgumentTypeParameter(ITypeSymbol argument)

public static bool IsPartial(INamedTypeSymbol symbol)
{
return symbol.DeclaringSyntaxReferences.Any(syntax => syntax.GetSyntax() is BaseTypeDeclarationSyntax declaration && declaration.Modifiers.Any(SyntaxKind.PartialKeyword));
bool isPartial = true;
for (ITypeSymbol parent = symbol; parent is not null; parent = parent.ContainingType)
{
isPartial &= parent.DeclaringSyntaxReferences.Any(
syntax => syntax.GetSyntax() is BaseTypeDeclarationSyntax declaration &&
declaration.Modifiers.Any(SyntaxKind.PartialKeyword));
}
return isPartial;
}

public static bool IsPartial(TypeDeclarationSyntax node)
{
bool isPartial = true;
for (TypeDeclarationSyntax parent = node; parent is not null; parent = parent.Parent as TypeDeclarationSyntax)
{
isPartial &= parent.Modifiers.Any(static m => m.IsKind(SyntaxKind.PartialKeyword));
}
return isPartial;
}

public static bool HasPrivateclass(ITypeSymbol symbol)
Expand Down Expand Up @@ -1028,7 +1045,7 @@ public static string GetAbiMarshalerType(string type, string abiType, TypeKind k

public static string EscapeTypeNameForIdentifier(string typeName)
{
return Regex.Replace(typeName, """[(\ |:<>,\.)]""", "_");
return Regex.Replace(typeName, """[(\ |:<>,\.\-@)]""", "_");
}

public readonly struct MappedType
Expand Down
27 changes: 22 additions & 5 deletions src/Authoring/WinRT.SourceGenerator/WinRTAotCodeFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public sealed class WinRTAotDiagnosticAnalyzer : DiagnosticAnalyzer
WinRTRules.ClassNotAotCompatibleOldProjectionWarning,
WinRTRules.ClassNotAotCompatibleOldProjectionInfo,
WinRTRules.ClassEnableUnsafeWarning,
WinRTRules.ClassEnableUnsafeInfo);
WinRTRules.ClassEnableUnsafeInfo,
WinRTRules.ClassWithBindableCustomPropertyNotPartial);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => _supportedDiagnostics;

Expand All @@ -47,7 +48,8 @@ public override void Initialize(AnalysisContext context)
bool isComponentProject = context.Options.AnalyzerConfigOptionsProvider.IsCsWinRTComponent();
var winrtTypeAttribute = context.Compilation.GetTypeByMetadataName("WinRT.WindowsRuntimeTypeAttribute");
var winrtExposedTypeAttribute = context.Compilation.GetTypeByMetadataName("WinRT.WinRTExposedTypeAttribute");
if (winrtTypeAttribute is null || winrtExposedTypeAttribute is null)
var generatedBindableCustomPropertyAttribute = context.Compilation.GetTypeByMetadataName("WinRT.GeneratedBindableCustomPropertyAttribute");
if (winrtTypeAttribute is null || winrtExposedTypeAttribute is null || generatedBindableCustomPropertyAttribute is null)
{
return;
}
Expand Down Expand Up @@ -115,6 +117,13 @@ public override void Initialize(AnalysisContext context)
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, namedType.Locations[0], namedType.Name, string.Join(", ", interfacesFromOldProjections)));
}
}

// Make sure classes with the GeneratedBindableCustomProperty attribute are marked partial.
if (GeneratorHelper.HasAttributeWithType(namedType, generatedBindableCustomPropertyAttribute) &&
!GeneratorHelper.IsPartial(namedType))
{
context.ReportDiagnostic(Diagnostic.Create(WinRTRules.ClassWithBindableCustomPropertyNotPartial, namedType.Locations[0], namedType.Name));
}
}
}, SymbolKind.NamedType);

Expand Down Expand Up @@ -416,13 +425,21 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

private static async Task<Document> MakeTypePartial(Document document, ClassDeclarationSyntax @class, CancellationToken token)
{
var newClass = @class.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword));

var oldRoot = await document.GetSyntaxRootAsync(token).ConfigureAwait(false);
if (oldRoot is null)
return document;

var newRoot = oldRoot.ReplaceNode(@class, newClass);
var newRoot = oldRoot.ReplaceNodes(@class.AncestorsAndSelf().OfType<TypeDeclarationSyntax>(),
(_, typeDeclaration) =>
{
if (!typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
{
return typeDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword));
}

return typeDeclaration;
});

return document.WithSyntaxRoot(newRoot);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Authoring/WinRT.SourceGenerator/WinRTRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,12 @@ private static DiagnosticDescriptor MakeRule(string id, string title, string mes
CsWinRTDiagnosticStrings.EnableUnsafe_Brief,
CsWinRTDiagnosticStrings.EnableUnsafe_Text,
false);

public static DiagnosticDescriptor ClassWithBindableCustomPropertyNotPartial = MakeRule(
"CsWinRT1028",
CsWinRTDiagnosticStrings.ClassNotMarkedPartial_Brief,
CsWinRTDiagnosticStrings.BindableCustomPropertyClassNotMarkedPartial_Text,
false,
true);
}
}
33 changes: 33 additions & 0 deletions src/Tests/FunctionalTests/CCW/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,17 @@ internal static IProperties2 GetGenericInstance()
}
}

class TestClass3
{
// Making sure it compiles if the parent class isn't partial, but the actual class is.
#pragma warning disable CsWinRT1028 // Class is not marked partial
partial class NestedTestClass2 : IProperties2
#pragma warning restore CsWinRT1028 // Class is not marked partial
{
private int _value;
public int ReadWriteProperty { get => _value; set => _value = value; }
}
}
sealed partial class CustomCommand : ICommand
{
public event EventHandler CanExecuteChanged;
Expand Down Expand Up @@ -663,6 +674,28 @@ partial class LanguageDervied2 : Language
public int Derived { get; set; }
}

// Testing code compiles when not marked partial
[GeneratedBindableCustomProperty]
#pragma warning disable CsWinRT1028 // Class is not marked partial
class LanguageDervied3 : Language
#pragma warning restore CsWinRT1028 // Class is not marked partial
{
public int Derived { get; set; }
}

class ParentClass
{
// Testing code compiles when not marked partial
[GeneratedBindableCustomProperty]
#pragma warning disable CsWinRT1028 // Class is not marked partial
partial class LanguageDervied3 : Language
#pragma warning restore CsWinRT1028 // Class is not marked partial
{
public int Derived { get; set; }
}
}


[GeneratedBindableCustomPropertyAttribute]
sealed partial class Language2
{
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/FunctionalTests/Collections/Collections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Authoring\WinRT.SourceGenerator\WinRT.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" SetPlatform="Platform=x64"/>
<ProjectReference Include="..\..\..\Authoring\WinRT.SourceGenerator\WinRT.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" SetPlatform="Platform=x64" />
<ProjectReference Include="..\..\..\Projections\Test\Test.csproj" />
<ProjectReference Include="..\..\..\Projections\Windows\Windows.csproj" />
<ProjectReference Include="..\TestLibrary\TestLibrary.csproj" />
<ProjectReference Include="..\TestLibrary\Test-Library.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/cswinrt.sln
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CCW", "Tests\FunctionalTest
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinAppSDK", "Projections\WinAppSDK\WinAppSDK.csproj", "{7B803846-91AE-4B98-AC93-D3FCFB2DE5AA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestLibrary", "Tests\FunctionalTests\TestLibrary\TestLibrary.csproj", "{335D51AC-1DCF-4487-A2BD-34CE2F17B3C4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test-Library", "Tests\FunctionalTests\TestLibrary\Test-Library.csproj", "{335D51AC-1DCF-4487-A2BD-34CE2F17B3C4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Windows.UI.Xaml", "Projections\Windows.UI.Xaml\Windows.UI.Xaml.csproj", "{E85F3614-79B6-4652-BDB0-64AF68874CE0}"
EndProject
Expand Down
Loading