diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.Diagnostics.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.Diagnostics.cs
index 38a70d07..9cea28a0 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.Diagnostics.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.Diagnostics.cs
@@ -171,21 +171,6 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return base.VisitUsingStatement(node);
}
- ///
- public override SyntaxNode? VisitVariableDeclarator(VariableDeclaratorSyntax node)
- {
- var updatedNode = (VariableDeclaratorSyntax)base.VisitVariableDeclarator(node)!;
-
- if (node.Initializer is null &&
- node.Parent is VariableDeclarationSyntax declaration &&
- SemanticModel.For(node).GetTypeInfo(declaration.Type).Type is ITypeSymbol { IsUnmanagedType: false } type)
- {
- Context.ReportDiagnostic(InvalidObjectDeclaration, node, type);
- }
-
- return updatedNode;
- }
-
///
public override SyntaxNode? VisitYieldStatement(YieldStatementSyntax node)
{
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
index b52a6974..d346d8f4 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
@@ -73,6 +73,25 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return updatedNode.ReplaceAndTrackType(updatedNode.Type, node.Type, SemanticModel.For(node), DiscoveredTypes);
}
+ ///
+ public override SyntaxNode? VisitVariableDeclaration(VariableDeclarationSyntax node)
+ {
+ var updatedNode = (VariableDeclarationSyntax)base.VisitVariableDeclaration(node)!;
+
+ if (SemanticModel.For(node).GetTypeInfo(node.Type).Type is ITypeSymbol { IsUnmanagedType: false } type)
+ {
+ Context.ReportDiagnostic(InvalidObjectDeclaration, node, type);
+ }
+
+ // If var is used, replace it with the explicit type
+ if (updatedNode.Type is IdentifierNameSyntax { IsVar: true })
+ {
+ updatedNode = updatedNode.ReplaceAndTrackType(updatedNode.Type, node.Type, SemanticModel.For(node), DiscoveredTypes);
+ }
+
+ return updatedNode;
+ }
+
///
public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
index 42be1d7c..55d7f58d 100644
--- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
+++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
@@ -283,7 +283,7 @@ namespace ComputeSharp.Tests.SourceGenerators
}
}";
- VerifyGeneratedDiagnostics(source, "CMPS0010");
+ VerifyGeneratedDiagnostics(source, "CMPS0010", "CMPS0031");
}
[TestMethod]
@@ -310,7 +310,7 @@ namespace ComputeSharp.Tests.SourceGenerators
}
}";
- VerifyGeneratedDiagnostics(source, "CMPS0010");
+ VerifyGeneratedDiagnostics(source, "CMPS0010", "CMPS0031");
}
[TestMethod]
@@ -337,7 +337,7 @@ namespace ComputeSharp.Tests.SourceGenerators
}
}";
- VerifyGeneratedDiagnostics(source, "CMPS0011");
+ VerifyGeneratedDiagnostics(source, "CMPS0011", "CMPS0031");
}
[TestMethod]
@@ -894,7 +894,7 @@ namespace ComputeSharp.Tests.SourceGenerators
}
}";
- VerifyGeneratedDiagnostics(source, "CMPS0029");
+ VerifyGeneratedDiagnostics(source, "CMPS0029", "CMPS0031");
}
[TestMethod]
@@ -923,7 +923,7 @@ namespace ComputeSharp.Tests.SourceGenerators
}
}";
- VerifyGeneratedDiagnostics(source, "CMPS0029");
+ VerifyGeneratedDiagnostics(source, "CMPS0029", "CMPS0031");
}
[TestMethod]
diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
index 50e72d30..b667e00a 100644
--- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
+++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
@@ -171,5 +171,26 @@ namespace ComputeSharp.Tests
return new(1, 1, 1, 1);
}
}
+
+ [AutoConstructor]
+ public readonly partial struct LoopWithVarCounterShader : IComputeShader
+ {
+ public readonly ReadWriteBuffer buffer;
+
+ ///
+ public void Execute()
+ {
+ for (var i = 0; i < 10; i++)
+ {
+ buffer[ThreadIds.X * 10 + i] = i;
+ }
+ }
+ }
+
+ [TestMethod]
+ public void LoopWithVarCounter()
+ {
+ ReflectionServices.GetShaderInfo(out var info);
+ }
}
}