-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test and document Span breaking changes (#75079)
* Test more breaking changes * Document the main breaking changes * Improve the doc
- Loading branch information
Showing
3 changed files
with
225 additions
and
11 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
docs/compilers/CSharp/Compiler Breaking Changes - DotNet 10.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This document lists known breaking changes in Roslyn after .NET 9 all the way to .NET 10. | ||
|
||
## `Span<T>` and `ReadOnlySpan<T>` overloads are applicable in more scenarios in C# 14 and newer | ||
|
||
***Introduced in Visual Studio 2022 version 17.13*** | ||
|
||
C# 14 introduces new [built-in span conversions and type inference rules](https://github.com/dotnet/csharplang/issues/7905). | ||
This means that different overloads might be chosen compared to C# 13, and sometimes an ambiguity compile-time error | ||
might be raised because a new overload is applicable but there is no single best overload. | ||
|
||
The following example shows some ambiguities and possible workarounds. | ||
Note that another workaround is for API authors to use the `OverloadResolutionPriorityAttribute`. | ||
|
||
```cs | ||
var x = new long[] { 1 }; | ||
Assert.Equal([2], x); // previously Assert.Equal<T>(T[], T[]), now ambiguous with Assert.Equal<T>(ReadOnlySpan<T>, Span<T>) | ||
Assert.Equal([2], x.AsSpan()); // workaround | ||
var y = new int[] { 1, 2 }; | ||
var s = new ArraySegment<int>(x, 1, 1); | ||
Assert.Equal(y, s); // previously Assert.Equal<T>(T, T), now ambiguous with Assert.Equal<T>(Span<T>, Span<T>) | ||
Assert.Equal(y.AsSpan(), s); // workaround | ||
``` | ||
|
||
A `Span<T>` overload might be chosen in C# 14 where an overload taking an interface implemented by `T[]` (such as `IEnumerable<T>`) was chosen in C# 13, | ||
and that can lead to an `ArrayTypeMismatchException` at runtime if used with a covariant array: | ||
|
||
```cs | ||
string[] s = new[] { "a" }; | ||
object[] o = s; // array variance | ||
C.R(o); // wrote 1 previously, now crashes in Span<T> constructor with ArrayTypeMismatchException | ||
C.R(o.AsEnumerable()); // workaround | ||
static class C | ||
{ | ||
public static void R<T>(IEnumerable<T> e) => Console.Write(1); | ||
public static void R<T>(Span<T> s) => Console.Write(2); | ||
// another workaround: | ||
[OverloadResolutionPriority(1)] | ||
public static void R<T>(ReadOnlySpan<T> s) => Console.Write(3); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters