Fix array assertion overload ambiguity with older C# versions - #11038
Fix array assertion overload ambiguity with older C# versions#11038Amaury Levé (Evangelink) wants to merge 3 commits into
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Predicate-based array calls remain ambiguous under C# 12.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review tier: Balanced
Findings: 2
New issues introduced by this change (2)
| Severity | Finding |
|---|---|
src/TestFramework/TestFramework/Assertions/Assert.Contains.cs — The exact-array overload only covers the item form. Under C# 12, `Func<int, bool> predicate = ...;… |
|
src/TestFramework/TestFramework/Assertions/Assert.DoesNotContain.cs — The predicate form remains unfixed: with C# 12, `Func<int, bool> predicate = ...;… |
What changed in this PR
Adds array-specific assertion overloads to avoid C# 12 overload ambiguity.
Changes:
- Adds item and comparer array overloads with API baselines.
- Adds unit and C# 12 acceptance coverage.
| File | Description |
|---|---|
Assert.Contains.cs |
Adds array forwarding overloads. |
Assert.DoesNotContain.cs |
Adds array forwarding overloads. |
PublicAPI.Unshipped.txt |
Tracks new public APIs. |
AssertTests.ContainsSpan.cs |
Tests array and comparer behavior. |
SdkTests.cs |
Verifies compilation under C# 12. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| public void TestMethod1() | ||
| { | ||
| int[] values = new[] { 1, 2, 3 }; | ||
| Assert.Contains(1, values); |
There was a problem hiding this comment.
Are we sure it's only arrays that cause issues here? We don't want to end up needing to grow the list of overloads if we discover more issues.
It might even be safest to take a breaking change to the very new feature. What was the motivation to add the overloads?
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The compatibility guard omits attributed public Assert method families, making its completeness check inaccurate.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review tier: Balanced
Findings: 1
New issues introduced by this change (1)
| Severity | Finding |
|---|---|
test/IntegrationTests/MSTest.Acceptance.IntegrationTests/AssertSourceCompatibilityTests.cs — This regex skips attributed API entries because it requires the line to begin with static. The… |
Issues resolved since last review (2)
| Severity | Finding |
|---|---|
src/TestFramework/TestFramework/Assertions/Assert.DoesNotContain.cs — The predicate form remains unfixed: with C# 12, `Func<int, bool> predicate = ...;… View resolved comment |
|
src/TestFramework/TestFramework/Assertions/Assert.Contains.cs — The exact-array overload only covers the item form. Under C# 12, `Func<int, bool> predicate = ...;… View resolved comment |
| private static readonly Regex PublicAssertMethodRegex = new( | ||
| @"^static Microsoft\.VisualStudio\.TestTools\.UnitTesting\.Assert\.(?<name>[A-Za-z0-9]+)(?:<|\()", | ||
| RegexOptions.CultureInvariant); |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
The broad public overload-resolution changes warrant final human API review despite comprehensive compatibility coverage.
Review tier: Balanced
Findings: 1
Pre-existing issues (1)
| Severity | Finding |
|---|---|
test/IntegrationTests/MSTest.Acceptance.IntegrationTests/AssertSourceCompatibilityTests.cs — This regex skips attributed API entries because it requires the line to begin with static. The… View comment |


Summary
Assertfamilies so C# 12 consumers, including explicit generic callers, do not encounter ambiguities betweenIEnumerable<T>and span overloadsIEnumerable<T>implementations, includingstring,ArraySegment<T>, mixed collection types, and user-defined span-convertible enumerablesIEnumerable<T>implementationsAssertmethod family and dual-conversion categoryFixes #11022
Compatibility scope
Exact array overloads cover both inferred and explicit
<T>calls for the reported/common case. Constrained forwarders cover inferred calls for arbitrary enumerable collection types. Explicit<T>calls for non-array dual-convertible types cannot use the additional inferred collection type parameter and would require an unscalable set of per-type overloads.Validation
AssertSourceCompatibilityTestspackage-consumer oracle passedArraySegment<T>,ImmutableArray<T>, collection expressions, mixed types, and a custom dual-convertible enumerable