-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix SafeContext of Span-valued collection expressions to match specification #80684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RikkiGibson
merged 12 commits into
main
from
copilot/fix-safecontext-of-span-collection
Oct 22, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
da1e30d
Initial plan
Copilot fac1ebd
Fix SafeContext of Span-valued collection expressions to match specif…
Copilot 9881b26
Add test case for issue example and verify all tests pass
Copilot 902f061
Add breaking changes documentation for DotNet 11
Copilot 05226c9
Update Compiler Breaking Changes - DotNet 11.md
RikkiGibson ea8b11d
Update Compiler Breaking Changes - DotNet 11.md
RikkiGibson 170bf02
Add tests for scoped parameter assignment in constructors vs methods
Copilot bad0591
Fix ref-safe-context of locals in top-level block of constructors
RikkiGibson bad3524
Alternative solution to constructor locals issue
RikkiGibson 1ffc591
Fix some edge cases
RikkiGibson 7b78b2f
Extract constructor safe-context fix from this PR
RikkiGibson 8b0c05c
doc other alternative
RikkiGibson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
docs/compilers/CSharp/Compiler Breaking Changes - DotNet 11.md
This file contains hidden or 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,61 @@ | ||
| # Breaking changes in Roslyn after .NET 10.0.100 through .NET 11.0.100 | ||
|
|
||
| This document lists known breaking changes in Roslyn after .NET 10 general release (.NET SDK version 10.0.100) through .NET 11 general release (.NET SDK version 11.0.100). | ||
|
|
||
| ## The *safe-context* of a collection expression of Span/ReadOnlySpan type is now *declaration-block* | ||
|
|
||
| ***Introduced in Visual Studio 2026 version 18.3*** | ||
|
|
||
| The C# compiler made a breaking change in order to properly adhere to the [ref safety rules](https://github.com/dotnet/csharplang/blob/main/proposals/csharp-12.0/collection-expressions.md#ref-safety) in the *collection expressions* feature specification. Specifically, the following clause: | ||
|
|
||
| > * If the target type is a *span type* `System.Span<T>` or `System.ReadOnlySpan<T>`, the safe-context of the collection expression is the *declaration-block*. | ||
|
|
||
| Previously, the compiler used safe-context *function-member* in this situation. We have now made a change to use *declaration-block* per the specification. This can cause new errors to appear in existing code, such as in the scenario below: | ||
|
|
||
| ```cs | ||
| scoped Span<int> items1 = default; | ||
| scoped Span<int> items2 = default; | ||
| foreach (var x in new[] { 1, 2 }) | ||
| { | ||
| Span<int> items = [x]; | ||
| if (x == 1) | ||
| items1 = items; // previously allowed, now an error | ||
|
|
||
| if (x == 2) | ||
| items2 = items; // previously allowed, now an error | ||
| } | ||
| ``` | ||
|
|
||
| If your code is impacted by this breaking change, consider using an array type for the relevant collection expressions instead: | ||
|
|
||
| ```cs | ||
| scoped Span<int> items1 = default; | ||
| scoped Span<int> items2 = default; | ||
| foreach (var x in new[] { 1, 2 }) | ||
| { | ||
| int[] items = [x]; | ||
| if (x == 1) | ||
| items1 = items; // ok, using 'int[]' conversion to 'Span<int>' | ||
|
|
||
| if (x == 2) | ||
| items2 = items; // ok | ||
| } | ||
| ``` | ||
|
|
||
| Alternatively, move the collection-expression to a scope where the assignment is permitted: | ||
| ```cs | ||
| scoped Span<int> items1 = default; | ||
| scoped Span<int> items2 = default; | ||
| Span<int> items = [0]; | ||
| foreach (var x in new[] { 1, 2 }) | ||
| { | ||
| items[0] = x; | ||
| if (x == 1) | ||
| items1 = items; // ok | ||
|
|
||
| if (x == 2) | ||
| items2 = items; // ok | ||
| } | ||
| ``` | ||
|
|
||
| See also https://github.com/dotnet/csharplang/issues/9750. | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version is not out yet, but, assuming we do not backport to 10.0.100, this is when we expect the fix to reach users.