diff --git a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs index 8a5771e5..c75b86ee 100644 --- a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs @@ -59,7 +59,7 @@ public sealed partial class AutoConstructorGenerator : IIncrementalGenerator { return ( from fieldSymbol in structDeclarationSymbol.GetMembers().OfType() - where fieldSymbol is { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false } + where fieldSymbol is { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } let typeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) select new ParameterInfo(typeName, fieldSymbol.Name)).ToImmutableArray(); } 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 f68e444d..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); @@ -240,6 +240,12 @@ partial class ID2D1ShaderGenerator methodDeclarationSymbol.TypeParameters.Length == 0 && methodDeclarationSymbol.Parameters.Length == 0; + // Except for the entry point, ignore explicit interface implementations + if (!isShaderEntryPoint && !methodDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty) + { + continue; + } + // Create the source rewriter for the current method ShaderSourceRewriter shaderSourceRewriter = new( structDeclarationSymbol, @@ -366,15 +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()) { - diagnostics.Add(DiagnosticDescriptors.PropertyDeclaration, propertySymbol); + // Detect properties that are not explicit interface implementations + if (memberSymbol is IPropertySymbol { ExplicitInterfaceImplementations.IsEmpty: true }) + { + diagnostics.Add(InvalidPropertyDeclaration, memberSymbol, structDeclarationSymbol, memberSymbol); + } + + // 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.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs index 14f23044..a6e21e6f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -43,9 +43,7 @@ partial class ID2D1ShaderGenerator foreach ( IFieldSymbol fieldSymbol in from fieldSymbol in currentTypeSymbol.GetMembers().OfType() - where fieldSymbol.Type is INamedTypeSymbol { IsStatic: false } && - !fieldSymbol.IsConst && !fieldSymbol.IsStatic && - !fieldSymbol.IsFixedSizeBuffer + where fieldSymbol is { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } select fieldSymbol) { string fieldName = fieldSymbol.Name; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs index 9232e403..79a971ae 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs @@ -18,7 +18,7 @@ partial class ID2D1ShaderGenerator /// Whether or not implements . private static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, INamedTypeSymbol d2D1PixelShaderSymbol) { - foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.Interfaces) + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { if (interfaceSymbol.Name == nameof(ID2D1PixelShader) && SymbolEqualityComparer.Default.Equals(interfaceSymbol, d2D1PixelShaderSymbol)) 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 3e35ad4b..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); @@ -354,6 +354,12 @@ partial class IShaderGenerator methodDeclarationSymbol.TypeParameters.Length == 0 && methodDeclarationSymbol.Parameters.Length == 0); + // Except for the entry point, ignore explicit interface implementations + if (!isShaderEntryPoint && !methodDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty) + { + continue; + } + // Create the source rewriter for the current method ShaderSourceRewriter shaderSourceRewriter = new( structDeclarationSymbol, @@ -487,15 +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()) { - diagnostics.Add(DiagnosticDescriptors.PropertyDeclaration, propertySymbol); + // Detect properties that are not explicit interface implementations + if (memberSymbol is IPropertySymbol { ExplicitInterfaceImplementations.IsEmpty: true }) + { + diagnostics.Add(InvalidPropertyDeclaration, memberSymbol, structDeclarationSymbol, memberSymbol); + } + + // 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/IShaderGenerator.CreateGetDispatchIdMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetDispatchIdMethod.cs index ebeb772e..589a926d 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetDispatchIdMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetDispatchIdMethod.cs @@ -23,7 +23,7 @@ partial class IShaderGenerator structDeclarationSymbol .GetMembers() .OfType() - .Where(static member => member.Type is INamedTypeSymbol { TypeKind: TypeKind.Delegate, IsStatic: false }) + .Where(static member => member is { Type: INamedTypeSymbol { TypeKind: TypeKind.Delegate, IsStatic: false }, IsImplicitlyDeclared: false }) .Select(static member => member.Name) .ToImmutableArray(); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs index f7a20523..40af5a53 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -48,9 +48,7 @@ partial class IShaderGenerator foreach ( IFieldSymbol fieldSymbol in from fieldSymbol in currentTypeSymbol.GetMembers().OfType() - where fieldSymbol.Type is INamedTypeSymbol { IsStatic: false } && - !fieldSymbol.IsConst && !fieldSymbol.IsStatic && - !fieldSymbol.IsFixedSizeBuffer + where fieldSymbol is { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } select fieldSymbol) { string fieldName = fieldSymbol.Name; diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs index 88d0550c..77462b98 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs @@ -23,7 +23,7 @@ partial class IShaderGenerator INamedTypeSymbol iComputeShaderSymbol, INamedTypeSymbol iPixelShaderSymbol) { - foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.Interfaces) + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { if (interfaceSymbol.Name == nameof(IComputeShader) && SymbolEqualityComparer.Default.Equals(interfaceSymbol, iComputeShaderSymbol)) 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 469c9e25..dd147250 100644 --- a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs +++ b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs @@ -54,4 +54,36 @@ public unsafe partial class AutoConstructorAttributeTests Assert.AreEqual(instance.a, a); Assert.AreEqual(instance.b, b); } + + public partial interface IAnotherInterface + { + public int B { get; } + + public void Foo(); + } + + [AutoConstructor] + public partial struct AnotherStruct : IAnotherInterface + { + public int a; + public float b; + + int IAnotherInterface.B => 100; + + void IAnotherInterface.Foo() + { + } + } + + [TestMethod] + public void GenerateConstructorWithExplicitInterfaceMembers() + { + int a = 42; + float b = 3.14f; + + AnotherStruct instance = new(a, b); + + Assert.AreEqual(instance.a, a); + Assert.AreEqual(instance.b, b); + } } diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 0c497090..7b5d0d08 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -272,6 +272,70 @@ namespace ComputeSharp.Tests this.buffer[ThreadIds.X] *= this.number; } } + + [TestMethod] + public void ComputeShaderWithInheritedShaderInterface() + { + _ = ReflectionServices.GetShaderInfo(); + } + + public interface IMyBaseShader : IComputeShader + { + public int A { get; } + + public void B(); + } + + [AutoConstructor] + internal readonly partial struct ComputeShaderWithInheritedShaderInterfaceShader : IMyBaseShader + { + int IMyBaseShader.A => 42; + + void IMyBaseShader.B() + { + } + + public readonly ReadWriteBuffer buffer; + public readonly float number; + + /// + public void Execute() + { + this.buffer[ThreadIds.X] *= this.number; + } + } + + [TestMethod] + public void PixelShaderWithInheritedShaderInterface() + { + _ = ReflectionServices.GetShaderInfo(); + } + + public interface IMyBaseShader : IPixelShader + where T : unmanaged + { + public int A { get; } + + public void B(); + } + + [AutoConstructor] + internal readonly partial struct PixelShaderWithInheritedShaderInterfaceShader : IMyBaseShader + { + int IMyBaseShader.A => 42; + + void IMyBaseShader.B() + { + } + + public readonly float number; + + /// + public float4 Execute() + { + return default; + } + } } }