Tweak properties detection to block all field backed ones

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-29 12:58:19 +02:00
1 親 7d14403632
コミット 1ef6040e58
7ファイル変更
+79 -31

検索条件に一致するファイルはありません

+10 -7
ファイルの表示
@@ -413,31 +413,34 @@ internal static class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a property declaration.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)"</c>.
/// </para>
/// </summary>
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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a shader with a root signature that is too large.
/// <para>
/// Format: <c>"The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources"</c>.
/// Format: <c>"The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources"</c>.
/// </para>
/// </summary>
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");
/// <summary>
@@ -45,7 +45,7 @@ partial class ID2D1ShaderGenerator
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// Properties are not supported
DetectAndReportPropertyDeclarations(builder, structDeclarationSymbol);
DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol);
// We need to sets to track all discovered custom types and static methods
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
@@ -372,21 +372,25 @@ partial class ID2D1ShaderGenerator
}
/// <summary>
/// Finds and reports all declared properties in a shader.
/// Finds and reports all invalid declared properties in a shader.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
private static void DetectAndReportPropertyDeclarations(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
{
foreach (var propertySymbol in structDeclarationSymbol.GetMembers().OfType<IPropertySymbol>())
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);
}
}
}
+7 -4
ファイルの表示
@@ -542,15 +542,18 @@ internal static class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a property declaration.
/// <para>
/// Format: <c>"The compute shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)"</c>.
/// </para>
/// </summary>
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");
/// <summary>
+13 -9
ファイルの表示
@@ -43,8 +43,8 @@ partial class IShaderGenerator
{
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// 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<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
@@ -493,21 +493,25 @@ partial class IShaderGenerator
}
/// <summary>
/// Finds and reports all declared properties in a shader.
/// Finds and reports all invalid declared properties in a shader.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
private static void DetectAndReportPropertyDeclarations(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
{
foreach (var propertySymbol in structDeclarationSymbol.GetMembers().OfType<IPropertySymbol>())
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);
}
}
}
+33
ファイルの表示
@@ -1253,6 +1253,39 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0040", "CMPS0047");
}
[TestMethod]
public void PropertyDeclaration_AutoProp_WithExplicitInterfaceImplementation()
{
string source = @"
using ComputeSharp;
namespace ComputeSharp
{
public class ReadWriteBuffer<T> { }
}
namespace MyFancyApp.Sample
{
public interface IFoo
{
float Pi { get; }
}
public struct MyShader : IComputeShader, IFoo
{
public ReadWriteBuffer<float> buffer;
float IFoo.Pi { get; } = 3.14f;
public void Execute()
{
}
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0040", "CMPS0047");
}
[TestMethod]
public void PropertyDeclaration_GetterBlock()
{
+2 -1
ファイルの表示
@@ -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()
{
+2 -2
ファイルの表示
@@ -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<float4>
{
int IMyBaseShader<float4>.A { get; }
int IMyBaseShader<float4>.A => 42;
void IMyBaseShader<float4>.B()
{