Skip to content

Conversation

@thomhurst
Copy link
Owner

Fixes #4065

Copilot AI review requested due to automatic review settings December 12, 2025 12:16
@claude
Copy link
Contributor

claude bot commented Dec 12, 2025

Code Review

Summary

This PR fixes issue #4065 where casted enum arrays in the Arguments attribute cause source generator compilation failures. The fix enhances the VisitCollectionExpression method to properly infer element types from parent cast expressions and from the collection elements themselves.

Strengths

  1. Root Cause Fixed: The fix correctly addresses the issue where (MyEnum[])[MyEnum.One, MyEnum.Two] was being generated as new object[] instead of new MyEnum[].

  2. Comprehensive Test Coverage: The bug repro test file covers three scenarios - multi-element enum array, single-element enum array, and backward compatibility with old syntax.

  3. Layered Fallback Logic: The fix implements a sensible priority order - check typeInfo.ConvertedType, then typeInfo.Type, then parent cast expression (NEW), then infer from first element (NEW).

  4. Clear Comments: The code includes helpful comments explaining each fallback case.

Observations and Recommendations

Minor: Potential Edge Cases

  1. Empty Collection Expressions with Cast: What happens with (MyEnum[])[]? The fourth fallback checks node.Elements.Count > 0, but if all previous checks fail, it would fall back to object. Consider adding a test case for empty arrays.

  2. Null Safety: Line 242 accesses elementTypeInfo.Type without null check. While likely safe, consider adding a check for IErrorTypeSymbol to prevent using error types if the first element has compilation errors.

Performance: Hot Path Consideration

The VisitCollectionExpression method is in a hot path (source generation). The current implementation is good - checks are ordered by likelihood and early exits when element type is found. The List allocation is acceptable given this is a syntax rewriter already allocating extensively.

Code Style: Fully Compliant

Modern C# syntax, clear variable names, proper use of braces, and comments explain the why.

Testing Verification Needed

Per TUnit CLAUDE.md guidelines:

  1. Snapshot Tests: Did this change affect source generator output? Ensure dotnet test TUnit.Core.SourceGenerator.Tests was run and any .verified.txt files are committed.

  2. Dual-Mode Verification: This appears to be source-generator-only. Verify reflection mode handles this syntax correctly or confirm it does not encounter it.

  3. Integration Tests: The BugRepro4065.cs tests should be run with filter to verify all three test cases pass.

Pre-Commit Checklist

  • All tests pass
  • Source generator tests run
  • Snapshot files reviewed and committed if applicable
  • Bug repro tests pass with filter
  • AOT compatibility maintained (no new reflection added)

Verdict

Recommendation: Approve with minor suggestions

The fix is sound and directly addresses the reported issue. Consider: (1) adding a test case for empty enum arrays, (2) optional null/error type check on line 242, (3) verify snapshot tests were run.

Great work tracking down this edge case in the cast expression handling!

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #4065 where casted enum arrays in Arguments attributes (e.g., (MyEnum[])[MyEnum.One, MyEnum.Two]) caused source generator compilation failures. The fix enhances the collection expression visitor in the source generator to handle additional scenarios for determining array element types.

Key Changes

  • Added logic to extract element type from parent cast expressions when processing collection expressions
  • Added fallback logic to infer element type from the first element when semantic model doesn't provide type information
  • Added integration tests to verify casted enum arrays work correctly with the Arguments attribute

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
TUnit.TestProject/Bugs/4065/BugRepro4065.cs Adds integration tests for casted enum array arguments using both collection expression syntax and traditional array initialization
TUnit.Core.SourceGenerator/CodeGenerators/Helpers/FullyQualifiedWithGlobalPrefixRewriter.cs Enhances VisitCollectionExpression to handle cast expressions and infer element types when semantic model doesn't provide them

Comment on lines +1 to +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);
}
}
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.
@thomhurst thomhurst enabled auto-merge (squash) December 12, 2025 12:31
@thomhurst thomhurst disabled auto-merge December 12, 2025 12:50
@thomhurst thomhurst merged commit 9a721c4 into main Dec 12, 2025
20 checks passed
@thomhurst thomhurst deleted the bug/4065 branch December 12, 2025 12:50
@claude claude bot mentioned this pull request Dec 12, 2025
1 task
This was referenced Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Compile error when using Arguments Attribute with a enum array

2 participants