diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
index c9693689..f3058c13 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -46,3 +46,4 @@ CMPSD2D0036 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0037 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0038 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0039 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPSD2D0040 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index ea0d2d07..ff4fd2ab 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -551,4 +551,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "A D2D1 shader must have all the indices of its simple and complex inputs in the valid range (between 0 and the number of declared inputs).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for a method invocation that is not valid from a shader.
+ ///
+ /// Format: "The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor InvalidMethodCall = new DiagnosticDescriptor(
+ id: "CMPSD2D0040",
+ title: "Invalid method invocation from a D2D1 shader",
+ messageFormat: "The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)",
+ category: "ComputeSharp.D2D1.Shaders",
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "A D2D1 shader can only invoke methods that are either HLSL intrinsics or with source available for analysis.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index 6ccf0f91..b519066b 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -305,8 +305,14 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter
if (!this.staticMethods.ContainsKey(method))
{
+ if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
+ {
+ Diagnostics.Add(InvalidMethodCall, node, method);
+
+ return updatedNode;
+ }
+
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.staticMethods, ConstantDefinitions, Diagnostics);
- MethodDeclarationSyntax methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax();
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace(eol: "\n").WithoutTrivia();
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
index 70dd2866..7fc831e9 100644
--- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -55,3 +55,4 @@ CMPS0045 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
CMPS0046 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0047 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0048 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPS0049 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index e32a8062..b9f9d0d9 100644
--- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -680,4 +680,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "The dispatch axis value for a shader marked as embedded bytecode have to be valid (flags are not supported).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for a method invocation that is not valid from a shader.
+ ///
+ /// Format: "The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor InvalidMethodCall = new DiagnosticDescriptor(
+ id: "CMPS0049",
+ title: "Invalid method invocation from a shader",
+ messageFormat: "The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)",
+ category: "ComputeSharp.Shaders",
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "Shaders can only invoke methods that are either HLSL intrinsics or with source available for analysis.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index f005e1ac..59e5e7fd 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -416,8 +416,14 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter
if (!this.staticMethods.ContainsKey(method))
{
+ if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
+ {
+ Diagnostics.Add(InvalidMethodCall, node, method);
+
+ return updatedNode;
+ }
+
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.staticMethods, ConstantDefinitions, Diagnostics);
- MethodDeclarationSyntax methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax();
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace(eol: "\n").WithoutTrivia();
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
index 5dc60816..4a1f0af3 100644
--- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
+++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
@@ -1574,6 +1574,34 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics(source, "CMPS0048");
}
+ [TestMethod]
+ public void InvalidMethodCall()
+ {
+ string source = @"
+ using System;
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public readonly ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ buffer[0] = Convert.ToSingle(42);
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0049", "CMPS0047");
+ }
+
///
/// Verifies the output of a source generator.
///