diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 9d65598d..1ad3ab02 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -413,31 +413,34 @@ internal static class DiagnosticDescriptors /// /// Gets a for a property declaration. + /// + /// Format: "The D2D1 shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)". + /// /// - public static readonly DiagnosticDescriptor PropertyDeclaration = new DiagnosticDescriptor( + public static readonly DiagnosticDescriptor InvalidPropertyDeclaration = new DiagnosticDescriptor( id: "CMPSD2D0031", - title: "Property declaration", - messageFormat: "Property declarations cannot be used in a compute shader", + title: "Invalid property declaration", + messageFormat: "The D2D1 shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "Property declarations cannot be used in a compute shader.", + description: "Property declarations (except for stateless properties explicitly implementing an interface property) cannot be used in a D2D1 shader.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// /// Gets a for a shader with a root signature that is too large. /// - /// Format: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources". + /// Format: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources". /// /// public static readonly DiagnosticDescriptor ShaderDispatchDataSizeExceeded = new DiagnosticDescriptor( id: "CMPSD2D0032", title: "Shader dispatch data size exceeded", - messageFormat: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources", + messageFormat: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources.", + description: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index 36842aac..07a8829f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -45,7 +45,7 @@ partial class ID2D1ShaderGenerator ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); // Properties are not supported - DetectAndReportPropertyDeclarations(builder, structDeclarationSymbol); + DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol); // We need to sets to track all discovered custom types and static methods HashSet discoveredTypes = new(SymbolEqualityComparer.Default); @@ -372,21 +372,25 @@ partial class ID2D1ShaderGenerator } /// - /// Finds and reports all declared properties in a shader. + /// Finds and reports all invalid declared properties in a shader. /// /// The collection of produced instances. /// The input instance to process. - private static void DetectAndReportPropertyDeclarations(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) + private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) { - foreach (var propertySymbol in structDeclarationSymbol.GetMembers().OfType()) + foreach (var memberSymbol in structDeclarationSymbol.GetMembers()) { - // Ignore explicit interface implementations - if (!propertySymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty) + // Detect properties that are not explicit interface implementations + if (memberSymbol is IPropertySymbol { ExplicitInterfaceImplementations.IsEmpty: true }) { - return; + diagnostics.Add(InvalidPropertyDeclaration, memberSymbol, structDeclarationSymbol, memberSymbol); } - diagnostics.Add(DiagnosticDescriptors.PropertyDeclaration, propertySymbol); + // Detect properties causing a field to be generated + if (memberSymbol is IFieldSymbol { AssociatedSymbol: IPropertySymbol associatedProperty }) + { + diagnostics.Add(InvalidPropertyDeclaration, associatedProperty, structDeclarationSymbol, associatedProperty); + } } } diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index d57ee454..1e9e4d7f 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -542,15 +542,18 @@ internal static class DiagnosticDescriptors /// /// Gets a for a property declaration. + /// + /// Format: "The compute shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)". + /// /// - public static readonly DiagnosticDescriptor PropertyDeclaration = new DiagnosticDescriptor( + public static readonly DiagnosticDescriptor InvalidPropertyDeclaration = new DiagnosticDescriptor( id: "CMPS0040", - title: "Property declaration", - messageFormat: "Property declarations cannot be used in a compute shader", + title: "Invalid property declaration", + messageFormat: "The compute shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)", category: "ComputeSharp.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "Property declarations cannot be used in a compute shader.", + description: "Property declarations (except for stateless properties explicitly implementing an interface property) cannot be used in a compute shader.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs index bfb74874..83ff9589 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -43,8 +43,8 @@ partial class IShaderGenerator { ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); - // Properties are not supported - DetectAndReportPropertyDeclarations(builder, structDeclarationSymbol); + // Detect invalid properties + DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol); // We need to sets to track all discovered custom types and static methods HashSet discoveredTypes = new(SymbolEqualityComparer.Default); @@ -493,21 +493,25 @@ partial class IShaderGenerator } /// - /// Finds and reports all declared properties in a shader. + /// Finds and reports all invalid declared properties in a shader. /// /// The collection of produced instances. /// The input instance to process. - private static void DetectAndReportPropertyDeclarations(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) + private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) { - foreach (var propertySymbol in structDeclarationSymbol.GetMembers().OfType()) + foreach (var memberSymbol in structDeclarationSymbol.GetMembers()) { - // Ignore explicit interface implementations - if (!propertySymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty) + // Detect properties that are not explicit interface implementations + if (memberSymbol is IPropertySymbol { ExplicitInterfaceImplementations.IsEmpty: true }) { - return; + diagnostics.Add(InvalidPropertyDeclaration, memberSymbol, structDeclarationSymbol, memberSymbol); } - diagnostics.Add(DiagnosticDescriptors.PropertyDeclaration, propertySymbol); + // Detect properties causing a field to be generated + if (memberSymbol is IFieldSymbol { AssociatedSymbol: IPropertySymbol associatedProperty }) + { + diagnostics.Add(InvalidPropertyDeclaration, associatedProperty, structDeclarationSymbol, associatedProperty); + } } } diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index 53623b77..42fc327f 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -1253,6 +1253,39 @@ public class DiagnosticsTests VerifyGeneratedDiagnostics(source, "CMPS0040", "CMPS0047"); } + [TestMethod] + public void PropertyDeclaration_AutoProp_WithExplicitInterfaceImplementation() + { + string source = @" + using ComputeSharp; + + namespace ComputeSharp + { + public class ReadWriteBuffer { } + } + + namespace MyFancyApp.Sample + { + public interface IFoo + { + float Pi { get; } + } + + public struct MyShader : IComputeShader, IFoo + { + public ReadWriteBuffer buffer; + + float IFoo.Pi { get; } = 3.14f; + + public void Execute() + { + } + } + }"; + + VerifyGeneratedDiagnostics(source, "CMPS0040", "CMPS0047"); + } + [TestMethod] public void PropertyDeclaration_GetterBlock() { diff --git a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs index d63de33c..dd147250 100644 --- a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs +++ b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs @@ -62,12 +62,13 @@ public unsafe partial class AutoConstructorAttributeTests public void Foo(); } + [AutoConstructor] public partial struct AnotherStruct : IAnotherInterface { public int a; public float b; - int IAnotherInterface.B { get; } + int IAnotherInterface.B => 100; void IAnotherInterface.Foo() { diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 3fd445c3..7b5d0d08 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -289,7 +289,7 @@ namespace ComputeSharp.Tests [AutoConstructor] internal readonly partial struct ComputeShaderWithInheritedShaderInterfaceShader : IMyBaseShader { - int IMyBaseShader.A { get; } + int IMyBaseShader.A => 42; void IMyBaseShader.B() { @@ -322,7 +322,7 @@ namespace ComputeSharp.Tests [AutoConstructor] internal readonly partial struct PixelShaderWithInheritedShaderInterfaceShader : IMyBaseShader { - int IMyBaseShader.A { get; } + int IMyBaseShader.A => 42; void IMyBaseShader.B() {