Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ public override SyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node)
{
elementType = arrayTypeSymbol2.ElementType.GloballyQualified();
}
// Check parent cast expression for array type context
// Handles cases like: (MyEnum[])[MyEnum.One, MyEnum.Two]
else if (node.Parent is CastExpressionSyntax castExpr)
{
var castTypeInfo = semanticModel.GetTypeInfo(castExpr.Type);
if (castTypeInfo.Type is IArrayTypeSymbol castArrayType)
{
elementType = castArrayType.ElementType.GloballyQualified();
}
}
// Infer element type from first element if still unknown
// Handles cases where semantic model doesn't provide array type info
else if (node.Elements.Count > 0 && node.Elements[0] is ExpressionElementSyntax firstElement)
{
var elementTypeInfo = semanticModel.GetTypeInfo(firstElement.Expression);
if (elementTypeInfo.Type is ITypeSymbol inferredType)
{
elementType = inferredType.GloballyQualified();
}
}

// Visit and rewrite each element
var rewrittenElements = new List<ExpressionSyntax>();
Expand Down
38 changes: 38 additions & 0 deletions TUnit.TestProject/Bugs/4065/BugRepro4065.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using TUnit.TestProject.Attributes;

namespace TUnit.TestProject.Bugs._4065;

public enum MyEnum
{
One,
Two,
Three
}

[EngineTest(ExpectedResult.Pass)]
public class BugRepro4065
{
[Test]
[Arguments((MyEnum[])[MyEnum.One, MyEnum.Two])]
public async Task EnumArrayWithCollectionExpression(MyEnum[] values)
{
await Assert.That(values.Length).IsEqualTo(2);
await Assert.That(values[0]).IsEqualTo(MyEnum.One);
await Assert.That(values[1]).IsEqualTo(MyEnum.Two);
}

[Test]
[Arguments((MyEnum[])[MyEnum.Three])]
public async Task EnumArraySingleElement(MyEnum[] values)
{
await Assert.That(values.Length).IsEqualTo(1);
await Assert.That(values[0]).IsEqualTo(MyEnum.Three);
}

[Test]
[Arguments(new MyEnum[] { MyEnum.One, MyEnum.Two })] // Old syntax (should still work)
public async Task EnumArrayWithNewSyntax(MyEnum[] values)
{
await Assert.That(values.Length).IsEqualTo(2);
}
}
Comment on lines +1 to +38
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug fix is missing a snapshot test in TUnit.Core.SourceGenerator.Tests. Based on the project's pattern (as seen in bugs 1538, 2085, etc.), there should be a corresponding test file at TUnit.Core.SourceGenerator.Tests/Bugs/4065/Tests4065.cs that verifies the generated code output matches expectations.

The snapshot test should:

  1. Extend TestsBase
  2. Call RunTest with the path to this test file
  3. Generate a .verified.txt file that captures the generated source code

This is critical because it ensures the source generator produces correct output for casted enum arrays and prevents future regressions. See TUnit.Core.SourceGenerator.Tests/Bugs/2085/Tests2085.cs as an example pattern to follow.

Copilot uses AI. Check for mistakes.
Loading