You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// <param name="adjustDepth">When true, narrows <see cref="_localScopeDepth"/> when the instance is created, and widens it when the instance is disposed.</param>
// (5,48): error CS8347: Cannot use a result of 'ReadOnlySpan<int>.ReadOnlySpan(ref readonly int)' in this context because it may expose variables referenced by parameter 'reference' outside of their declaration scope
26636
+
// private int x = M0(M1(out int x1), items = new ReadOnlySpan<int>(ref x1)); // 2, 3
26637
+
Diagnostic(ErrorCode.ERR_EscapeCall, "new ReadOnlySpan<int>(ref x1)").WithArguments("System.ReadOnlySpan<int>.ReadOnlySpan(ref readonly int)", "reference").WithLocation(5, 48),
26638
+
// (5,74): error CS8352: Cannot use variable 'x1' in this context because it may expose referenced variables outside of their declaration scope
26639
+
// private int x = M0(M1(out int x1), items = new ReadOnlySpan<int>(ref x1)); // 2, 3
public void SpanAssignment_StaticFieldInitializer()
26648
+
{
26649
+
// Each field initializer can be considered its own local scope as variables declared within it are not visible to the other initializers.
26650
+
string source = """
26651
+
using System;
26652
+
26653
+
class C
26654
+
{
26655
+
private static int x = M0(M1(out ReadOnlySpan<int> items, out int x1), items = new ReadOnlySpan<int>(ref x1)); // 1, 2
26656
+
private static int y = items[0]; // 3
26657
+
26658
+
static int M0(int x0, ReadOnlySpan<int> items) => items[0];
26659
+
static int M1(out ReadOnlySpan<int> span, out int x1) { span = default; return x1 = 0; }
26660
+
}
26661
+
""";
26662
+
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net80);
26663
+
comp.VerifyEmitDiagnostics(
26664
+
// (5,84): error CS8347: Cannot use a result of 'ReadOnlySpan<int>.ReadOnlySpan(ref readonly int)' in this context because it may expose variables referenced by parameter 'reference' outside of their declaration scope
26665
+
// private static int x = M0(M1(out ReadOnlySpan<int> items, out int x1), items = new ReadOnlySpan<int>(ref x1)); // 1, 2
26666
+
Diagnostic(ErrorCode.ERR_EscapeCall, "new ReadOnlySpan<int>(ref x1)").WithArguments("System.ReadOnlySpan<int>.ReadOnlySpan(ref readonly int)", "reference").WithLocation(5, 84),
26667
+
// (5,110): error CS8168: Cannot return local 'x1' by reference because it is not a ref local
26668
+
// private static int x = M0(M1(out ReadOnlySpan<int> items, out int x1), items = new ReadOnlySpan<int>(ref x1)); // 1, 2
// (5,32): error CS9203: A collection expression of type 'ReadOnlySpan<int>' cannot be used in this context because it may be exposed outside of the current scope.
0 commit comments