Improve primary constructors error state, add test

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-03 12:33:52 +01:00
1 親 7e7b57cd67
コミット 7cfaf16ef8
2ファイル変更
+35 -1

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

+6 -1
ファイルの表示
@@ -655,11 +655,16 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
if (!this.constructors.ContainsKey(constructor))
{
// If we can't find the constructor declaration for the syntax reference of the constructor, it means that either the
// type is from another assembly (hence we have no syntax references at all), or the constructor is compiler generated
// (even if IsImplicitlyDeclared is false) and a primary constructor. This is deliberately not supported, as there
// are very subtle ways in which captures can interact with fields and methods, and we cannot guarantee to track and
// rewrite all of them correctly to exactly preserve the same semantics. So we just block such cases entirely as well.
if (constructor.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not ConstructorDeclarationSyntax constructorNode)
{
Diagnostics.Add(InvalidMethodOrConstructorCall, node, constructor);
return updatedNode;
return base.VisitUserDefinedObjectCreationExpression(node, updatedNode, targetType);
}
// Chaining constructors is not supported, so emit a diagnostic to inform the user.
+29
ファイルの表示
@@ -1452,6 +1452,35 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<ComputeShaderDescriptorGenerator>(source, "CMPS0047", "CMPS0061");
}
[TestMethod]
public void InvalidMethodOrConstructorCall_PrimaryConstructor()
{
const string source = """
using ComputeSharp;
public struct MyStruct(int x)
{
public int PlusOne()
{
return x + 1;
}
}
[GeneratedComputeShaderDescriptor]
public partial struct MyShader : IComputeShader
{
public ReadWriteBuffer<int> buffer;
public void Execute()
{
buffer[ThreadIds.X] = new MyStruct(ThreadIds.X).PlusOne();
}
}
""";
VerifyGeneratedDiagnostics<ComputeShaderDescriptorGenerator>(source, "CMPS0047", "CMPS0049");
}
/// <summary>
/// Verifies the output of a source generator.
/// </summary>