diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs index 2b7a746835b210db583e5f9f82231bafe23e6372..f146d97c13e0570bdd7074cfd5820dd5cdbb54d1 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs @@ -930,6 +930,47 @@ .locals init (int? V_0) IL_0018: ldstr ""null"" IL_001d: call ""void System.Console.Write(string)"" IL_0022: ret +}"); + } + + [Fact, WorkItem(19153, "https://github.com/dotnet/roslyn/issues/19153")] + public void RedundantBox() + { + var source = @"using System; +public class C +{ + public static void M(U x) where T : U + { + // when T is not known to be a reference type, there is an unboxing conversion from + // a type parameter U to T, provided T depends on U. + switch (x) + { + case T i: + Console.Write(i); + break; + } + } +}"; + var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll); + compilation.VerifyDiagnostics(); + var compVerifier = CompileAndVerify(compilation); + compVerifier.VerifyIL("C.M(U)", +@"{ + // Code size 37 (0x25) + .maxstack 1 + .locals init (U V_0) + IL_0000: ldarg.0 + IL_0001: stloc.0 + IL_0002: ldloc.0 + IL_0003: box ""U"" + IL_0008: isinst ""T"" + IL_000d: brfalse.s IL_0024 + IL_000f: ldloc.0 + IL_0010: box ""U"" + IL_0015: unbox.any ""T"" + IL_001a: box ""T"" + IL_001f: call ""void System.Console.Write(object)"" + IL_0024: ret }"); } }