From b266308c8bff237334c2eb15ccc8deb8202b0718 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Nov 2023 00:23:37 +0100 Subject: [PATCH] Emit a diagnostic for collection expressions --- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 13 +++++++++ .../HlslSourceRewriter.Diagnostics.cs | 8 ++++++ .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 13 +++++++++ .../DiagnosticsTests.cs | 27 ++++++++++++++++--- 6 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index ac76dd13..9378f60c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -78,3 +78,4 @@ CMPSD2D0068 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0069 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0070 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0071 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0072 | 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 d5feb67a..2fdfe529 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1067,4 +1067,17 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "An initializer expression cannot be used in a pixel shader.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a collection expression. + /// + public static readonly DiagnosticDescriptor CollectionExpression = new( + id: "CMPSD2D0072", + title: "Collection expression", + messageFormat: "A collection expression cannot be used in a pixel shader", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A collection expression cannot be used in a pixel shader.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.Diagnostics.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.Diagnostics.cs index 8ea04da9..2a7da27c 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.Diagnostics.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.Diagnostics.cs @@ -24,6 +24,14 @@ partial class HlslSourceRewriter return base.VisitInitializerExpression(node); } + /// + public override SyntaxNode? VisitCollectionExpression(CollectionExpressionSyntax node) + { + Diagnostics.Add(CollectionExpression, node); + + return base.VisitCollectionExpression(node); + } + /// public override SyntaxNode? VisitAwaitExpression(AwaitExpressionSyntax node) { diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index 78110930..35b0fc9a 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -65,3 +65,4 @@ CMPS0056 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser CMPS0057 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0058 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0059 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0060 | 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 bb79aa1f..3e77eb76 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -843,4 +843,17 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "An initializer expression cannot be used in a compute shader.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a collection expression. + /// + public static readonly DiagnosticDescriptor CollectionExpression = new( + id: "CMPS0060", + title: "Collection expression", + messageFormat: "A collection expression cannot be used in a compute shader", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A collection expression cannot be used in a compute shader.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index 0b805863..bdff7164 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -1345,7 +1345,7 @@ public class DiagnosticsTests } [TestMethod] - public void InitializerExpression_ObjectCreation() + public void InvalidInitializerExpression_ObjectCreation() { const string source = """ using ComputeSharp; @@ -1371,7 +1371,7 @@ public class DiagnosticsTests } [TestMethod] - public void InitializerExpression_ImplicitObjectCreation() + public void InvalidInitializerExpression_ImplicitObjectCreation() { const string source = """ using ComputeSharp; @@ -1397,7 +1397,7 @@ public class DiagnosticsTests } [TestMethod] - public void InitializerExpression_CollectionInitializer() + public void InvalidInitializerExpression_CollectionInitializer() { const string source = """ using ComputeSharp; @@ -1417,6 +1417,27 @@ public class DiagnosticsTests VerifyGeneratedDiagnostics(source, "CMPS0031", "CMPS0047", "CMPS0059"); } + [TestMethod] + public void InvalidCollectionExpression() + { + const string source = """ + using ComputeSharp; + + [GeneratedComputeShaderDescriptor] + public partial struct MyShader : IComputeShader + { + public ReadWriteBuffer buffer; + + public void Execute() + { + int[] numbers = [1, 2, 3, 4]; + } + } + """; + + VerifyGeneratedDiagnostics(source, "CMPS0031", "CMPS0047", "CMPS0060"); + } + /// /// Verifies the output of a source generator. ///