From 07e929f9afcb67f3a5a5577ddb1bb8e85f5492bb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 14 Dec 2023 16:23:22 +0100 Subject: [PATCH] Add unit test for recursive rewriting handling --- .../DiagnosticsTests.cs | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index 3cf768d2..680aa668 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -1573,6 +1573,72 @@ public class DiagnosticsTests VerifyGeneratedDiagnostics(source, "CMPS0047", "CMPS0062"); } + [TestMethod] + public void RecursiveMembers() + { + const string source = """ + using ComputeSharp; + + internal static class ClassWithRecursiveMembers + { + public static int A(int x) + { + return B(x); + } + + public static int B(int x) + { + if (x <= 0) + { + return 0; + } + + return A(x - 1); + } + } + + public struct StructTypeWithRecursiveMembers + { + public int x; + + public int Fib() + { + if (x <= 0) + { + return x; + } + + x--; + + return Fib(); + } + } + + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly struct ShaderWithRecursiveMembers : IComputeShader + { + public readonly ReadWriteBuffer results; + + public void Execute() + { + results[0] = ClassWithRecursiveMembers.A(42); + + StructTypeWithRecursiveMembers instance; + instance.x = 42; + + results[1] = instance.Fib(); + } + } + """; + + // This should be correctly handled by the HLSL rewriter, and not cause a StackOverflowException. + // The HLSL compilation is expected to fail because recursion is not actually supported: + // + // "error: recursive functions not allowed" + VerifyGeneratedDiagnostics(source, "CMPS0046"); + } + /// /// Verifies the output of a source generator. ///