diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 00f1714f..04a9263a 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -547,6 +547,11 @@ internal sealed partial class ShaderSourceRewriter( return updatedNode; } + // Add a marker to avoid endless recursion in case we have any loops in the graph. + // This will be updated right after rewriting this method completes. This temporary + // value is fine, as the actual rewritten method is not used while rewriting HLSL. + this.staticMethods.Add(method, null!); + ShaderSourceRewriter shaderSourceRewriter = new( this.shaderType, SemanticModel, @@ -561,7 +566,7 @@ internal sealed partial class ShaderSourceRewriter( MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia(); - this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier))); + this.staticMethods[method] = processedMethod.WithIdentifier(Identifier(methodIdentifier)); } return updatedNode.WithExpression(IdentifierName(methodIdentifier)); @@ -602,11 +607,13 @@ internal sealed partial class ShaderSourceRewriter( return updatedNode; } + this.instanceMethods[method] = null!; + ShaderSourceRewriter shaderSourceRewriter = new( this.shaderType, SemanticModel, DiscoveredTypes, - this.instanceMethods, + this.staticMethods, this.instanceMethods, this.constructors, ConstantDefinitions, @@ -616,7 +623,7 @@ internal sealed partial class ShaderSourceRewriter( MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia(); - this.instanceMethods.Add(method, processedMethod); + this.instanceMethods[method] = processedMethod; } return updatedNode; @@ -691,11 +698,13 @@ internal sealed partial class ShaderSourceRewriter( Diagnostics.Add(InvalidBaseConstructorDeclaration, node, constructor); } + this.constructors[constructor] = default!; + ShaderSourceRewriter shaderSourceRewriter = new( this.shaderType, SemanticModel, DiscoveredTypes, - this.instanceMethods, + this.staticMethods, this.instanceMethods, this.constructors, ConstantDefinitions, @@ -757,7 +766,7 @@ internal sealed partial class ShaderSourceRewriter( .WithParameterList(processedMethod.ParameterList) .WithBody(processedMethod.Body); - this.constructors.Add(constructor, (stubNode, ctorNode)); + this.constructors[constructor] = (stubNode, ctorNode); } // Rewrite the expression to invoke the rewritten constructor: 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. ///