diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
index 0de6514d..973c044b 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
@@ -20,10 +20,9 @@ partial class D2DPixelShaderSourceGenerator
///
/// Validates that the return type of the annotated method is valid and returns the type name.
///
- /// The collection of produced instances.
/// The input instance to process.
- /// The HLSL source to compile, if present.
- public static string? GetInvalidReturnType(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol)
+ /// The fully qualified type name, if invalid.
+ public static string? GetInvalidReturnType(IMethodSymbol methodSymbol)
{
if (methodSymbol.ReturnType is not INamedTypeSymbol
{
@@ -33,13 +32,6 @@ partial class D2DPixelShaderSourceGenerator
TypeArguments: [{ SpecialType: SpecialType.System_Byte }]
})
{
- diagnostics.Add(
- InvalidD2DPixelShaderSourceMethodReturnType,
- methodSymbol,
- methodSymbol.Name,
- methodSymbol.ContainingType,
- methodSymbol.ReturnType);
-
return methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
index c26dfc4a..57082fdd 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
@@ -40,7 +40,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
// Get the remaining info for the current shader
ImmutableArray modifiers = methodDeclaration.Modifiers.Select(token => (ushort)token.Kind()).ToImmutableArray();
string methodName = methodSymbol.Name;
- string? invalidReturnType = Execute.GetInvalidReturnType(diagnostics, methodSymbol);
+ string? invalidReturnType = Execute.GetInvalidReturnType(methodSymbol);
string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol);
D2D1ShaderProfile shaderProfile = Execute.GetShaderProfile(diagnostics, methodSymbol);
D2D1CompileOptions compileOptions = Execute.GetCompileOptions(diagnostics, methodSymbol);
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs
new file mode 100644
index 00000000..f14c2949
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs
@@ -0,0 +1,58 @@
+using System.Collections.Immutable;
+using System.Linq;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.Diagnostics;
+using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
+
+namespace ComputeSharp.D2D1.SourceGenerators;
+
+///
+/// A diagnostic analyzer that generates diagnostics when [D2DPixelShaderSource] is used on a method with an incorrect return type.
+///
+[DiagnosticAnalyzer(LanguageNames.CSharp)]
+public sealed class InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer : DiagnosticAnalyzer
+{
+ ///
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DPixelShaderSourceMethodReturnType);
+
+ ///
+ public override void Initialize(AnalysisContext context)
+ {
+ context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
+ context.EnableConcurrentExecution();
+
+ context.RegisterCompilationStartAction(static context =>
+ {
+ // Get the [D2DPixelShaderSource] symbol
+ if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute") is not { } d2DPixelShaderSourceAttributeSymbol)
+ {
+ return;
+ }
+
+ // Also get the constructed ReadOnlySpan type
+ INamedTypeSymbol readOnlySpanSymbol = context.Compilation.GetTypeByMetadataName("System.ReadOnlySpan`1")!;
+ INamedTypeSymbol byteSymbol = context.Compilation.GetSpecialType(SpecialType.System_Byte);
+ INamedTypeSymbol readOnlySpanOfByteSymbol = readOnlySpanSymbol.Construct(byteSymbol);
+
+ context.RegisterSymbolAction(context =>
+ {
+ // Only partial definition methods are possible targets
+ if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol)
+ {
+ return;
+ }
+
+ // Emit a diagnostic if the return type is not ReadOnlySpan
+ if (!SymbolEqualityComparer.Default.Equals(methodSymbol.ReturnType, readOnlySpanOfByteSymbol))
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ InvalidD2DPixelShaderSourceMethodReturnType,
+ methodSymbol.Locations.First(),
+ methodSymbol.Name,
+ methodSymbol.ContainingType,
+ methodSymbol.ReturnType));
+ }
+ }, SymbolKind.Method);
+ });
+ }
+}
\ No newline at end of file