From bf7c470cb39e02fcb0264ae9def1c0276042cc39 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 26 Nov 2023 16:35:20 +0100 Subject: [PATCH] Skip generating for generic shader types, and warn --- .../D2DPixelShaderDescriptorGenerator.cs | 5 +++-- ...tedPixelShaderDescriptorAttributeTargetAnalyzer.cs | 11 ++++++++--- .../Diagnostics/DiagnosticDescriptors.cs | 6 +++--- .../ComputeShaderDescriptorGenerator.cs | 5 +++-- ...dComputeShaderDescriptorAttributeTargetAnalyzer.cs | 11 ++++++----- .../Diagnostics/DiagnosticDescriptors.cs | 6 +++--- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index dd88f388..48acf353 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -39,8 +39,9 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene return default; } - // If the type symbol doesn't have at least one interface, it can't possibly be a shader type - if (context.TargetSymbol is not INamedTypeSymbol { AllInterfaces.Length: > 0 } typeSymbol) + // If the type symbol doesn't have at least one interface, it can't possibly be a shader type. + // Also check for generic types, just like with DX12 shaders (including nesting inside generics). + if (context.TargetSymbol is not INamedTypeSymbol { AllInterfaces.Length: > 0, IsGenericType: false } typeSymbol) { return default; } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs index 4dc12f69..49f9b8fd 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs @@ -38,9 +38,14 @@ public sealed class InvalidD2DGeneratedPixelShaderDescriptorAttributeTargetAnaly return; } - // Emit a diagnostic if the target type is using [D2DGeneratedPixelShaderDescriptor] but does not implement ID2D1PixelShader - if (typeSymbol.TryGetAttributeWithType(d2DGeneratedPixelShaderDescriptorAttributeSymbol, out AttributeData? attribute) && - !typeSymbol.HasInterfaceWithType(d2D1PixelShaderSymbol)) + // If the type does not have [D2DGeneratedPixelShaderDescriptor], we can stop here + if (!typeSymbol.TryGetAttributeWithType(d2DGeneratedPixelShaderDescriptorAttributeSymbol, out AttributeData? attribute)) + { + return; + } + + // Emit a diagnostic if the target type is generic or does not implement ID2D1PixelShader + if (typeSymbol.IsGenericType || !typeSymbol.HasInterfaceWithType(d2D1PixelShaderSymbol)) { context.ReportDiagnostic(Diagnostic.Create( InvalidD2DGeneratedPixelShaderDescriptorAttributeTarget, diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 1cf84379..f4f43079 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -978,17 +978,17 @@ partial class DiagnosticDescriptors /// /// Gets a for when the [D2DGeneratedPixelShaderDescriptor] attribute is being used on an invalid target type. /// - /// Format: "The type {0} is not a valid target for the [D2DGeneratedPixelShaderDescriptor] attribute (only types implementing the ID2D1PixelShader interface are valid)". + /// Format: "The type {0} is not a valid target for the [D2DGeneratedPixelShaderDescriptor] attribute (only non generic types implementing the ID2D1PixelShader interface are valid)". /// /// public static readonly DiagnosticDescriptor InvalidD2DGeneratedPixelShaderDescriptorAttributeTarget = new( id: "CMPSD2D0066", title: "Invalid [D2DGeneratedPixelShaderDescriptor] attribute target", - messageFormat: "The type {0} is not a valid target for the [D2DGeneratedPixelShaderDescriptor] attribute (only types implementing ID2D1PixelShader interface are valid)", + messageFormat: "The type {0} is not a valid target for the [D2DGeneratedPixelShaderDescriptor] attribute (only non generic types implementing ID2D1PixelShader interface are valid)", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "The [D2DGeneratedPixelShaderDescriptor] attribute must be used on types that implement the ID2D1PixelShader interface.", + description: "The [D2DGeneratedPixelShaderDescriptor] attribute must be used on non generic types that implement the ID2D1PixelShader interface.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index c6253f92..bf952bab 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -39,8 +39,9 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener return default; } - // If the type symbol doesn't have at least one interface, it can't possibly be a shader type - if (context.TargetSymbol is not INamedTypeSymbol { AllInterfaces.Length: > 0 } typeSymbol) + // If the type symbol doesn't have at least one interface, it can't possibly be a shader type. + // Additionally, shader types cannot be generic (including nested inside generic types). + if (context.TargetSymbol is not INamedTypeSymbol { AllInterfaces.Length: > 0, IsGenericType: false } typeSymbol) { return default; } diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs index 28f94f81..3035b23e 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs @@ -45,13 +45,14 @@ public sealed class InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyz return; } - // If the type doesn't implement IComputeShader nor IComputeShader, we can emit a diagnostic - if (!MissingComputeShaderDescriptorOnComputeShaderAnalyzer.IsComputeShaderType(typeSymbol, computeShaderSymbol, pixelShaderSymbol)) + // If the type is generic or it doesn't implement IComputeShader nor IComputeShader, we emit a diagnostic + if (typeSymbol.IsGenericType || + !MissingComputeShaderDescriptorOnComputeShaderAnalyzer.IsComputeShaderType(typeSymbol, computeShaderSymbol, pixelShaderSymbol)) { context.ReportDiagnostic(Diagnostic.Create( - InvalidGeneratedComputeShaderDescriptorAttributeTarget, - attribute.GetLocation(), - typeSymbol)); + InvalidGeneratedComputeShaderDescriptorAttributeTarget, + attribute.GetLocation(), + typeSymbol)); } }, SymbolKind.NamedType); }); diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 87340a59..2a8e155c 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -754,17 +754,17 @@ partial class DiagnosticDescriptors /// /// Gets a for when the [GeneratedComputeShaderDescriptor] attribute is being used on an invalid target type. /// - /// Format: "The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only types implementing the IComputeShader or IComputeShader<TPixel> interface are valid)". + /// Format: "The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only non generic types implementing the IComputeShader or IComputeShader<TPixel> interface are valid)". /// /// public static readonly DiagnosticDescriptor InvalidGeneratedComputeShaderDescriptorAttributeTarget = new( id: "CMPS0054", title: "Invalid [GeneratedComputeShaderDescriptor] attribute target", - messageFormat: "The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only types implementing the IComputeShader or IComputeShader interface are valid)", + messageFormat: "The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only non generic types implementing the IComputeShader or IComputeShader interface are valid)", category: "ComputeSharp.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "The [GeneratedComputeShaderDescriptor] attribute must be used on types that implement the IComputeShader or IComputeShader interfaces.", + description: "The [GeneratedComputeShaderDescriptor] attribute must be used on non generic types that implement the IComputeShader or IComputeShader interfaces.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); ///