diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs index c5868a6a..d8b9051c 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.SourceGenerators; public sealed class InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer : DiagnosticAnalyzer { /// - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DGeneratedPixelShaderDescriptorAttributeTarget); + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidGeneratedPixelShaderDescriptorAttributeTarget); /// public override void Initialize(AnalysisContext context) @@ -39,16 +39,27 @@ public sealed class InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyz return; } - // Emit a diagnostic if the target type is using [GeneratedComputeShaderDescriptor] but does not implement IComputeShader nor IComputeShader - if (typeSymbol.TryGetAttributeWithType(generatedComputeShaderDescriptorAttributeSymbol, out AttributeData? attribute) && - !typeSymbol.HasInterfaceWithType(computeShaderSymbol) && - !typeSymbol.HasInterfaceWithType(pixelShaderSymbol)) + // If the current type does not have [GeneratedComputeShaderDescriptor], there is nothing to do + if (!typeSymbol.TryGetAttributeWithType(generatedComputeShaderDescriptorAttributeSymbol, out AttributeData? attribute)) { - context.ReportDiagnostic(Diagnostic.Create( - InvalidD2DGeneratedPixelShaderDescriptorAttributeTarget, - attribute.GetLocation(), - typeSymbol)); + return; } + + // If the type implements IComputeShader or IComputeShader, it is valid + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, computeShaderSymbol) || + SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, pixelShaderSymbol)) + { + return; + } + } + + // If we got here, the type is not valid, so we can emit a diagnostic + context.ReportDiagnostic(Diagnostic.Create( + InvalidGeneratedPixelShaderDescriptorAttributeTarget, + attribute.GetLocation(), + typeSymbol)); }, SymbolKind.NamedType); }); } diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index e6467719..8fd642cb 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -759,7 +759,7 @@ partial class DiagnosticDescriptors /// Format: "The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only types implementing the IComputeShader or IComputeShader<TPixel> interface are valid)". /// /// - public static readonly DiagnosticDescriptor InvalidD2DGeneratedPixelShaderDescriptorAttributeTarget = new DiagnosticDescriptor( + public static readonly DiagnosticDescriptor InvalidGeneratedPixelShaderDescriptorAttributeTarget = new DiagnosticDescriptor( 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)",