From 784a29cbe7d7979e77f2d2dca15a99037a704aa9 Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Mon, 26 Sep 2022 17:09:14 +0200 Subject: [PATCH] Update ref foreach tests --- .../Test/Semantic/Semantics/RefFieldTests.cs | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs index 0d7ef071bc5ea..4dc3d56d4fe26 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs @@ -11393,12 +11393,12 @@ static void Main() { int i1 = 1; foreach (S s1 in Enumerable1.Create(ref i1)) { - s0 = s1; // <-- An error is expected here, see https://github.com/dotnet/roslyn/issues/64218 + s0 = s1; break; } } foreach (scoped S s2 in Enumerable1.Create(s0)) { - s0 = s2; // 2 + s0 = s2; // 1 break; } foreach (S s3 in Enumerable1.Create(s0)) { @@ -11428,7 +11428,7 @@ class Enumerator1 var comp = CreateCompilation(source); comp.VerifyDiagnostics( // (15,18): error CS8352: Cannot use variable 's2' in this context because it may expose referenced variables outside of their declaration scope - // s0 = s2; // 2 + // s0 = s2; // 1 Diagnostic(ErrorCode.ERR_EscapeVariable, "s2").WithArguments("s2").WithLocation(15, 18) ); } @@ -11489,6 +11489,49 @@ class Enumerator1 ); } + [Fact] + [WorkItem(64218, "https://github.com/dotnet/roslyn/issues/64218")] + public void LocalScope_12_Foreach_03() + { + var source = +@"class Program +{ + static void Main() + { + int i0 = 0; + S s0 = new S(ref i0); + { + int i1 = 1; + foreach (S s1 in Enumerable1.Create(ref i1)) { + s0 = s1; + break; + } + } + } +} +ref struct S +{ + public S(ref int i) { } +} +ref struct Enumerable1 +{ + public static Enumerable1 Create(ref int p) => default; + public Enumerator1 GetEnumerator() => default; +} +ref struct Enumerator1 +{ + public S Current => throw null; + public bool MoveNext() => false; +} +"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (10,22): error CS8352: Cannot use variable 's1' in this context because it may expose referenced variables outside of their declaration scope + // s0 = s1; + Diagnostic(ErrorCode.ERR_EscapeVariable, "s1").WithArguments("s1").WithLocation(10, 22) + ); + } + [Fact] public void ScopedRefAndRefStructOnly_06_Foreach() {