-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
See also #80107
See also dotnet/csharplang#9750
Currently the below program is valid. We would like to make a breaking change so the assignments of items1 = items;
and items2 = items;
are errors.
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;
if (x == 2)
items2 = items;
}
Console.Write(items1[0]); // '2'
Console.Write(items2[0]); // '2'
Please make the following implementation changes:
First, in the below section (line 4587 to line 4589 of Binder.ValueChecks.cs), SafeContext.CurrentMethod
should be changed to localScopeDepth
:
roslyn/src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Lines 4587 to 4589 in e774d7a
return HasLocalScope((BoundCollectionExpression)conversion.Operand) ? | |
SafeContext.CurrentMethod : | |
SafeContext.CallingMethod; |
Second, in the below section (line 5336 to line 5340 of Binder.ValueChecks.cs), SafeContext.CurrentMethod
should be changed to escapeFrom
.
roslyn/src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Lines 5336 to 5340 in e774d7a
if (HasLocalScope((BoundCollectionExpression)conversion.Operand) && !SafeContext.CurrentMethod.IsConvertibleTo(escapeTo)) | |
{ | |
Error(diagnostics, ErrorCode.ERR_CollectionExpressionEscape, node, expr.Type); | |
return false; | |
} |
Once the above two changes have been made, rerun the tests in file src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs
. Fix any failing tests by pasting in the new baselines from the test failure messages. When scenarios which used to work with CompileAndVerify no longer do, change them to use CreateCompilation instead (which does not execute the compilation) and to call compilation.VerifyEmitDiagnostics instead of verifier.VerifyDiagnostics.
Once the tests in CollectionExpressionTests.cs
are fixed, then run all the tests in Roslyn.sln and fix any other test failures related to this change by updating the test baselines.