Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Nov 11, 2024
1 parent 7dd4295 commit 641edc0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6089,6 +6089,7 @@ public static async System.Threading.Tasks.Task<int> ProduceAsync(bool b, System
}
}
""";
// Note: nested hoisted local gets cleared when exiting nested scope normally
CompileAndVerify(src, expectedOutput: ExpectedOutput("value True"), targetFramework: TargetFramework.Net90, verify: Verification.Skipped).VerifyDiagnostics();
}

Expand Down
49 changes: 48 additions & 1 deletion src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenIterators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3114,7 +3114,7 @@ public static System.Collections.Generic.IEnumerable<int> M(bool b)
}
}
""";
// Note: nested hoisted local does not get cleared when exiting normally
// Note: nested hoisted local gets cleared when exiting normally
CompileAndVerify(src, expectedOutput: "42 value True").VerifyDiagnostics();
}

Expand Down Expand Up @@ -3323,5 +3323,52 @@ .maxstack 0
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75666")]
public void AddVariableCleanup_NestedUnmanagedWithGenericsLocal()
{
var src = """
using System.Reflection;
var enumerable = C.M(true, 42);
var enumerator = enumerable.GetEnumerator();
try
{
assert(enumerator.MoveNext());
System.Console.Write($"{enumerator.Current} ");
assert(!enumerator.MoveNext());
System.Console.Write(((S<int>)enumerator.GetType().GetField("<s>5__2", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(enumerator)).field);
}
finally
{
enumerator.Dispose();
}
void assert(bool b)
{
if (!b) throw new System.Exception();
}
public struct S<T> where T : unmanaged // UnmanagedWithGenerics
{
public T field;
}
public class C
{
public static System.Collections.Generic.IEnumerable<int> M<T>(bool b, T t) where T : unmanaged
{
while (b)
{
S<T> s = new S<T> { field = t };
yield return 42;
b = false;
System.Console.Write(s.field);
}
}
}
""";
CompileAndVerify(src, expectedOutput: "42 4242").VerifyDiagnostics();
}
}
}

0 comments on commit 641edc0

Please sign in to comment.