PatternTests.cs 108.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Linq;
using Microsoft.CodeAnalysis.CodeGen;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
{
    public class PatternTests : EmitMetadataTestBase
    {
        [Fact, WorkItem(18811, "https://github.com/dotnet/roslyn/issues/18811")]
        public void MissingNullable_01()
        {
            var source = @"namespace System {
    public class Object { }
    public abstract class ValueType { }
    public struct Void { }
    public struct Boolean { }
    public struct Int32 { }
}
static class C {
    public static bool M() => ((object)123) is int i;
}
";
J
Jared Parsons 已提交
30
            var compilation = CreateEmptyCompilation(source, options: TestOptions.ReleaseDll);
31 32 33
            compilation.GetDiagnostics().Verify();
            compilation.GetEmitDiagnostics().Verify(
                // warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
34
                Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
                );
        }

        [Fact, WorkItem(18811, "https://github.com/dotnet/roslyn/issues/18811")]
        public void MissingNullable_02()
        {
            var source = @"namespace System {
    public class Object { }
    public abstract class ValueType { }
    public struct Void { }
    public struct Boolean { }
    public struct Int32 { }
    public struct Nullable<T> where T : struct { }
}
static class C {
    public static bool M() => ((object)123) is int i;
}
";
J
Jared Parsons 已提交
53
            var compilation = CreateEmptyCompilation(source, options: TestOptions.UnsafeReleaseDll);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            compilation.GetDiagnostics().Verify();
            compilation.GetEmitDiagnostics().Verify(
                // warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
                Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion)
                );
        }

        [Fact]
        public void MissingNullable_03()
        {
            var source = @"namespace System {
    public class Object { }
    public abstract class ValueType { }
    public struct Void { }
    public struct Boolean { }
    public struct Int32 { }
    public struct Nullable<T> where T : struct { }
}
static class C {
    static void M1(int? x)
    {
        switch (x)
        {
            case int i: break;
        }
    }
    static bool M2(int? x) => x is int i;
}
";
J
Jared Parsons 已提交
83
            var compilation = CreateEmptyCompilation(source, options: TestOptions.UnsafeReleaseDll);
84 85 86 87
            compilation.GetDiagnostics().Verify();
            compilation.GetEmitDiagnostics().Verify(
                // warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
                Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1),
88 89 90
                // (14,18): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
                //             case int i: break;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(14, 18),
91 92 93
                // (14,18): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //             case int i: break;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(14, 18),
94 95 96
                // (14,18): error CS0656: Missing compiler required member 'System.Nullable`1.get_Value'
                //             case int i: break;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_Value").WithLocation(14, 18),
97 98 99
                // (17,36): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
                //     static bool M2(int? x) => x is int i;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(17, 36),
100 101 102
                // (17,36): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //     static bool M2(int? x) => x is int i;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(17, 36),
103
                // (17,36): error CS0656: Missing compiler required member 'System.Nullable`1.get_Value'
104
                //     static bool M2(int? x) => x is int i;
105
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_Value").WithLocation(17, 36)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
                );
        }

        [Fact]
        public void MissingNullable_04()
        {
            var source = @"namespace System {
    public class Object { }
    public abstract class ValueType { }
    public struct Void { }
    public struct Boolean { }
    public struct Int32 { }
    public struct Nullable<T> where T : struct { public T GetValueOrDefault() => default(T); }
}
static class C {
    static void M1(int? x)
    {
        switch (x)
        {
            case int i: break;
        }
    }
    static bool M2(int? x) => x is int i;
}
";
J
Jared Parsons 已提交
131
            var compilation = CreateEmptyCompilation(source, options: TestOptions.UnsafeReleaseDll);
132 133 134
            compilation.GetDiagnostics().Verify();
            compilation.GetEmitDiagnostics().Verify(
                // warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
135
                Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1),
136 137 138
                // (14,18): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
                //             case int i: break;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(14, 18),
139 140
                // (17,36): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
                //     static bool M2(int? x) => x is int i;
141
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(17, 36)
142 143
                );
        }
144

145
        [Fact, WorkItem(17266, "https://github.com/dotnet/roslyn/issues/17266")]
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        public void DoubleEvaluation01()
        {
            var source =
@"using System;
public class C
{
    public static void Main()
    {
        if (TryGet() is int index)
        {
            Console.WriteLine(index);
        }
    }

    public static int? TryGet()
    {
        Console.WriteLine(""eval"");
        return null;
    }
}";
J
Jared Parsons 已提交
166
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
167 168 169 170 171
            compilation.VerifyDiagnostics();
            var expectedOutput = @"eval";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("C.Main",
@"{
172
  // Code size       42 (0x2a)
173 174 175
  .maxstack  1
  .locals init (int V_0, //index
                bool V_1,
176
                int? V_2)
177 178 179 180
  IL_0000:  nop
  IL_0001:  call       ""int? C.TryGet()""
  IL_0006:  stloc.2
  IL_0007:  ldloca.s   V_2
181
  IL_0009:  call       ""bool int?.HasValue.get""
182
  IL_000e:  brfalse.s  IL_001b
183 184
  IL_0010:  ldloca.s   V_2
  IL_0012:  call       ""int int?.GetValueOrDefault()""
185 186 187 188 189 190 191 192 193 194 195 196 197
  IL_0017:  stloc.0
  IL_0018:  ldc.i4.1
  IL_0019:  br.s       IL_001c
  IL_001b:  ldc.i4.0
  IL_001c:  stloc.1
  IL_001d:  ldloc.1
  IL_001e:  brfalse.s  IL_0029
  IL_0020:  nop
  IL_0021:  ldloc.0
  IL_0022:  call       ""void System.Console.WriteLine(int)""
  IL_0027:  nop
  IL_0028:  nop
  IL_0029:  ret
198
}");
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

            compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("C.Main",
@"{
  // Code size       30 (0x1e)
  .maxstack  1
  .locals init (int V_0, //index
                int? V_1)
  IL_0000:  call       ""int? C.TryGet()""
  IL_0005:  stloc.1
  IL_0006:  ldloca.s   V_1
  IL_0008:  call       ""bool int?.HasValue.get""
  IL_000d:  brfalse.s  IL_001d
  IL_000f:  ldloca.s   V_1
  IL_0011:  call       ""int int?.GetValueOrDefault()""
  IL_0016:  stloc.0
  IL_0017:  ldloc.0
  IL_0018:  call       ""void System.Console.WriteLine(int)""
  IL_001d:  ret
}");
221 222
        }

223
        [Fact, WorkItem(19122, "https://github.com/dotnet/roslyn/issues/19122")]
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        public void PatternCrash_01()
        {
            var source = @"using System;
using System.Collections.Generic;
using System.Linq;

public class Class2 : IDisposable
{
    public Class2(bool parameter = false)
    {
    }

    public void Dispose()
    {
    }
}

class X<T>
{
    IdentityAccessor<T> idAccessor = new IdentityAccessor<T>();
    void Y<U>() where U : T
    {
        // BUG: The following line is the problem
        if (GetT().FirstOrDefault(p => idAccessor.GetId(p) == Guid.Empty) is U u)
        {
        }
    }

    IEnumerable<T> GetT()
    {
        yield return default(T);
    }
}
class IdentityAccessor<T>
{
    public Guid GetId(T t)
    {
        return Guid.Empty;
    }
}";
264
            var compilation = CreateCompilation(source, options: TestOptions.DebugDll);
265 266 267 268
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("X<T>.Y<U>",
@"{
269
  // Code size       61 (0x3d)
270 271 272
  .maxstack  3
  .locals init (U V_0, //u
                bool V_1,
273
                T V_2)
274 275 276 277 278 279 280
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  call       ""System.Collections.Generic.IEnumerable<T> X<T>.GetT()""
  IL_0007:  ldarg.0
  IL_0008:  ldftn      ""bool X<T>.<Y>b__1_0<U>(T)""
  IL_000e:  newobj     ""System.Func<T, bool>..ctor(object, System.IntPtr)""
  IL_0013:  call       ""T System.Linq.Enumerable.FirstOrDefault<T>(System.Collections.Generic.IEnumerable<T>, System.Func<T, bool>)""
281 282
  IL_0018:  stloc.2
  IL_0019:  ldloc.2
283
  IL_001a:  box        ""T""
284
  IL_001f:  isinst     ""U""
285
  IL_0024:  brfalse.s  IL_0035
286 287 288
  IL_0026:  ldloc.2
  IL_0027:  box        ""T""
  IL_002c:  unbox.any  ""U""
289 290 291 292 293 294 295 296 297 298
  IL_0031:  stloc.0
  IL_0032:  ldc.i4.1
  IL_0033:  br.s       IL_0036
  IL_0035:  ldc.i4.0
  IL_0036:  stloc.1
  IL_0037:  ldloc.1
  IL_0038:  brfalse.s  IL_003c
  IL_003a:  nop
  IL_003b:  nop
  IL_003c:  ret
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
}");
        }

        [Fact, WorkItem(24522, "https://github.com/dotnet/roslyn/issues/24522")]
        public void IgnoreDeclaredConversion_01()
        {
            var source =
@"class Base<T>
{
    public static implicit operator Derived(Base<T> obj)
    {
        return new Derived();
    }
}

class Derived : Base<object>
{
}

class Program
{
    static void Main(string[] args)
    {
        Base<object> x = new Derived();
        System.Console.WriteLine(x is Derived);
        System.Console.WriteLine(x is Derived y);
        switch (x)
        {
            case Derived z: System.Console.WriteLine(true); break;
        }
        System.Console.WriteLine(null != (x as Derived));
    }
}";
J
Jared Parsons 已提交
332
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
333 334 335 336 337 338 339 340 341
            compilation.VerifyDiagnostics();
            var expectedOutput =
@"True
True
True
True";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("Program.Main",
@"{
342
  // Code size       84 (0x54)
343 344 345
  .maxstack  2
  .locals init (Base<object> V_0, //x
                Derived V_1, //y
346
                Derived V_2, //z
347 348
                Base<object> V_3,
                Base<object> V_4)
349 350 351 352 353 354 355 356 357 358
  IL_0000:  nop
  IL_0001:  newobj     ""Derived..ctor()""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  isinst     ""Derived""
  IL_000d:  ldnull
  IL_000e:  cgt.un
  IL_0010:  call       ""void System.Console.WriteLine(bool)""
  IL_0015:  nop
  IL_0016:  ldloc.0
359
  IL_0017:  isinst     ""Derived""
360 361 362 363 364 365 366
  IL_001c:  stloc.1
  IL_001d:  ldloc.1
  IL_001e:  ldnull
  IL_001f:  cgt.un
  IL_0021:  call       ""void System.Console.WriteLine(bool)""
  IL_0026:  nop
  IL_0027:  ldloc.0
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  IL_0028:  stloc.s    V_4
  IL_002a:  ldloc.s    V_4
  IL_002c:  stloc.3
  IL_002d:  ldloc.3
  IL_002e:  isinst     ""Derived""
  IL_0033:  stloc.2
  IL_0034:  ldloc.2
  IL_0035:  brtrue.s   IL_0039
  IL_0037:  br.s       IL_0044
  IL_0039:  br.s       IL_003b
  IL_003b:  ldc.i4.1
  IL_003c:  call       ""void System.Console.WriteLine(bool)""
  IL_0041:  nop
  IL_0042:  br.s       IL_0044
  IL_0044:  ldloc.0
  IL_0045:  isinst     ""Derived""
  IL_004a:  ldnull
  IL_004b:  cgt.un
  IL_004d:  call       ""void System.Console.WriteLine(bool)""
  IL_0052:  nop
  IL_0053:  ret
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
}");
        }

        [Fact]
        public void DoublePattern01()
        {
            var source =
@"using System;
class Program
{
    static bool P1(double d) => d is double.NaN;
    static bool P2(float f) => f is float.NaN;
    static bool P3(double d) => d is 3.14d;
    static bool P4(float f) => f is 3.14f;
    static bool P5(object o)
    {
        switch (o)
        {
            case double.NaN: return true;
            case float.NaN: return true;
            case 3.14d: return true;
            case 3.14f: return true;
            default: return false;
        }
    }
    public static void Main(string[] args)
    {
        Console.Write(P1(double.NaN));
        Console.Write(P1(1.0));
        Console.Write(P2(float.NaN));
        Console.Write(P2(1.0f));
        Console.Write(P3(3.14));
        Console.Write(P3(double.NaN));
        Console.Write(P4(3.14f));
        Console.Write(P4(float.NaN));
        Console.Write(P5(double.NaN));
        Console.Write(P5(0.0d));
        Console.Write(P5(float.NaN));
        Console.Write(P5(0.0f));
        Console.Write(P5(3.14d));
        Console.Write(P5(125));
        Console.Write(P5(3.14f));
        Console.Write(P5(1.0f));
    }
}";
433
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
            compilation.VerifyDiagnostics();
            var expectedOutput = @"TrueFalseTrueFalseTrueFalseTrueFalseTrueFalseTrueFalseTrueFalseTrueFalse";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("Program.P1",
@"{
  // Code size        7 (0x7)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  call       ""bool double.IsNaN(double)""
  IL_0006:  ret
}");
            compVerifier.VerifyIL("Program.P2",
@"{
  // Code size        7 (0x7)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  call       ""bool float.IsNaN(float)""
  IL_0006:  ret
}");
            compVerifier.VerifyIL("Program.P3",
@"{
  // Code size       13 (0xd)
  .maxstack  2
457 458
  IL_0000:  ldarg.0
  IL_0001:  ldc.r8     3.14
459 460 461 462 463 464 465
  IL_000a:  ceq
  IL_000c:  ret
}");
            compVerifier.VerifyIL("Program.P4",
@"{
  // Code size        9 (0x9)
  .maxstack  2
466 467
  IL_0000:  ldarg.0
  IL_0001:  ldc.r4     3.14
468 469 470 471 472
  IL_0006:  ceq
  IL_0008:  ret
}");
            compVerifier.VerifyIL("Program.P5",
@"{
473
  // Code size      103 (0x67)
474
  .maxstack  2
475 476 477 478 479
  .locals init (object V_0,
                double V_1,
                float V_2,
                object V_3,
                bool V_4)
480 481
  IL_0000:  nop
  IL_0001:  ldarg.0
482 483 484 485 486 487 488 489 490 491 492 493
  IL_0002:  stloc.3
  IL_0003:  ldloc.3
  IL_0004:  stloc.0
  IL_0005:  ldloc.0
  IL_0006:  isinst     ""double""
  IL_000b:  brfalse.s  IL_002a
  IL_000d:  ldloc.0
  IL_000e:  unbox.any  ""double""
  IL_0013:  stloc.1
  IL_0014:  ldloc.1
  IL_0015:  call       ""bool double.IsNaN(double)""
  IL_001a:  brtrue.s   IL_004b
494 495
  IL_001c:  ldloc.1
  IL_001d:  ldc.r8     3.14
496 497 498 499 500 501 502 503 504 505 506
  IL_0026:  beq.s      IL_0055
  IL_0028:  br.s       IL_005f
  IL_002a:  ldloc.0
  IL_002b:  isinst     ""float""
  IL_0030:  brfalse.s  IL_005f
  IL_0032:  ldloc.0
  IL_0033:  unbox.any  ""float""
  IL_0038:  stloc.2
  IL_0039:  ldloc.2
  IL_003a:  call       ""bool float.IsNaN(float)""
  IL_003f:  brtrue.s   IL_0050
507 508
  IL_0041:  ldloc.2
  IL_0042:  ldc.r4     3.14
509 510 511 512 513
  IL_0047:  beq.s      IL_005a
  IL_0049:  br.s       IL_005f
  IL_004b:  ldc.i4.1
  IL_004c:  stloc.s    V_4
  IL_004e:  br.s       IL_0064
514
  IL_0050:  ldc.i4.1
515 516 517 518 519 520 521 522 523 524 525 526 527
  IL_0051:  stloc.s    V_4
  IL_0053:  br.s       IL_0064
  IL_0055:  ldc.i4.1
  IL_0056:  stloc.s    V_4
  IL_0058:  br.s       IL_0064
  IL_005a:  ldc.i4.1
  IL_005b:  stloc.s    V_4
  IL_005d:  br.s       IL_0064
  IL_005f:  ldc.i4.0
  IL_0060:  stloc.s    V_4
  IL_0062:  br.s       IL_0064
  IL_0064:  ldloc.s    V_4
  IL_0066:  ret
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
}");
        }

        [Fact]
        public void DecimalEquality()
        {
            // demonstrate that pattern-matching against a decimal constant is
            // at least as efficient as simply using ==
            var source =
@"using System;
public class C
{
    public static void Main()
    {
        Console.Write(M1(1.0m));
        Console.Write(M2(1.0m));
        Console.Write(M1(2.0m));
        Console.Write(M2(2.0m));
    }

    static int M1(decimal d)
    {
        if (M(d) is 1.0m) return 1;
        return 0;
    }

    static int M2(decimal d)
    {
        if (M(d) == 1.0m) return 1;
        return 0;
    }

    public static decimal M(decimal d)
    {
        return d;
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"1100";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("C.M1",
@"{
  // Code size       39 (0x27)
572
  .maxstack  6
573 574 575 576 577 578 579
  .locals init (bool V_0,
                decimal V_1,
                int V_2)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  call       ""decimal C.M(decimal)""
  IL_0007:  stloc.1
580 581
  IL_0008:  ldloc.1
  IL_0009:  ldc.i4.s   10
582 583
  IL_000b:  ldc.i4.0
  IL_000c:  ldc.i4.0
584 585 586
  IL_000d:  ldc.i4.0
  IL_000e:  ldc.i4.1
  IL_000f:  newobj     ""decimal..ctor(int, int, int, bool, byte)""
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  IL_0014:  call       ""bool decimal.op_Equality(decimal, decimal)""
  IL_0019:  stloc.0
  IL_001a:  ldloc.0
  IL_001b:  brfalse.s  IL_0021
  IL_001d:  ldc.i4.1
  IL_001e:  stloc.2
  IL_001f:  br.s       IL_0025
  IL_0021:  ldc.i4.0
  IL_0022:  stloc.2
  IL_0023:  br.s       IL_0025
  IL_0025:  ldloc.2
  IL_0026:  ret
}");
            compVerifier.VerifyIL("C.M2",
@"{
  // Code size       37 (0x25)
  .maxstack  6
  .locals init (bool V_0,
                int V_1)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  call       ""decimal C.M(decimal)""
  IL_0007:  ldc.i4.s   10
  IL_0009:  ldc.i4.0
  IL_000a:  ldc.i4.0
  IL_000b:  ldc.i4.0
  IL_000c:  ldc.i4.1
  IL_000d:  newobj     ""decimal..ctor(int, int, int, bool, byte)""
  IL_0012:  call       ""bool decimal.op_Equality(decimal, decimal)""
  IL_0017:  stloc.0
  IL_0018:  ldloc.0
  IL_0019:  brfalse.s  IL_001f
  IL_001b:  ldc.i4.1
  IL_001c:  stloc.1
  IL_001d:  br.s       IL_0023
  IL_001f:  ldc.i4.0
  IL_0020:  stloc.1
  IL_0021:  br.s       IL_0023
  IL_0023:  ldloc.1
  IL_0024:  ret
}");

            compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("C.M1",
@"{
634 635
  // Code size       28 (0x1c)
  .maxstack  6
636 637
  IL_0000:  ldarg.0
  IL_0001:  call       ""decimal C.M(decimal)""
638 639
  IL_0006:  ldc.i4.s   10
  IL_0008:  ldc.i4.0
640 641
  IL_0009:  ldc.i4.0
  IL_000a:  ldc.i4.0
642 643 644 645 646 647 648
  IL_000b:  ldc.i4.1
  IL_000c:  newobj     ""decimal..ctor(int, int, int, bool, byte)""
  IL_0011:  call       ""bool decimal.op_Equality(decimal, decimal)""
  IL_0016:  brfalse.s  IL_001a
  IL_0018:  ldc.i4.1
  IL_0019:  ret
  IL_001a:  ldc.i4.0
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
  IL_001b:  ret
}");
            compVerifier.VerifyIL("C.M2",
@"{
  // Code size       28 (0x1c)
  .maxstack  6
  IL_0000:  ldarg.0
  IL_0001:  call       ""decimal C.M(decimal)""
  IL_0006:  ldc.i4.s   10
  IL_0008:  ldc.i4.0
  IL_0009:  ldc.i4.0
  IL_000a:  ldc.i4.0
  IL_000b:  ldc.i4.1
  IL_000c:  newobj     ""decimal..ctor(int, int, int, bool, byte)""
  IL_0011:  call       ""bool decimal.op_Equality(decimal, decimal)""
  IL_0016:  brfalse.s  IL_001a
  IL_0018:  ldc.i4.1
  IL_0019:  ret
  IL_001a:  ldc.i4.0
  IL_001b:  ret
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
}");
        }

        [Fact, WorkItem(16878, "https://github.com/dotnet/roslyn/issues/16878")]
        public void RedundantNullCheck()
        {
            var source =
@"public class C
{
    static int M1(bool? b1, bool b2)
    {
        switch (b1)
        {
            case null:
                return 1;
            case var _ when b2:
                return 2;
            case true:
                return 3;
            case false:
                return 4;
        }
    }

    static int M2(object o, bool b)
    {
        switch (o)
        {
            case string a when b:
                return 1;
            case string a:
                return 2;
        }
        return 3;
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M1",
@"{
710
  // Code size       33 (0x21)
711
  .maxstack  1
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
  IL_0000:  ldarga.s   V_0
  IL_0002:  call       ""bool bool?.HasValue.get""
  IL_0007:  brfalse.s  IL_0016
  IL_0009:  br.s       IL_0018
  IL_000b:  ldarga.s   V_0
  IL_000d:  call       ""bool bool?.GetValueOrDefault()""
  IL_0012:  brtrue.s   IL_001d
  IL_0014:  br.s       IL_001f
  IL_0016:  ldc.i4.1
  IL_0017:  ret
  IL_0018:  ldarg.1
  IL_0019:  brfalse.s  IL_000b
  IL_001b:  ldc.i4.2
  IL_001c:  ret
  IL_001d:  ldc.i4.3
727
  IL_001e:  ret
728
  IL_001f:  ldc.i4.4
729 730 731 732
  IL_0020:  ret
}");
            compVerifier.VerifyIL("C.M2",
@"{
733
  // Code size       19 (0x13)
734
  .maxstack  1
735
  .locals init (string V_0) //a
736
  IL_0000:  ldarg.0
737 738 739 740 741 742 743 744 745 746 747 748
  IL_0001:  isinst     ""string""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brfalse.s  IL_0011
  IL_000a:  ldarg.1
  IL_000b:  brfalse.s  IL_000f
  IL_000d:  ldc.i4.1
  IL_000e:  ret
  IL_000f:  ldc.i4.2
  IL_0010:  ret
  IL_0011:  ldc.i4.3
  IL_0012:  ret
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
}");
        }

        [Fact, WorkItem(12813, "https://github.com/dotnet/roslyn/issues/12813")]
        public void NoBoxingOnIntegerConstantPattern()
        {
            var source =
@"public class C
{
    static bool M1(int x)
    {
        return x is 42;
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M1",
@"{
  // Code size        6 (0x6)
  .maxstack  2
770 771
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.s   42
772 773
  IL_0003:  ceq
  IL_0005:  ret
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
}");
        }

        [Fact, WorkItem(22654, "https://github.com/dotnet/roslyn/issues/22654")]
        public void NoRedundantTypeCheck()
        {
            var source =
@"using System;
public class C
{
    public void SwitchBasedPatternMatching(object o)
    {
        switch (o)
        {
            case int n when n == 1:
                Console.WriteLine(""1""); break;
            case string s:
                Console.WriteLine(""s""); break;
            case int n when n == 2:
                Console.WriteLine(""2""); break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.SwitchBasedPatternMatching",
@"{
802
  // Code size       67 (0x43)
803
  .maxstack  2
804
  .locals init (int V_0) //n
805
  IL_0000:  ldarg.1
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
  IL_0001:  isinst     ""int""
  IL_0006:  brfalse.s  IL_0011
  IL_0008:  ldarg.1
  IL_0009:  unbox.any  ""int""
  IL_000e:  stloc.0
  IL_000f:  br.s       IL_001a
  IL_0011:  ldarg.1
  IL_0012:  isinst     ""string""
  IL_0017:  brtrue.s   IL_0029
  IL_0019:  ret
  IL_001a:  ldloc.0
  IL_001b:  ldc.i4.1
  IL_001c:  bne.un.s   IL_0034
  IL_001e:  ldstr      ""1""
  IL_0023:  call       ""void System.Console.WriteLine(string)""
  IL_0028:  ret
  IL_0029:  ldstr      ""s""
  IL_002e:  call       ""void System.Console.WriteLine(string)""
  IL_0033:  ret
  IL_0034:  ldloc.0
  IL_0035:  ldc.i4.2
  IL_0036:  bne.un.s   IL_0042
  IL_0038:  ldstr      ""2""
  IL_003d:  call       ""void System.Console.WriteLine(string)""
  IL_0042:  ret
831 832 833
}");
        }

834
        [Fact, WorkItem(15437, "https://github.com/dotnet/roslyn/issues/15437")]
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
        public void IsTypeDiscard()
        {
            var source =
@"public class C
{
    public bool IsString(object o)
    {
        return o is string _;
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.IsString",
@"{
  // Code size       10 (0xa)
  .maxstack  2
  IL_0000:  ldarg.1
  IL_0001:  isinst     ""string""
  IL_0006:  ldnull
  IL_0007:  cgt.un
  IL_0009:  ret
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
}");
        }

        [Fact, WorkItem(19150, "https://github.com/dotnet/roslyn/issues/19150")]
        public void RedundantHasValue()
        {
            var source =
@"using System;
public class C
{
    public static void M(int? x)
    {
        switch (x)
        {
            case int i:
                Console.Write(i);
                break;
            case null:
                Console.Write(""null"");
                break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
885
  // Code size       33 (0x21)
886
  .maxstack  1
887 888 889 890 891 892 893 894 895 896
  IL_0000:  ldarga.s   V_0
  IL_0002:  call       ""bool int?.HasValue.get""
  IL_0007:  brfalse.s  IL_0016
  IL_0009:  ldarga.s   V_0
  IL_000b:  call       ""int int?.GetValueOrDefault()""
  IL_0010:  call       ""void System.Console.Write(int)""
  IL_0015:  ret
  IL_0016:  ldstr      ""null""
  IL_001b:  call       ""void System.Console.Write(string)""
  IL_0020:  ret
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
}");
        }

        [Fact, WorkItem(19153, "https://github.com/dotnet/roslyn/issues/19153")]
        public void RedundantBox()
        {
            var source = @"using System;
public class C
{
    public static void M<T, U>(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<T, U>(U)",
@"{
923
  // Code size       35 (0x23)
924 925
  .maxstack  1
  IL_0000:  ldarg.0
926 927 928 929 930 931 932 933 934
  IL_0001:  box        ""U""
  IL_0006:  isinst     ""T""
  IL_000b:  brfalse.s  IL_0022
  IL_000d:  ldarg.0
  IL_000e:  box        ""U""
  IL_0013:  unbox.any  ""T""
  IL_0018:  box        ""T""
  IL_001d:  call       ""void System.Console.Write(object)""
  IL_0022:  ret
935 936 937
}");
        }

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead01()
        {
            var source = @"using System;

public class Person {
    public string Name { get; set; }
}

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: ""Bill"" }:
                Console.WriteLine(""Hey Bill!"");
            break;
            case { Name: ""Bob"" }:
                Console.WriteLine(""Hey Bob!"");
            break;
            case { Name: var name }:
                Console.WriteLine($""Hello {name}!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       85 (0x55)
  .maxstack  3
  .locals init (string V_0) //name
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0054
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  ldloc.0
  IL_000b:  brfalse.s  IL_003f
  IL_000d:  ldloc.0
  IL_000e:  ldstr      ""Bill""
  IL_0013:  call       ""bool string.op_Equality(string, string)""
  IL_0018:  brtrue.s   IL_0029
  IL_001a:  ldloc.0
  IL_001b:  ldstr      ""Bob""
  IL_0020:  call       ""bool string.op_Equality(string, string)""
  IL_0025:  brtrue.s   IL_0034
  IL_0027:  br.s       IL_003f
  IL_0029:  ldstr      ""Hey Bill!""
  IL_002e:  call       ""void System.Console.WriteLine(string)""
  IL_0033:  ret
  IL_0034:  ldstr      ""Hey Bob!""
  IL_0039:  call       ""void System.Console.WriteLine(string)""
  IL_003e:  ret
  IL_003f:  ldstr      ""Hello ""
  IL_0044:  ldloc.0
  IL_0045:  ldstr      ""!""
  IL_004a:  call       ""string string.Concat(string, string, string)""
  IL_004f:  call       ""void System.Console.WriteLine(string)""
  IL_0054:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead02()
        {
            var source = @"using System;

public class Person {
    public string Name { get; set; }
    public int Age;
}

public class C {
    public void M(Person p) {
        switch (p) {
            case Person { Name: var name, Age: 0}:
                Console.WriteLine($""Hello baby { name }!"");
            break;
            case Person { Name: var name }:
                Console.WriteLine($""Hello { name }!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       64 (0x40)
  .maxstack  3
  .locals init (string V_0, //name
                string V_1) //name
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_003f
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  ldarg.1
  IL_000b:  ldfld      ""int Person.Age""
  IL_0010:  brtrue.s   IL_0028
  IL_0012:  ldstr      ""Hello baby ""
  IL_0017:  ldloc.0
  IL_0018:  ldstr      ""!""
  IL_001d:  call       ""string string.Concat(string, string, string)""
  IL_0022:  call       ""void System.Console.WriteLine(string)""
  IL_0027:  ret
  IL_0028:  ldloc.0
  IL_0029:  stloc.1
  IL_002a:  ldstr      ""Hello ""
  IL_002f:  ldloc.1
  IL_0030:  ldstr      ""!""
  IL_0035:  call       ""string string.Concat(string, string, string)""
  IL_003a:  call       ""void System.Console.WriteLine(string)""
  IL_003f:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead03()
        {
            var source = @"using System;

public class Person {
    public string Name { get; set; }
}

public class Student : Person { }

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: ""Bill"" }:
                Console.WriteLine(""Hey Bill!"");
            break;
            case Student { Name: var name }:
                Console.WriteLine($""Hello student { name}!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       68 (0x44)
  .maxstack  3
  .locals init (string V_0) //name
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0043
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  ldloc.0
  IL_000b:  brfalse.s  IL_001a
  IL_000d:  ldloc.0
  IL_000e:  ldstr      ""Bill""
  IL_0013:  call       ""bool string.op_Equality(string, string)""
  IL_0018:  brtrue.s   IL_0023
  IL_001a:  ldarg.1
  IL_001b:  isinst     ""Student""
  IL_0020:  brtrue.s   IL_002e
  IL_0022:  ret
  IL_0023:  ldstr      ""Hey Bill!""
  IL_0028:  call       ""void System.Console.WriteLine(string)""
  IL_002d:  ret
  IL_002e:  ldstr      ""Hello student ""
  IL_0033:  ldloc.0
  IL_0034:  ldstr      ""!""
  IL_0039:  call       ""string string.Concat(string, string, string)""
  IL_003e:  call       ""void System.Console.WriteLine(string)""
  IL_0043:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead04()
        {
            // Cannot combine the evaluations of name here, since we first check if p is Teacher,
            // and only if that fails check if p is null. 
            // Combining the evaluations would mean first checking if p is null, then evaluating name, then checking if p is Teacher.
            // This would not necessarily be more performant.
            var source = @"using System;

public class Person {
    public string Name { get; set; }
}

public class Teacher : Person { }

public class C {
    public void M(Person p) {
        switch (p) {
            case Teacher { Name: var name }:
                Console.WriteLine($""Hello teacher { name}!"");
            break;
            case Person { Name: var name }:
                Console.WriteLine($""Hello { name}!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"
{
  // Code size       77 (0x4d)
  .maxstack  3
  .locals init (string V_0, //name
                string V_1, //name
                Teacher V_2)
  IL_0000:  ldarg.1
  IL_0001:  isinst     ""Teacher""
  IL_0006:  stloc.2
  IL_0007:  ldloc.2
  IL_0008:  brfalse.s  IL_0013
  IL_000a:  ldloc.2
  IL_000b:  callvirt   ""string Person.Name.get""
  IL_0010:  stloc.0
  IL_0011:  br.s       IL_001f
  IL_0013:  ldarg.1
  IL_0014:  brfalse.s  IL_004c
  IL_0016:  ldarg.1
  IL_0017:  callvirt   ""string Person.Name.get""
  IL_001c:  stloc.0
  IL_001d:  br.s       IL_0035
  IL_001f:  ldstr      ""Hello teacher ""
  IL_0024:  ldloc.0
  IL_0025:  ldstr      ""!""
  IL_002a:  call       ""string string.Concat(string, string, string)""
  IL_002f:  call       ""void System.Console.WriteLine(string)""
  IL_0034:  ret
  IL_0035:  ldloc.0
  IL_0036:  stloc.1
  IL_0037:  ldstr      ""Hello ""
  IL_003c:  ldloc.1
  IL_003d:  ldstr      ""!""
  IL_0042:  call       ""string string.Concat(string, string, string)""
  IL_0047:  call       ""void System.Console.WriteLine(string)""
  IL_004c:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead05()
        {
            // Cannot combine the evaluations of name here, since we first check if p is Teacher,
            // and only if that fails check if p is Student. 
            // Combining the evaluations would mean first checking if p is null, 
            // then evaluating name, 
            // then checking if p is Teacher, 
            // then checking if p is Student.
            // This would not necessarily be more performant.
            var source = @"using System;

public class Person {
    public string Name { get; set; }
}

public class Student : Person { }

public class Teacher : Person { }

public class C {
    public void M(Person p) {
        switch (p) {
            case Teacher { Name: var name }:
                Console.WriteLine($""Hello teacher { name}!"");
            break;
            case Student { Name: var name }:
                Console.WriteLine($""Hello student { name}!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"
{
  // Code size       84 (0x54)
  .maxstack  3
  .locals init (string V_0, //name
                string V_1, //name
                Teacher V_2,
                Student V_3)
  IL_0000:  ldarg.1
  IL_0001:  isinst     ""Teacher""
  IL_0006:  stloc.2
  IL_0007:  ldloc.2
  IL_0008:  brfalse.s  IL_0013
  IL_000a:  ldloc.2
  IL_000b:  callvirt   ""string Person.Name.get""
  IL_0010:  stloc.0
  IL_0011:  br.s       IL_0026
  IL_0013:  ldarg.1
  IL_0014:  isinst     ""Student""
  IL_0019:  stloc.3
  IL_001a:  ldloc.3
  IL_001b:  brfalse.s  IL_0053
  IL_001d:  ldloc.3
  IL_001e:  callvirt   ""string Person.Name.get""
  IL_0023:  stloc.0
  IL_0024:  br.s       IL_003c
  IL_0026:  ldstr      ""Hello teacher ""
  IL_002b:  ldloc.0
  IL_002c:  ldstr      ""!""
  IL_0031:  call       ""string string.Concat(string, string, string)""
  IL_0036:  call       ""void System.Console.WriteLine(string)""
  IL_003b:  ret
  IL_003c:  ldloc.0
  IL_003d:  stloc.1
  IL_003e:  ldstr      ""Hello student ""
  IL_0043:  ldloc.1
  IL_0044:  ldstr      ""!""
  IL_0049:  call       ""string string.Concat(string, string, string)""
  IL_004e:  call       ""void System.Console.WriteLine(string)""
  IL_0053:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead06()
        {
            var source = @"using System;

public class Person {
    public string Name { get; set; }
}

public class Student : Person { }

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: { Length: 0 } }:
                Console.WriteLine(""Hello!"");
            break;
            case Student { Name: { Length : 1 } }:
                Console.WriteLine($""Hello student!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       58 (0x3a)
  .maxstack  2
  .locals init (string V_0,
                int V_1)
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0039
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  ldloc.0
  IL_000b:  brfalse.s  IL_0039
  IL_000d:  ldloc.0
  IL_000e:  callvirt   ""int string.Length.get""
  IL_0013:  stloc.1
  IL_0014:  ldloc.1
  IL_0015:  brfalse.s  IL_0024
  IL_0017:  ldarg.1
  IL_0018:  isinst     ""Student""
  IL_001d:  brfalse.s  IL_0039
  IL_001f:  ldloc.1
  IL_0020:  ldc.i4.1
  IL_0021:  beq.s      IL_002f
  IL_0023:  ret
  IL_0024:  ldstr      ""Hello!""
  IL_0029:  call       ""void System.Console.WriteLine(string)""
  IL_002e:  ret
  IL_002f:  ldstr      ""Hello student!""
  IL_0034:  call       ""void System.Console.WriteLine(string)""
  IL_0039:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead07()
        {
            // Cannot combine the evaluations of name here, since we first check if name is MemoryStream,
            // and only if that fails check if name is null. 
            // Combining the evaluations would mean first checking if name is null, then evaluating name, then checking if p is Teacher.
            // This would not necessarily be more performant.
            var source = @"using System;
using System.IO;

public class Person {
    public Stream Name { get; set; }
}

public class C {
    public void M(Person p) {
        switch (p) {
            case Person { Name: MemoryStream { Length: 1 } }:
                Console.WriteLine(""Your Names A MemoryStream!"");
            break;
            case Person { Name: { Length : var x } }:
                Console.WriteLine(""Your Names A Stream!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       66 (0x42)
  .maxstack  2
  .locals init (System.IO.Stream V_0,
                System.IO.MemoryStream V_1)
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0041
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""System.IO.Stream Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  ldloc.0
  IL_000b:  isinst     ""System.IO.MemoryStream""
  IL_0010:  stloc.1
  IL_0011:  ldloc.1
  IL_0012:  brfalse.s  IL_0020
  IL_0014:  ldloc.1
  IL_0015:  callvirt   ""long System.IO.Stream.Length.get""
  IL_001a:  ldc.i4.1
  IL_001b:  conv.i8
  IL_001c:  beq.s      IL_002c
  IL_001e:  br.s       IL_0023
  IL_0020:  ldloc.0
  IL_0021:  brfalse.s  IL_0041
  IL_0023:  ldloc.0
  IL_0024:  callvirt   ""long System.IO.Stream.Length.get""
  IL_0029:  pop
  IL_002a:  br.s       IL_0037
  IL_002c:  ldstr      ""Your Names A MemoryStream!""
  IL_0031:  call       ""void System.Console.WriteLine(string)""
  IL_0036:  ret
  IL_0037:  ldstr      ""Your Names A Stream!""
  IL_003c:  call       ""void System.Console.WriteLine(string)""
  IL_0041:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead08()
        {
            var source = @"
public class Person {
    public string Name { get; set; }
}

public class Student : Person { }

public class C {
    public string M(Person p) {
        return p switch  {
            { Name: ""Bill"" } => ""Hey Bill!"",
            Student { Name: var name } => ""Hello student { name}!"",
            _ => null,
        };
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       56 (0x38)
  .maxstack  2
  .locals init (string V_0, //name
                string V_1)
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0034
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  ldloc.0
  IL_000b:  brfalse.s  IL_001a
  IL_000d:  ldloc.0
  IL_000e:  ldstr      ""Bill""
  IL_0013:  call       ""bool string.op_Equality(string, string)""
  IL_0018:  brtrue.s   IL_0024
  IL_001a:  ldarg.1
  IL_001b:  isinst     ""Student""
  IL_0020:  brtrue.s   IL_002c
  IL_0022:  br.s       IL_0034
  IL_0024:  ldstr      ""Hey Bill!""
  IL_0029:  stloc.1
  IL_002a:  br.s       IL_0036
  IL_002c:  ldstr      ""Hello student { name}!""
  IL_0031:  stloc.1
  IL_0032:  br.s       IL_0036
  IL_0034:  ldnull
  IL_0035:  stloc.1
  IL_0036:  ldloc.1
  IL_0037:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead09()
        {
            var source = @"using System;

public class Person {
    public virtual string Name { get; set; }
}

public class Student : Person { }

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: ""Bill"" }:
                Console.WriteLine(""Hey Bill!"");
            break;
            case Student { Name: var name }:
                Console.WriteLine($""Hello student { name}!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       68 (0x44)
  .maxstack  3
  .locals init (string V_0) //name
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0043
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  ldloc.0
  IL_000b:  brfalse.s  IL_001a
  IL_000d:  ldloc.0
  IL_000e:  ldstr      ""Bill""
  IL_0013:  call       ""bool string.op_Equality(string, string)""
  IL_0018:  brtrue.s   IL_0023
  IL_001a:  ldarg.1
  IL_001b:  isinst     ""Student""
  IL_0020:  brtrue.s   IL_002e
  IL_0022:  ret
  IL_0023:  ldstr      ""Hey Bill!""
  IL_0028:  call       ""void System.Console.WriteLine(string)""
  IL_002d:  ret
  IL_002e:  ldstr      ""Hello student ""
  IL_0033:  ldloc.0
  IL_0034:  ldstr      ""!""
  IL_0039:  call       ""string string.Concat(string, string, string)""
  IL_003e:  call       ""void System.Console.WriteLine(string)""
  IL_0043:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void NoRedundantPropertyRead10()
        {
            // Currently we don't combine these redundant property reads.
            // However we could do so in the future.

            var source = @"using System;

public abstract class Person {
    public abstract string Name { get; set; }
}

public class Student : Person { 
    public override string Name { get; set; }
}

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: ""Bill"" }:
                Console.WriteLine(""Hey Bill!"");
            break;
            case Student { Name: var name }:
                Console.WriteLine($""Hello student { name}!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       78 (0x4e)
  .maxstack  3
  .locals init (string V_0, //name
                string V_1,
                Student V_2)
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_004d
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.1
  IL_000a:  ldloc.1
  IL_000b:  brfalse.s  IL_001a
  IL_000d:  ldloc.1
  IL_000e:  ldstr      ""Bill""
  IL_0013:  call       ""bool string.op_Equality(string, string)""
  IL_0018:  brtrue.s   IL_002d
  IL_001a:  ldarg.1
  IL_001b:  isinst     ""Student""
  IL_0020:  stloc.2
  IL_0021:  ldloc.2
  IL_0022:  brfalse.s  IL_004d
  IL_0024:  ldloc.2
  IL_0025:  callvirt   ""string Person.Name.get""
  IL_002a:  stloc.0
  IL_002b:  br.s       IL_0038
  IL_002d:  ldstr      ""Hey Bill!""
  IL_0032:  call       ""void System.Console.WriteLine(string)""
  IL_0037:  ret
  IL_0038:  ldstr      ""Hello student ""
  IL_003d:  ldloc.0
  IL_003e:  ldstr      ""!""
  IL_0043:  call       ""string string.Concat(string, string, string)""
  IL_0048:  call       ""void System.Console.WriteLine(string)""
  IL_004d:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void CombiningRedundantPropertyReadsDoesNotChangeNullabilityAnalysis01()
        {
            // Currently we don't combine the redundant property reads at all.
            // However this could be improved in the future so this test is important

            var source = @"using System;

#nullable enable

public class Person {
    public virtual string? Name { get; }
}

public class Student : Person {
    public override string Name { get => base.Name ?? """"; }
}

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: ""Bill""}:
                Console.WriteLine(""Hey Bill!"");
            break;
            case Student { Name: var name }:
                Console.WriteLine($""Student has name of length { name.Length }!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       83 (0x53)
  .maxstack  2
  .locals init (string V_0, //name
                string V_1,
                Student V_2)
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0052
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.1
  IL_000a:  ldloc.1
  IL_000b:  brfalse.s  IL_001a
  IL_000d:  ldloc.1
  IL_000e:  ldstr      ""Bill""
  IL_0013:  call       ""bool string.op_Equality(string, string)""
  IL_0018:  brtrue.s   IL_002d
  IL_001a:  ldarg.1
  IL_001b:  isinst     ""Student""
  IL_0020:  stloc.2
  IL_0021:  ldloc.2
  IL_0022:  brfalse.s  IL_0052
  IL_0024:  ldloc.2
  IL_0025:  callvirt   ""string Person.Name.get""
  IL_002a:  stloc.0
  IL_002b:  br.s       IL_0038
  IL_002d:  ldstr      ""Hey Bill!""
  IL_0032:  call       ""void System.Console.WriteLine(string)""
  IL_0037:  ret
  IL_0038:  ldstr      ""Student has name of length {0}!""
  IL_003d:  ldloc.0
  IL_003e:  callvirt   ""int string.Length.get""
  IL_0043:  box        ""int""
  IL_0048:  call       ""string string.Format(string, object)""
  IL_004d:  call       ""void System.Console.WriteLine(string)""
  IL_0052:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void CombiningRedundantPropertyReadsDoesNotChangeNullabilityAnalysis02()
        {
            var source = @"using System;

#nullable enable

public class Person {
    public string? Name { get;}
}

public class Student : Person {
}

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: var name } when name is null:
                Console.WriteLine($""Hey { name }"");
            break;
            case Student { Name: var name } when name != null:
                Console.WriteLine($""Student has name of length { name.Length }!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       73 (0x49)
  .maxstack  2
  .locals init (string V_0, //name
                string V_1) //name
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0048
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  br.s       IL_0015
  IL_000c:  ldarg.1
  IL_000d:  isinst     ""Student""
  IL_0012:  brtrue.s   IL_0029
  IL_0014:  ret
  IL_0015:  ldloc.0
  IL_0016:  brtrue.s   IL_000c
  IL_0018:  ldstr      ""Hey ""
  IL_001d:  ldloc.0
  IL_001e:  call       ""string string.Concat(string, string)""
  IL_0023:  call       ""void System.Console.WriteLine(string)""
  IL_0028:  ret
  IL_0029:  ldloc.0
  IL_002a:  stloc.1
  IL_002b:  ldloc.1
  IL_002c:  brfalse.s  IL_0048
  IL_002e:  ldstr      ""Student has name of length {0}!""
  IL_0033:  ldloc.1
  IL_0034:  callvirt   ""int string.Length.get""
  IL_0039:  box        ""int""
  IL_003e:  call       ""string string.Format(string, object)""
  IL_0043:  call       ""void System.Console.WriteLine(string)""
  IL_0048:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void CombiningRedundantPropertyReadsDoesNotChangeNullabilityAnalysis03()
        {
            var source = @"using System;

#nullable enable

public class Person {
    public string? Name { get; }
}

public class Student : Person {
}

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: string name }:
                Console.WriteLine($""Hey {name}"");
            break;
            case Student { Name: var name }:
                Console.WriteLine($""Student has name of length { name.Length }!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics(
                // (19,66): warning CS8602: Dereference of a possibly null reference.
                //                 Console.WriteLine($"Student has name of length { name.Length }!");
                Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "name").WithLocation(19, 66));
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       68 (0x44)
  .maxstack  2
  .locals init (string V_0, //name
                string V_1) //name
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0043
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.0
  IL_000a:  ldloc.0
  IL_000b:  brtrue.s   IL_0016
  IL_000d:  ldarg.1
  IL_000e:  isinst     ""Student""
  IL_0013:  brtrue.s   IL_0027
  IL_0015:  ret
  IL_0016:  ldstr      ""Hey ""
  IL_001b:  ldloc.0
  IL_001c:  call       ""string string.Concat(string, string)""
  IL_0021:  call       ""void System.Console.WriteLine(string)""
  IL_0026:  ret
  IL_0027:  ldloc.0
  IL_0028:  stloc.1
  IL_0029:  ldstr      ""Student has name of length {0}!""
  IL_002e:  ldloc.1
  IL_002f:  callvirt   ""int string.Length.get""
  IL_0034:  box        ""int""
  IL_0039:  call       ""string string.Format(string, object)""
  IL_003e:  call       ""void System.Console.WriteLine(string)""
  IL_0043:  ret
}");
        }

        [Fact, WorkItem(34933, "https://github.com/dotnet/roslyn/issues/34933")]
        public void DoNotCombineDifferentPropertyReadsWithSameName()
        {
            var source = @"using System;

public class Person {
    public string Name { get; set; }
}

public class Student : Person {
    public new string Name { get; set; }
}

public class C {
    public void M(Person p) {
        switch (p) {
            case { Name: ""Bill"" }:
                Console.WriteLine(""Hey Bill!"");
            break;
            case Student { Name: var name }:
                Console.WriteLine($""Hello student { name}!"");
            break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("C.M",
@"{
  // Code size       78 (0x4e)
  .maxstack  3
  .locals init (string V_0, //name
                string V_1,
                Student V_2)
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_004d
  IL_0003:  ldarg.1
  IL_0004:  callvirt   ""string Person.Name.get""
  IL_0009:  stloc.1
  IL_000a:  ldloc.1
  IL_000b:  brfalse.s  IL_001a
  IL_000d:  ldloc.1
  IL_000e:  ldstr      ""Bill""
  IL_0013:  call       ""bool string.op_Equality(string, string)""
  IL_0018:  brtrue.s   IL_002d
  IL_001a:  ldarg.1
  IL_001b:  isinst     ""Student""
  IL_0020:  stloc.2
  IL_0021:  ldloc.2
  IL_0022:  brfalse.s  IL_004d
  IL_0024:  ldloc.2
  IL_0025:  callvirt   ""string Student.Name.get""
  IL_002a:  stloc.0
  IL_002b:  br.s       IL_0038
  IL_002d:  ldstr      ""Hey Bill!""
  IL_0032:  call       ""void System.Console.WriteLine(string)""
  IL_0037:  ret
  IL_0038:  ldstr      ""Hello student ""
  IL_003d:  ldloc.0
  IL_003e:  ldstr      ""!""
  IL_0043:  call       ""string string.Concat(string, string, string)""
  IL_0048:  call       ""void System.Console.WriteLine(string)""
  IL_004d:  ret
}");
        }

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
        [Fact, WorkItem(20641, "https://github.com/dotnet/roslyn/issues/20641")]
        public void PatternsVsAs01()
        {
            var source = @"using System.Collections;
using System.Collections.Generic;

class Program
{
    static void Main() { }

    internal static bool TryGetCount1<T>(IEnumerable<T> source, out int count)
    {
        ICollection nonGeneric = source as ICollection;
        if (nonGeneric != null)
        {
            count = nonGeneric.Count;
            return true;
        }

        ICollection<T> generic = source as ICollection<T>;
        if (generic != null)
        {
            count = generic.Count;
            return true;
        }

        count = -1;
        return false;
    }

    internal static bool TryGetCount2<T>(IEnumerable<T> source, out int count)
    {
        switch (source)
        {
            case ICollection nonGeneric:
                count = nonGeneric.Count;
                return true;

            case ICollection<T> generic:
                count = generic.Count;
                return true;

            default:
                count = -1;
                return false;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("Program.TryGetCount1<T>",
@"{
  // Code size       45 (0x2d)
  .maxstack  2
  .locals init (System.Collections.ICollection V_0, //nonGeneric
                System.Collections.Generic.ICollection<T> V_1) //generic
  IL_0000:  ldarg.0
  IL_0001:  isinst     ""System.Collections.ICollection""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brfalse.s  IL_0014
  IL_000a:  ldarg.1
  IL_000b:  ldloc.0
  IL_000c:  callvirt   ""int System.Collections.ICollection.Count.get""
  IL_0011:  stind.i4
  IL_0012:  ldc.i4.1
  IL_0013:  ret
  IL_0014:  ldarg.0
  IL_0015:  isinst     ""System.Collections.Generic.ICollection<T>""
  IL_001a:  stloc.1
  IL_001b:  ldloc.1
  IL_001c:  brfalse.s  IL_0028
  IL_001e:  ldarg.1
  IL_001f:  ldloc.1
  IL_0020:  callvirt   ""int System.Collections.Generic.ICollection<T>.Count.get""
  IL_0025:  stind.i4
  IL_0026:  ldc.i4.1
  IL_0027:  ret
  IL_0028:  ldarg.1
  IL_0029:  ldc.i4.m1
  IL_002a:  stind.i4
  IL_002b:  ldc.i4.0
  IL_002c:  ret
}");
            compVerifier.VerifyIL("Program.TryGetCount2<T>",
@"{
1933
  // Code size       47 (0x2f)
1934 1935
  .maxstack  2
  .locals init (System.Collections.ICollection V_0, //nonGeneric
1936
                System.Collections.Generic.ICollection<T> V_1) //generic
1937
  IL_0000:  ldarg.0
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
  IL_0001:  isinst     ""System.Collections.ICollection""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brtrue.s   IL_0016
  IL_000a:  ldarg.0
  IL_000b:  isinst     ""System.Collections.Generic.ICollection<T>""
  IL_0010:  stloc.1
  IL_0011:  ldloc.1
  IL_0012:  brtrue.s   IL_0020
  IL_0014:  br.s       IL_002a
  IL_0016:  ldarg.1
  IL_0017:  ldloc.0
  IL_0018:  callvirt   ""int System.Collections.ICollection.Count.get""
  IL_001d:  stind.i4
  IL_001e:  ldc.i4.1
  IL_001f:  ret
  IL_0020:  ldarg.1
  IL_0021:  ldloc.1
  IL_0022:  callvirt   ""int System.Collections.Generic.ICollection<T>.Count.get""
  IL_0027:  stind.i4
  IL_0028:  ldc.i4.1
  IL_0029:  ret
  IL_002a:  ldarg.1
  IL_002b:  ldc.i4.m1
  IL_002c:  stind.i4
  IL_002d:  ldc.i4.0
  IL_002e:  ret
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
}");
        }

        [Fact, WorkItem(20641, "https://github.com/dotnet/roslyn/issues/20641")]
        public void PatternsVsAs02()
        {
            var source = @"using System.Collections;
class Program
{
    static void Main() { }

    internal static bool IsEmpty1(IEnumerable source)
    {
        var c = source as ICollection;
        return c != null && c.Count > 0;
    }

    internal static bool IsEmpty2(IEnumerable source)
    {
        return source is ICollection c && c.Count > 0;
    }

}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("Program.IsEmpty1",
@"{
  // Code size       22 (0x16)
  .maxstack  2
  .locals init (System.Collections.ICollection V_0) //c
  IL_0000:  ldarg.0
  IL_0001:  isinst     ""System.Collections.ICollection""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brfalse.s  IL_0014
  IL_000a:  ldloc.0
  IL_000b:  callvirt   ""int System.Collections.ICollection.Count.get""
  IL_0010:  ldc.i4.0
  IL_0011:  cgt
  IL_0013:  ret
  IL_0014:  ldc.i4.0
  IL_0015:  ret
}");
            compVerifier.VerifyIL("Program.IsEmpty2",
@"{
2011
  // Code size       22 (0x16)
2012
  .maxstack  2
2013
  .locals init (System.Collections.ICollection V_0) //c
2014 2015
  IL_0000:  ldarg.0
  IL_0001:  isinst     ""System.Collections.ICollection""
2016 2017 2018 2019 2020 2021 2022 2023 2024
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brfalse.s  IL_0014
  IL_000a:  ldloc.0
  IL_000b:  callvirt   ""int System.Collections.ICollection.Count.get""
  IL_0010:  ldc.i4.0
  IL_0011:  cgt
  IL_0013:  ret
  IL_0014:  ldc.i4.0
2025
  IL_0015:  ret
2026 2027 2028
}");
        }

N
Neal Gafter 已提交
2029 2030 2031
        [Fact]
        [WorkItem(20641, "https://github.com/dotnet/roslyn/issues/20641")]
        [WorkItem(1395, "https://github.com/dotnet/csharplang/issues/1395")]
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
        public void TupleSwitch01()
        {
            var source = @"using System;

public class Door
{
    public DoorState State;

    public enum DoorState { Opened, Closed, Locked }

    public enum Action { Open, Close, Lock, Unlock }

N
Neal Gafter 已提交
2044
    public void Act0(Action action, bool haveKey = false)
2045 2046 2047 2048 2049 2050
    {
        Console.Write($""{State} {action}{(haveKey ? "" withKey"" : null)}"");
        State = ChangeState0(State, action, haveKey);
        Console.WriteLine($"" -> {State}"");
    }

N
Neal Gafter 已提交
2051 2052 2053 2054 2055 2056 2057
    public void Act1(Action action, bool haveKey = false)
    {
        Console.Write($""{State} {action}{(haveKey ? "" withKey"" : null)}"");
        State = ChangeState1(State, action, haveKey);
        Console.WriteLine($"" -> {State}"");
    }

2058 2059
    public static DoorState ChangeState0(DoorState state, Action action, bool haveKey = false)
    {
2060
        switch (state, action)
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
        {
            case (DoorState.Opened, Action.Close):
                return DoorState.Closed;
            case (DoorState.Closed, Action.Open):
                return DoorState.Opened;
            case (DoorState.Closed, Action.Lock) when haveKey:
                return DoorState.Locked;
            case (DoorState.Locked, Action.Unlock) when haveKey:
                return DoorState.Closed;
            case var (oldState, _):
                return oldState;
        }
    }

    public static DoorState ChangeState1(DoorState state, Action action, bool haveKey = false) =>
        (state, action) switch {
2077 2078 2079 2080 2081
            (DoorState.Opened, Action.Close) => DoorState.Closed,
            (DoorState.Closed, Action.Open) => DoorState.Opened,
            (DoorState.Closed, Action.Lock) when haveKey => DoorState.Locked,
            (DoorState.Locked, Action.Unlock) when haveKey => DoorState.Closed,
            _ => state };
2082 2083 2084 2085 2086 2087 2088
}

class Program
{
    static void Main(string[] args)
    {
        var door = new Door();
N
Neal Gafter 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
        door.Act0(Door.Action.Close);
        door.Act0(Door.Action.Lock);
        door.Act0(Door.Action.Lock, true);
        door.Act0(Door.Action.Open);
        door.Act0(Door.Action.Unlock);
        door.Act0(Door.Action.Unlock, true);
        door.Act0(Door.Action.Open);
        Console.WriteLine();

        door = new Door();
        door.Act1(Door.Action.Close);
        door.Act1(Door.Action.Lock);
        door.Act1(Door.Action.Lock, true);
        door.Act1(Door.Action.Open);
        door.Act1(Door.Action.Unlock);
        door.Act1(Door.Action.Unlock, true);
        door.Act1(Door.Action.Open);
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
    }
}";
            var expectedOutput =
@"Opened Close -> Closed
Closed Lock -> Closed
Closed Lock withKey -> Locked
Locked Open -> Locked
Locked Unlock -> Locked
Locked Unlock withKey -> Closed
Closed Open -> Opened
N
Neal Gafter 已提交
2116 2117 2118 2119 2120 2121 2122 2123

Opened Close -> Closed
Closed Lock -> Closed
Closed Lock withKey -> Locked
Locked Open -> Locked
Locked Unlock -> Locked
Locked Unlock withKey -> Closed
Closed Open -> Opened
2124
";
2125
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularWithRecursivePatterns);
2126 2127 2128 2129
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("Door.ChangeState0",
@"{
N
Neal Gafter 已提交
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
  // Code size       59 (0x3b)
  .maxstack  2
  .locals init (Door.DoorState V_0) //oldState
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldloc.0
  IL_0003:  switch    (
        IL_0016,
        IL_001c,
        IL_0025)
  IL_0014:  br.s       IL_0039
2141 2142
  IL_0016:  ldarg.1
  IL_0017:  ldc.i4.1
N
Neal Gafter 已提交
2143 2144 2145 2146 2147 2148 2149 2150
  IL_0018:  beq.s      IL_002b
  IL_001a:  br.s       IL_0039
  IL_001c:  ldarg.1
  IL_001d:  brfalse.s  IL_002d
  IL_001f:  ldarg.1
  IL_0020:  ldc.i4.2
  IL_0021:  beq.s      IL_002f
  IL_0023:  br.s       IL_0039
2151 2152
  IL_0025:  ldarg.1
  IL_0026:  ldc.i4.3
N
Neal Gafter 已提交
2153 2154
  IL_0027:  beq.s      IL_0034
  IL_0029:  br.s       IL_0039
2155
  IL_002b:  ldc.i4.1
N
Neal Gafter 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
  IL_002c:  ret
  IL_002d:  ldc.i4.0
  IL_002e:  ret
  IL_002f:  ldarg.2
  IL_0030:  brfalse.s  IL_0039
  IL_0032:  ldc.i4.2
  IL_0033:  ret
  IL_0034:  ldarg.2
  IL_0035:  brfalse.s  IL_0039
  IL_0037:  ldc.i4.1
  IL_0038:  ret
  IL_0039:  ldloc.0
  IL_003a:  ret
2169 2170 2171
}");
            compVerifier.VerifyIL("Door.ChangeState1",
@"{
N
Neal Gafter 已提交
2172 2173 2174 2175 2176 2177 2178 2179 2180
  // Code size       67 (0x43)
  .maxstack  2
  .locals init (Door.DoorState V_0)
  IL_0000:  ldarg.0
  IL_0001:  switch    (
        IL_0014,
        IL_001a,
        IL_0023)
  IL_0012:  br.s       IL_003f
2181 2182
  IL_0014:  ldarg.1
  IL_0015:  ldc.i4.1
N
Neal Gafter 已提交
2183 2184 2185 2186 2187 2188 2189 2190
  IL_0016:  beq.s      IL_0029
  IL_0018:  br.s       IL_003f
  IL_001a:  ldarg.1
  IL_001b:  brfalse.s  IL_002d
  IL_001d:  ldarg.1
  IL_001e:  ldc.i4.2
  IL_001f:  beq.s      IL_0031
  IL_0021:  br.s       IL_003f
2191 2192
  IL_0023:  ldarg.1
  IL_0024:  ldc.i4.3
N
Neal Gafter 已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
  IL_0025:  beq.s      IL_0038
  IL_0027:  br.s       IL_003f
  IL_0029:  ldc.i4.1
  IL_002a:  stloc.0
  IL_002b:  br.s       IL_0041
  IL_002d:  ldc.i4.0
  IL_002e:  stloc.0
  IL_002f:  br.s       IL_0041
  IL_0031:  ldarg.2
  IL_0032:  brfalse.s  IL_003f
  IL_0034:  ldc.i4.2
  IL_0035:  stloc.0
  IL_0036:  br.s       IL_0041
  IL_0038:  ldarg.2
  IL_0039:  brfalse.s  IL_003f
  IL_003b:  ldc.i4.1
  IL_003c:  stloc.0
  IL_003d:  br.s       IL_0041
  IL_003f:  ldarg.0
  IL_0040:  stloc.0
2213
  IL_0041:  ldloc.0
N
Neal Gafter 已提交
2214
  IL_0042:  ret
N
Neal Gafter 已提交
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
}");
        }

        [Fact]
        [WorkItem(20641, "https://github.com/dotnet/roslyn/issues/20641")]
        [WorkItem(1395, "https://github.com/dotnet/csharplang/issues/1395")]
        public void SharingTemps01()
        {
            var source =
@"class Program
{
    static void Main(string[] args) { }
    void M1(string x)
    {
        switch (x)
        {
            case ""a"":
            case ""b"" when Mutate(ref x): // prevents sharing temps
            case ""c"":
                break;
        }
    }
    void M2(string x)
    {
        switch (x)
        {
            case ""a"":
            case ""b"" when Pure(x):
            case ""c"":
                break;
        }
    }
    static bool Mutate(ref string x) { x = null; return false; }
    static bool Pure(string x) { return false; }
}";
2250
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularWithoutRecursivePatterns);
N
Neal Gafter 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
            compVerifier.VerifyIL("Program.M1",
@"{
  // Code size       53 (0x35)
  .maxstack  2
  .locals init (string V_0)
  IL_0000:  ldarg.1
  IL_0001:  stloc.0
  IL_0002:  ldloc.0
  IL_0003:  brfalse.s  IL_0034
  IL_0005:  ldloc.0
  IL_0006:  ldstr      ""a""
  IL_000b:  call       ""bool string.op_Equality(string, string)""
  IL_0010:  brtrue.s   IL_0034
  IL_0012:  ldloc.0
  IL_0013:  ldstr      ""b""
  IL_0018:  call       ""bool string.op_Equality(string, string)""
  IL_001d:  brtrue.s   IL_002c
  IL_001f:  ldloc.0
  IL_0020:  ldstr      ""c""
  IL_0025:  call       ""bool string.op_Equality(string, string)""
  IL_002a:  pop
  IL_002b:  ret
  IL_002c:  ldarga.s   V_1
  IL_002e:  call       ""bool Program.Mutate(ref string)""
  IL_0033:  pop
  IL_0034:  ret
}");
            compVerifier.VerifyIL("Program.M2",
@"{
  // Code size       50 (0x32)
  .maxstack  2
  IL_0000:  ldarg.1
  IL_0001:  brfalse.s  IL_0031
  IL_0003:  ldarg.1
  IL_0004:  ldstr      ""a""
  IL_0009:  call       ""bool string.op_Equality(string, string)""
  IL_000e:  brtrue.s   IL_0031
  IL_0010:  ldarg.1
  IL_0011:  ldstr      ""b""
  IL_0016:  call       ""bool string.op_Equality(string, string)""
  IL_001b:  brtrue.s   IL_002a
  IL_001d:  ldarg.1
  IL_001e:  ldstr      ""c""
  IL_0023:  call       ""bool string.op_Equality(string, string)""
  IL_0028:  pop
  IL_0029:  ret
  IL_002a:  ldarg.1
  IL_002b:  call       ""bool Program.Pure(string)""
  IL_0030:  pop
  IL_0031:  ret
2303 2304
}");
        }
2305

2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
        [Fact, WorkItem(17266, "https://github.com/dotnet/roslyn/issues/17266")]
        public void IrrefutablePatternInIs01()
        {
            var source =
@"using System;
public class C
{
    public static void Main()
    {
        if (Get() is int index) { }
        Console.WriteLine(index);
    }

    public static int Get()
    {
        Console.WriteLine(""eval"");
        return 1;
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"eval
1";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }

        [Fact, WorkItem(17266, "https://github.com/dotnet/roslyn/issues/17266")]
        public void IrrefutablePatternInIs02()
        {
            var source =
@"using System;
public class C
{
    public static void Main()
    {
        if (Get() is Assignment(int left, var right)) { }
        Console.WriteLine(left);
        Console.WriteLine(right);
    }

    public static Assignment Get()
    {
        Console.WriteLine(""eval"");
        return new Assignment(1, 2);
    }
}
public struct Assignment
{
    public int Left, Right;
    public Assignment(int left, int right) => (Left, Right) = (left, right);
    public void Deconstruct(out int left, out int right) => (left, right) = (Left, Right);
}
";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe, parseOptions: TestOptions.RegularWithRecursivePatterns);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"eval
1
2";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }

        [Fact]
        public void MissingNullCheck01()
        {
            var source =
@"class Program
{
    public static void Main()
    {
        string s = null;
        System.Console.WriteLine(s is string { Length: 3 });
    }
}
";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe, parseOptions: TestOptions.RegularWithRecursivePatterns);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"False";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }
2385

2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
        [Fact]
        [WorkItem(24550, "https://github.com/dotnet/roslyn/issues/24550")]
        [WorkItem(1284, "https://github.com/dotnet/csharplang/issues/1284")]
        public void ConstantPatternVsUnconstrainedTypeParameter01()
        {
            var source =
@"using System;
class Program
{
    static void Main()
    {
        Console.WriteLine(Test1<object>(null));
        Console.WriteLine(Test1<int>(1));
        Console.WriteLine(Test1<int?>(null));
        Console.WriteLine(Test1<int?>(1));

        Console.WriteLine(Test2<object>(0));
        Console.WriteLine(Test2<int>(1));
        Console.WriteLine(Test2<int?>(0));
        Console.WriteLine(Test2<string>(""frog""));

        Console.WriteLine(Test3<object>(""frog""));
        Console.WriteLine(Test3<int>(1));
        Console.WriteLine(Test3<string>(""frog""));
        Console.WriteLine(Test3<int?>(1));
    }

    public static bool Test1<T>(T t)
    {
        return t is null;
    }
    public static bool Test2<T>(T t)
    {
        return t is 0;
    }
    public static bool Test3<T>(T t)
    {
        return t is ""frog"";
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            var expectedOutput =
@"True
False
True
False
True
False
True
False
True
False
True
False
";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("Program.Test1<T>(T)",
@"{
  // Code size       10 (0xa)
  .maxstack  2
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  ldnull
  IL_0007:  ceq
  IL_0009:  ret
}");
            compVerifier.VerifyIL("Program.Test2<T>(T)",
@"{
2455
  // Code size       35 (0x23)
2456 2457 2458 2459
  .maxstack  2
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  isinst     ""int""
2460
  IL_000b:  brfalse.s  IL_0021
2461 2462
  IL_000d:  ldarg.0
  IL_000e:  box        ""T""
2463 2464 2465 2466 2467 2468 2469 2470 2471
  IL_0013:  isinst     ""int""
  IL_0018:  unbox.any  ""int""
  IL_001d:  ldc.i4.0
  IL_001e:  ceq
  IL_0020:  ret
  IL_0021:  ldc.i4.0
  IL_0022:  ret
}
");
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
            compVerifier.VerifyIL("Program.Test3<T>(T)",
@"{
  // Code size       29 (0x1d)
  .maxstack  2
  .locals init (string V_0)
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  isinst     ""string""
  IL_000b:  stloc.0
  IL_000c:  ldloc.0
  IL_000d:  brfalse.s  IL_001b
2483 2484
  IL_000f:  ldloc.0
  IL_0010:  ldstr      ""frog""
2485 2486 2487 2488
  IL_0015:  call       ""bool string.op_Equality(string, string)""
  IL_001a:  ret
  IL_001b:  ldc.i4.0
  IL_001c:  ret
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
}");
        }

        [Fact]
        [WorkItem(24550, "https://github.com/dotnet/roslyn/issues/24550")]
        [WorkItem(1284, "https://github.com/dotnet/csharplang/issues/1284")]
        public void ConstantPatternVsUnconstrainedTypeParameter02()
        {
            var source =
@"class C<T>
{
    internal struct S { }
2501
    static bool Test(S s)
2502 2503 2504 2505 2506 2507 2508
    {
        return s is 1;
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation);
2509
            compVerifier.VerifyIL("C<T>.Test(C<T>.S)",
2510
@"{
2511
  // Code size       35 (0x23)
2512 2513 2514 2515
  .maxstack  2
  IL_0000:  ldarg.0
  IL_0001:  box        ""C<T>.S""
  IL_0006:  isinst     ""int""
2516
  IL_000b:  brfalse.s  IL_0021
2517 2518
  IL_000d:  ldarg.0
  IL_000e:  box        ""C<T>.S""
2519 2520 2521 2522 2523 2524 2525 2526 2527
  IL_0013:  isinst     ""int""
  IL_0018:  unbox.any  ""int""
  IL_001d:  ldc.i4.1
  IL_001e:  ceq
  IL_0020:  ret
  IL_0021:  ldc.i4.0
  IL_0022:  ret
}
");
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
        }

        [Fact]
        [WorkItem(26274, "https://github.com/dotnet/roslyn/issues/26274")]
        public void VariablesInSwitchExpressionArms()
        {
            var source =
@"class C
{
    public override bool Equals(object obj) =>
        obj switch
        {
2540 2541
            C x1 when x1 is var x2 => x2 is var x3 && x3 is {},
            _ => false
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
        };
    public override int GetHashCode() => 1;
    public static void Main()
    {
        C c = new C();
        System.Console.Write(c.Equals(new C()));
        System.Console.Write(c.Equals(new object()));
    }
}
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation, expectedOutput: "TrueFalse");
            compVerifier.VerifyIL("C.Equals(object)",
@"{
  // Code size       25 (0x19)
  .maxstack  2
  .locals init (C V_0, //x1
                C V_1, //x2
                C V_2, //x3
                bool V_3)
  IL_0000:  ldarg.1
  IL_0001:  isinst     ""C""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brfalse.s  IL_0015
  IL_000a:  ldloc.0
  IL_000b:  stloc.1
  IL_000c:  ldloc.1
  IL_000d:  stloc.2
  IL_000e:  ldloc.2
  IL_000f:  ldnull
  IL_0010:  cgt.un
  IL_0012:  stloc.3
  IL_0013:  br.s       IL_0017
  IL_0015:  ldc.i4.0
  IL_0016:  stloc.3
  IL_0017:  ldloc.3
  IL_0018:  ret
2581 2582 2583
}");
        }

2584
        [Fact, WorkItem(26387, "https://github.com/dotnet/roslyn/issues/26387")]
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
        public void ValueTypeArgument01()
        {
            var source =
@"using System;

class TestHelper
{
    static void Main()
    {
        Console.WriteLine(IsValueTypeT0(new S()));
        Console.WriteLine(IsValueTypeT1(new S()));
        Console.WriteLine(IsValueTypeT2(new S()));
    }

    static bool IsValueTypeT0<T>(T result)
    {
        return result is T;
    }
    static bool IsValueTypeT1<T>(T result)
    {
        return result is T v;
    }
    static bool IsValueTypeT2<T>(T result)
    {
        return result is T _;
    }
}

struct S { }
";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            var expectedOutput =
@"True
True
True";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("TestHelper.IsValueTypeT0<T>(T)",
@"{
  // Code size       15 (0xf)
  .maxstack  2
  .locals init (bool V_0)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  box        ""T""
  IL_0007:  ldnull
  IL_0008:  cgt.un
  IL_000a:  stloc.0
  IL_000b:  br.s       IL_000d
  IL_000d:  ldloc.0
  IL_000e:  ret
}");
            compVerifier.VerifyIL("TestHelper.IsValueTypeT1<T>(T)",
@"{
2639 2640
  // Code size       20 (0x14)
  .maxstack  1
2641 2642 2643 2644
  .locals init (T V_0, //v
                bool V_1)
  IL_0000:  nop
  IL_0001:  ldarg.0
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
  IL_0002:  box        ""T""
  IL_0007:  brfalse.s  IL_000e
  IL_0009:  ldarg.0
  IL_000a:  stloc.0
  IL_000b:  ldc.i4.1
  IL_000c:  br.s       IL_000f
  IL_000e:  ldc.i4.0
  IL_000f:  stloc.1
  IL_0010:  br.s       IL_0012
  IL_0012:  ldloc.1
  IL_0013:  ret
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
}");
            compVerifier.VerifyIL("TestHelper.IsValueTypeT2<T>(T)",
@"{
  // Code size       15 (0xf)
  .maxstack  2
  .locals init (bool V_0)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  box        ""T""
  IL_0007:  ldnull
  IL_0008:  cgt.un
  IL_000a:  stloc.0
  IL_000b:  br.s       IL_000d
  IL_000d:  ldloc.0
  IL_000e:  ret
}");
        }

        [Fact, WorkItem(26387, "https://github.com/dotnet/roslyn/issues/26387")]
        public void ValueTypeArgument02()
        {
            var source =
@"namespace ConsoleApp1
{
    public class TestHelper
    {
        public static void Main()
        {
            IsValueTypeT(new Result<(Cls, IInt)>((new Cls(), new Int())));
            System.Console.WriteLine(""done"");
        }

        public static void IsValueTypeT<T>(Result<T> result)
        {
            if (!(result.Value is T v))
                throw new NotPossibleException();
        }
    }

    public class Result<T>
    {
        public T Value { get; }

        public Result(T value)
        {
            Value = value;
        }
    }

    public class Cls { }
    public interface IInt { }

    public class Int : IInt { }

    public class NotPossibleException : System.Exception { }
}";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            var expectedOutput =
@"done";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("ConsoleApp1.TestHelper.IsValueTypeT<T>(ConsoleApp1.Result<T>)",
@"{
2719
  // Code size       31 (0x1f)
2720 2721 2722 2723 2724 2725
  .maxstack  2
  .locals init (T V_0, //v
                bool V_1)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  callvirt   ""T ConsoleApp1.Result<T>.Value.get""
2726 2727
  IL_0007:  stloc.0
  IL_0008:  ldloc.0
2728 2729
  IL_0009:  box        ""T""
  IL_000e:  ldnull
2730 2731 2732 2733 2734 2735 2736 2737 2738
  IL_000f:  cgt.un
  IL_0011:  ldc.i4.0
  IL_0012:  ceq
  IL_0014:  stloc.1
  IL_0015:  ldloc.1
  IL_0016:  brfalse.s  IL_001e
  IL_0018:  newobj     ""ConsoleApp1.NotPossibleException..ctor()""
  IL_001d:  throw
  IL_001e:  ret
2739 2740
}");
        }
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815

        [Fact]
        public void DeconstructNullableTuple_01()
        {
            var source =
@"
class C {
    static int i = 3;
    static (int,int)? GetNullableTuple() => (i++, i++);

    static void Main() {
        if (GetNullableTuple() is (int x1, int y1) tupl1)
        {
            System.Console.WriteLine($""x = {x1}, y = {y1}"");
        }
        if (GetNullableTuple() is (int x2, int y2) _)
        {
            System.Console.WriteLine($""x = {x2}, y = {y2}"");
        }
        switch (GetNullableTuple())
        {
            case (int x3, int y3) s:
                System.Console.WriteLine($""x = {x3}, y = {y3}"");
                break;
        }
    }
}";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"x = 3, y = 4
x = 5, y = 6
x = 7, y = 8";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }

        [Fact]
        public void DeconstructNullable_01()
        {
            var source =
@"
class C {
    static int i = 3;
    static S? GetNullableTuple() => new S(i++, i++);

    static void Main() {
        if (GetNullableTuple() is (int x1, int y1) tupl1)
        {
            System.Console.WriteLine($""x = {x1}, y = {y1}"");
        }
        if (GetNullableTuple() is (int x2, int y2) _)
        {
            System.Console.WriteLine($""x = {x2}, y = {y2}"");
        }
        switch (GetNullableTuple())
        {
            case (int x3, int y3) s:
                System.Console.WriteLine($""x = {x3}, y = {y3}"");
                break;
        }
    }
}
struct S
{
    int x, y;
    public S(int X, int Y) => (this.x, this.y) = (X, Y);
    public void Deconstruct(out int X, out int Y) => (X, Y) = (x, y);
}
";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"x = 3, y = 4
x = 5, y = 6
x = 7, y = 8";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883

        [Fact, WorkItem(28632, "https://github.com/dotnet/roslyn/issues/28632")]
        public void UnboxNullableInRecursivePattern01()
        {
            var source =
@"
static class Program
{
    public static int M1(int? x)
    {
        return x is int y ? y : 1;
    }
    public static int M2(int? x)
    {
        return x is { } y ? y : 2;
    }
    public static void Main()
    {
        System.Console.WriteLine(M1(null));
        System.Console.WriteLine(M2(null));
        System.Console.WriteLine(M1(3));
        System.Console.WriteLine(M2(4));
    }
}
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            var expectedOutput =
@"1
2
3
4";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("Program.M1",
@"{
  // Code size       23 (0x17)
  .maxstack  1
  .locals init (int V_0) //y
  IL_0000:  ldarga.s   V_0
  IL_0002:  call       ""bool int?.HasValue.get""
  IL_0007:  brfalse.s  IL_0013
  IL_0009:  ldarga.s   V_0
  IL_000b:  call       ""int int?.GetValueOrDefault()""
  IL_0010:  stloc.0
  IL_0011:  br.s       IL_0015
  IL_0013:  ldc.i4.1
  IL_0014:  ret
  IL_0015:  ldloc.0
  IL_0016:  ret
}");
            compVerifier.VerifyIL("Program.M2",
@"{
  // Code size       23 (0x17)
  .maxstack  1
  .locals init (int V_0) //y
  IL_0000:  ldarga.s   V_0
  IL_0002:  call       ""bool int?.HasValue.get""
  IL_0007:  brfalse.s  IL_0013
  IL_0009:  ldarga.s   V_0
  IL_000b:  call       ""int int?.GetValueOrDefault()""
  IL_0010:  stloc.0
  IL_0011:  br.s       IL_0015
  IL_0013:  ldc.i4.2
  IL_0014:  ret
  IL_0015:  ldloc.0
  IL_0016:  ret
}");
        }
N
Neal Gafter 已提交
2884

2885
        [Fact]
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
        public void DoNotShareInputForMutatingWhenClause()
        {
            var source =
@"using System;
class Program
{
    static void Main()
    {
        Console.Write(M1(1));
        Console.Write(M2(1));
        Console.Write(M3(1));
        Console.Write(M4(1));
        Console.Write(M5(1));
        Console.Write(M6(1));
2900 2901 2902
        Console.Write(M7(1));
        Console.Write(M8(1));
        Console.Write(M9(1));
2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
    }
    public static int M1(int x)
    {
        return x switch { _ when (++x) == 5 => 1, 1 => 2, _ => 3 };
    }
    public static int M2(int x)
    {
        return x switch { _ when (x+=1) == 5 => 1, 1 => 2, _ => 3 };
    }
    public static int M3(int x)
    {
        return x switch { _ when ((x, _) = (5, 6)) == (0, 0) => 1, 1 => 2, _ => 3 };
    }
    public static int M4(int x)
    {
        dynamic d = new Program();
        return x switch { _ when d.M(ref x) => 1, 1 => 2, _ => 3 };
    }
    bool M(ref int x) { x = 100; return false; }
    public static int M5(int x)
    {
        return x switch { _ when new Program(ref x).P => 1, 1 => 2, _ => 3 };
    }
    public static int M6(int x)
    {
        dynamic d = x;
        return x switch { _ when new Program(d, ref x).P => 1, 1 => 2, _ => 3 };
    }
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
    public static int M7(int x)
    {
        return x switch { _ when new Program(ref x).P && new Program().P => 1, 1 => 2, _ => 3 };
    }
    public static int M8(int x)
    {
        dynamic d = x;
        return x switch { _ when new Program(d, ref x).P && new Program().P => 1, 1 => 2, _ => 3 };
    }
    public static int M9(int x)
    {
        return x switch { _ when (x=100) == 1 => 1, 1 => 2, _ => 3 };
    }
2944 2945 2946 2947 2948 2949 2950 2951
    Program() { }
    Program(ref int x) { x = 100; }
    Program(int a, ref int x) { x = 100; }
    bool P => false;
}
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe, references: new[] { CSharpRef });
            compilation.VerifyDiagnostics();
2952
            var expectedOutput = @"222222222";
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }

        [Fact]
        public void GenerateStringHashOnlyOnce()
        {
            var source =
@"using System;
class Program
{
    static void Main()
    {
        Console.Write(M1(string.Empty));
        Console.Write(M2(string.Empty));
    }
    public static int M1(string s)
    {
        return s switch { ""a""=>1, ""b""=>2, ""c""=>3, ""d""=>4, ""e""=>5, ""f""=>6, ""g""=>7, ""h""=>8, _ => 9 };
    }
    public static int M2(string s)
    {
        return s switch { ""a""=>1, ""b""=>2, ""c""=>3, ""d""=>4, ""e""=>5, ""f""=>6, ""g""=>7, ""h""=>8, _ => 9 };
    }
}
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"99";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }

        [Fact]
        public void BindVariablesInWhenClause()
        {
            var source =
@"using System;
class Program
{
    static void Main()
    {
        var t = (1, 2);
        switch (t)
        {
            case var (x, y) when x+1 == y:
                Console.Write(1);
                break;
        }
        Console.Write(t switch
        {
            var (x, y) when x+1 == y => 1,
            _ => 2
        });
    }
}
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"11";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }
3013

N
Neal Gafter 已提交
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
        [Fact]
        public void MissingExceptions_01()
        {
            var source = @"namespace System {
    public class Object { }
    public abstract class ValueType { }
    public struct Void { }
    public struct Boolean { }
    public struct Int32 { }
}
static class C {
    public static bool M(int i) => i switch { 1 => true };
}
";
            var compilation = CreateEmptyCompilation(source, options: TestOptions.ReleaseDll);
            compilation.GetDiagnostics().Verify(
                // (9,38): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
                //     public static bool M(int i) => i switch { 1 => true };
                Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 38)
                );
            compilation.GetEmitDiagnostics().Verify(
                // warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
                Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1),
                // (9,36): error CS0656: Missing compiler required member 'System.InvalidOperationException..ctor'
                //     public static bool M(int i) => i switch { 1 => true };
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "i switch { 1 => true }").WithArguments("System.InvalidOperationException", ".ctor").WithLocation(9, 36),
                // (9,38): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive).
                //     public static bool M(int i) => i switch { 1 => true };
                Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 38)
                );
        }
3045

3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
        [Fact, WorkItem(32774, "https://github.com/dotnet/roslyn/issues/32774")]
        public void BadCode_32774()
        {
            var source = @"
public class Class1
{
    static void Main()
    {
        System.Console.WriteLine(SwitchCaseThatFails(12.123));
    }

    public static bool SwitchCaseThatFails(object someObject)
    {
        switch (someObject)
        {
            case IObject x when x.SubObject != null:
                return false;
            case IOtherObject x:
                return false;
            case double x:
                return true;
            default:
                return false;
        }
    }

    public interface IObject
    {
        IObject SubObject { get; }
    }

    public interface IOtherObject { }
}";
            var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            var expectedOutput = @"True";
            var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compVerifier.VerifyIL("Class1.SwitchCaseThatFails",
@"{
  // Code size       97 (0x61)
  .maxstack  1
  .locals init (Class1.IObject V_0, //x
                Class1.IOtherObject V_1, //x
                double V_2, //x
                object V_3,
                object V_4,
                bool V_5)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  stloc.s    V_4
  IL_0004:  ldloc.s    V_4
  IL_0006:  stloc.3
  IL_0007:  ldloc.3
  IL_0008:  isinst     ""Class1.IObject""
  IL_000d:  stloc.0
  IL_000e:  ldloc.0
  IL_000f:  brtrue.s   IL_003c
  IL_0011:  br.s       IL_001f
  IL_0013:  ldloc.3
  IL_0014:  isinst     ""Class1.IOtherObject""
  IL_0019:  stloc.1
  IL_001a:  ldloc.1
  IL_001b:  brtrue.s   IL_004b
  IL_001d:  br.s       IL_0059
  IL_001f:  ldloc.3
  IL_0020:  isinst     ""Class1.IOtherObject""
  IL_0025:  stloc.1
  IL_0026:  ldloc.1
  IL_0027:  brtrue.s   IL_004b
  IL_0029:  br.s       IL_002b
  IL_002b:  ldloc.3
  IL_002c:  isinst     ""double""
  IL_0031:  brfalse.s  IL_0059
  IL_0033:  ldloc.3
  IL_0034:  unbox.any  ""double""
  IL_0039:  stloc.2
  IL_003a:  br.s       IL_0052
  IL_003c:  ldloc.0
  IL_003d:  callvirt   ""Class1.IObject Class1.IObject.SubObject.get""
  IL_0042:  brtrue.s   IL_0046
  IL_0044:  br.s       IL_0013
  IL_0046:  ldc.i4.0
  IL_0047:  stloc.s    V_5
  IL_0049:  br.s       IL_005e
  IL_004b:  br.s       IL_004d
  IL_004d:  ldc.i4.0
  IL_004e:  stloc.s    V_5
  IL_0050:  br.s       IL_005e
  IL_0052:  br.s       IL_0054
  IL_0054:  ldc.i4.1
  IL_0055:  stloc.s    V_5
  IL_0057:  br.s       IL_005e
  IL_0059:  ldc.i4.0
  IL_005a:  stloc.s    V_5
  IL_005c:  br.s       IL_005e
  IL_005e:  ldloc.s    V_5
  IL_0060:  ret
}");
        }
3145

N
Neal Gafter 已提交
3146 3147
        // Possible test helper bug on Linux; see https://github.com/dotnet/roslyn/issues/33356
        [ConditionalFact(typeof(WindowsOnly))]
3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
        public void SwitchExpressionSequencePoints()
        {
            string source = @"
public class Program
{
    public static void Main()
    {
        int i = 0;
        var y = (i switch
        {
            0 => new Program(),
            1 => new Program(),
            _ => new Program(),
        }).Chain();
        y.Chain2();
    }
    public Program Chain() => this;
    public Program Chain2() => this;
}
";
            var v = CompileAndVerify(source, options: TestOptions.DebugExe);
3169
            v.VerifyIL(qualifiedMethodName: "Program.Main",
3170 3171 3172 3173 3174 3175 3176
@"{
  // Code size       55 (0x37)
  .maxstack  2
  .locals init (int V_0, //i
                Program V_1, //y
                Program V_2,
                Program V_3)
3177 3178 3179 3180
  // sequence point: {
  IL_0000:  nop
  // sequence point: int i = 0;
  IL_0001:  ldc.i4.0
3181
  IL_0002:  stloc.0
3182 3183
  // sequence point: var y = (i s ...   }).Chain()
  IL_0003:  ldloc.0
3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
  IL_0004:  brfalse.s  IL_000e
  IL_0006:  br.s       IL_0008
  IL_0008:  ldloc.0
  IL_0009:  ldc.i4.1
  IL_000a:  beq.s      IL_0016
  IL_000c:  br.s       IL_001e
  IL_000e:  newobj     ""Program..ctor()""
  IL_0013:  stloc.2
  IL_0014:  br.s       IL_0026
  IL_0016:  newobj     ""Program..ctor()""
  IL_001b:  stloc.2
  IL_001c:  br.s       IL_0026
  IL_001e:  newobj     ""Program..ctor()""
  IL_0023:  stloc.2
  IL_0024:  br.s       IL_0026
  IL_0026:  ldloc.2
  IL_0027:  stloc.3
  IL_0028:  ldloc.3
  IL_0029:  callvirt   ""Program Program.Chain()""
  IL_002e:  stloc.1
3204 3205
  // sequence point: y.Chain2();
  IL_002f:  ldloc.1
3206 3207
  IL_0030:  callvirt   ""Program Program.Chain2()""
  IL_0035:  pop
3208 3209
  // sequence point: }
  IL_0036:  ret
3210
}
3211
", sequencePoints: "Program.Main", source: source);
3212
        }
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298

        [Fact, WorkItem(33675, "https://github.com/dotnet/roslyn/issues/33675")]
        public void ParsingParenthesizedExpressionAsPatternOfExpressionSwitch()
        {
            var source = @"
public class Class1
{
    static void Main()
    {
        System.Console.Write(M(42));
        System.Console.Write(M(41));
    }

    static bool M(object o)
    {
        const int X = 42;
        return o switch { (X) => true, _ => false };
    }
}";
            foreach (var options in new[] { TestOptions.DebugExe, TestOptions.ReleaseExe })
            {
                var compilation = CreateCompilation(source, options: options);
                compilation.VerifyDiagnostics();
                var expectedOutput = @"TrueFalse";
                var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
                if (options.OptimizationLevel == OptimizationLevel.Debug)
                {
                    compVerifier.VerifyIL("Class1.M",
@"{
  // Code size       39 (0x27)
  .maxstack  2
  .locals init (bool V_0,
                int V_1,
                bool V_2,
                bool V_3)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  isinst     ""int""
  IL_0007:  brfalse.s  IL_001b
  IL_0009:  ldarg.0
  IL_000a:  unbox.any  ""int""
  IL_000f:  stloc.1
  IL_0010:  ldloc.1
  IL_0011:  ldc.i4.s   42
  IL_0013:  beq.s      IL_0017
  IL_0015:  br.s       IL_001b
  IL_0017:  ldc.i4.1
  IL_0018:  stloc.0
  IL_0019:  br.s       IL_001f
  IL_001b:  ldc.i4.0
  IL_001c:  stloc.0
  IL_001d:  br.s       IL_001f
  IL_001f:  ldloc.0
  IL_0020:  stloc.2
  IL_0021:  ldloc.2
  IL_0022:  stloc.3
  IL_0023:  br.s       IL_0025
  IL_0025:  ldloc.3
  IL_0026:  ret
}");
                }
                else
                {
                    compVerifier.VerifyIL("Class1.M",
@"{
  // Code size       26 (0x1a)
  .maxstack  2
  .locals init (bool V_0)
  IL_0000:  ldarg.0
  IL_0001:  isinst     ""int""
  IL_0006:  brfalse.s  IL_0016
  IL_0008:  ldarg.0
  IL_0009:  unbox.any  ""int""
  IL_000e:  ldc.i4.s   42
  IL_0010:  bne.un.s   IL_0016
  IL_0012:  ldc.i4.1
  IL_0013:  stloc.0
  IL_0014:  br.s       IL_0018
  IL_0016:  ldc.i4.0
  IL_0017:  stloc.0
  IL_0018:  ldloc.0
  IL_0019:  ret
}");
                }
            }
        }
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367

        [Fact, WorkItem(35584, "https://github.com/dotnet/roslyn/issues/35584")]
        public void MatchToTypeParameterUnbox_01()
        {
            var source = @"
class Program
{
    public static void Main() => System.Console.WriteLine(P<int>(0));
    public static string P<T>(T t) => (t is object o) ? o.ToString() : string.Empty;
}
";
            var expectedOutput = @"0";
            foreach (var options in new[] { TestOptions.DebugExe, TestOptions.ReleaseExe })
            {
                var compilation = CreateCompilation(source, options: options);
                compilation.VerifyDiagnostics();
                var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
                if (options.OptimizationLevel == OptimizationLevel.Debug)
                {
                    compVerifier.VerifyIL("Program.P<T>",
@"{
  // Code size       24 (0x18)
  .maxstack  1
  .locals init (object V_0) //o
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brtrue.s   IL_0011
  IL_000a:  ldsfld     ""string string.Empty""
  IL_000f:  br.s       IL_0017
  IL_0011:  ldloc.0
  IL_0012:  callvirt   ""string object.ToString()""
  IL_0017:  ret
}
");
                }
                else
                {
                    compVerifier.VerifyIL("Program.P<T>",
@"{
  // Code size       23 (0x17)
  .maxstack  1
  .locals init (object V_0) //o
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brtrue.s   IL_0010
  IL_000a:  ldsfld     ""string string.Empty""
  IL_000f:  ret
  IL_0010:  ldloc.0
  IL_0011:  callvirt   ""string object.ToString()""
  IL_0016:  ret
}
");
                }
            }
        }

        [Fact, WorkItem(35584, "https://github.com/dotnet/roslyn/issues/35584")]
        public void MatchToTypeParameterUnbox_02()
        {
            var source =
@"using System;
 class Program
{
    public static void Main()
    {
N
Neal Gafter 已提交
3368
        var generic = new Generic<int>(0);
3369 3370
    }
}
N
Neal Gafter 已提交
3371
public class Generic<T>
3372
{
N
Neal Gafter 已提交
3373
    public Generic(T value)
3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
    {
        if (value is object obj && obj == null)
        {
            throw new Exception(""Kaboom!"");
        }
    }
}
";
            var expectedOutput = @"";
            foreach (var options in new[] { TestOptions.DebugExe, TestOptions.ReleaseExe })
            {
                var compilation = CreateCompilation(source, options: options);
                compilation.VerifyDiagnostics();
                var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
                if (options.OptimizationLevel == OptimizationLevel.Debug)
                {
N
Neal Gafter 已提交
3390
                    compVerifier.VerifyIL("Generic<T>..ctor(T)",
3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
@"{
  // Code size       42 (0x2a)
  .maxstack  2
  .locals init (object V_0, //obj
                bool V_1)
  IL_0000:  ldarg.0
  IL_0001:  call       ""object..ctor()""
  IL_0006:  nop
  IL_0007:  nop
  IL_0008:  ldarg.1
  IL_0009:  box        ""T""
  IL_000e:  stloc.0
  IL_000f:  ldloc.0
  IL_0010:  brfalse.s  IL_0018
  IL_0012:  ldloc.0
  IL_0013:  ldnull
  IL_0014:  ceq
  IL_0016:  br.s       IL_0019
  IL_0018:  ldc.i4.0
  IL_0019:  stloc.1
  IL_001a:  ldloc.1
  IL_001b:  brfalse.s  IL_0029
  IL_001d:  nop
  IL_001e:  ldstr      ""Kaboom!""
  IL_0023:  newobj     ""System.Exception..ctor(string)""
  IL_0028:  throw
  IL_0029:  ret
}
");
                }
                else
                {
N
Neal Gafter 已提交
3423
                    compVerifier.VerifyIL("Generic<T>..ctor(T)",
3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
@"{
  // Code size       31 (0x1f)
  .maxstack  1
  .locals init (object V_0) //obj
  IL_0000:  ldarg.0
  IL_0001:  call       ""object..ctor()""
  IL_0006:  ldarg.1
  IL_0007:  box        ""T""
  IL_000c:  stloc.0
  IL_000d:  ldloc.0
  IL_000e:  brfalse.s  IL_001e
  IL_0010:  ldloc.0
  IL_0011:  brtrue.s   IL_001e
  IL_0013:  ldstr      ""Kaboom!""
  IL_0018:  newobj     ""System.Exception..ctor(string)""
  IL_001d:  throw
  IL_001e:  ret
}
");
                }
            }
        }

        [Fact, WorkItem(35584, "https://github.com/dotnet/roslyn/issues/35584")]
        public void MatchToTypeParameterUnbox_03()
        {
            var source =
@"using System;
class Program
{
    public static void Main() => System.Console.WriteLine(P<Enum>(null));
    public static string P<T>(T t) where T: Enum => (t is ValueType o) ? o.ToString() : ""1"";
}
";
            var expectedOutput = @"1";
            foreach (var options in new[] { TestOptions.DebugExe, TestOptions.ReleaseExe })
            {
                var compilation = CreateCompilation(source, options: options);
                compilation.VerifyDiagnostics();
                var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
                if (options.OptimizationLevel == OptimizationLevel.Debug)
                {
                    compVerifier.VerifyIL("Program.P<T>",
@"{
  // Code size       24 (0x18)
  .maxstack  1
  .locals init (System.ValueType V_0) //o
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brtrue.s   IL_0011
  IL_000a:  ldstr      ""1""
  IL_000f:  br.s       IL_0017
  IL_0011:  ldloc.0
  IL_0012:  callvirt   ""string object.ToString()""
  IL_0017:  ret
}
");
                }
                else
                {
                    compVerifier.VerifyIL("Program.P<T>",
@"{
  // Code size       23 (0x17)
  .maxstack  1
  .locals init (System.ValueType V_0) //o
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  brtrue.s   IL_0010
  IL_000a:  ldstr      ""1""
  IL_000f:  ret
  IL_0010:  ldloc.0
  IL_0011:  callvirt   ""string object.ToString()""
  IL_0016:  ret
}
");
                }
            }
        }
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716

        [Fact]
        public void CompileTimeRuntimeInstanceofMismatch_01()
        {
            var source =
@"using System;
class Program
{
    static void Main()
    {
        M(new byte[1]);
        M(new sbyte[1]);
        M(new Ebyte[1]);
        M(new Esbyte[1]);

        M(new short[1]);
        M(new ushort[1]);
        M(new Eshort[1]);
        M(new Eushort[1]);

        M(new int[1]);
        M(new uint[1]);
        M(new Eint[1]);
        M(new Euint[1]);

        M(new int[1][]);
        M(new uint[1][]);
        M(new Eint[1][]);
        M(new Euint[1][]);

        M(new long[1]);
        M(new ulong[1]);
        M(new Elong[1]);
        M(new Eulong[1]);

        M(new IntPtr[1]);
        M(new UIntPtr[1]);

        M(new IntPtr[1][]);
        M(new UIntPtr[1][]);
    }

    static void M(object o)
    {
        switch (o)
        {
            case byte[] _ when o.GetType() == typeof(byte[]):
                Console.WriteLine(""byte[]"");
                break;
            case sbyte[] _ when o.GetType() == typeof(sbyte[]):
                Console.WriteLine(""sbyte[]"");
                break;
            case Ebyte[] _ when o.GetType() == typeof(Ebyte[]):
                Console.WriteLine(""Ebyte[]"");
                break;
            case Esbyte[] _ when o.GetType() == typeof(Esbyte[]):
                Console.WriteLine(""Esbyte[]"");
                break;

            case short[] _ when o.GetType() == typeof(short[]):
                Console.WriteLine(""short[]"");
                break;
            case ushort[] _ when o.GetType() == typeof(ushort[]):
                Console.WriteLine(""ushort[]"");
                break;
            case Eshort[] _ when o.GetType() == typeof(Eshort[]):
                Console.WriteLine(""Eshort[]"");
                break;
            case Eushort[] _ when o.GetType() == typeof(Eushort[]):
                Console.WriteLine(""Eushort[]"");
                break;

            case int[] _ when o.GetType() == typeof(int[]):
                Console.WriteLine(""int[]"");
                break;
            case uint[] _ when o.GetType() == typeof(uint[]):
                Console.WriteLine(""uint[]"");
                break;
            case Eint[] _ when o.GetType() == typeof(Eint[]):
                Console.WriteLine(""Eint[]"");
                break;
            case Euint[] _ when o.GetType() == typeof(Euint[]):
                Console.WriteLine(""Euint[]"");
                break;

            case int[][] _ when o.GetType() == typeof(int[][]):
                Console.WriteLine(""int[][]"");
                break;
            case uint[][] _ when o.GetType() == typeof(uint[][]):
                Console.WriteLine(""uint[][]"");
                break;
            case Eint[][] _ when o.GetType() == typeof(Eint[][]):
                Console.WriteLine(""Eint[][]"");
                break;
            case Euint[][] _ when o.GetType() == typeof(Euint[][]):
                Console.WriteLine(""Euint[][]"");
                break;

            case long[] _ when o.GetType() == typeof(long[]):
                Console.WriteLine(""long[]"");
                break;
            case ulong[] _ when o.GetType() == typeof(ulong[]):
                Console.WriteLine(""ulong[]"");
                break;
            case Elong[] _ when o.GetType() == typeof(Elong[]):
                Console.WriteLine(""Elong[]"");
                break;
            case Eulong[] _ when o.GetType() == typeof(Eulong[]):
                Console.WriteLine(""Eulong[]"");
                break;

            case IntPtr[] _ when o.GetType() == typeof(IntPtr[]):
                Console.WriteLine(""IntPtr[]"");
                break;
            case UIntPtr[] _ when o.GetType() == typeof(UIntPtr[]):
                Console.WriteLine(""UIntPtr[]"");
                break;

            case IntPtr[][] _ when o.GetType() == typeof(IntPtr[][]):
                Console.WriteLine(""IntPtr[][]"");
                break;
            case UIntPtr[][] _ when o.GetType() == typeof(UIntPtr[][]):
                Console.WriteLine(""UIntPtr[][]"");
                break;

            default:
                Console.WriteLine(""oops: "" + o.GetType());
                break;
        }
    }
}
enum Ebyte : byte {}
enum Esbyte : sbyte {}
enum Eshort : short {}
enum Eushort : ushort {}
enum Eint : int {}
enum Euint : uint {}
enum Elong : long {}
enum Eulong : ulong {}
";
            var expectedOutput =
@"byte[]
sbyte[]
Ebyte[]
Esbyte[]
short[]
ushort[]
Eshort[]
Eushort[]
int[]
uint[]
Eint[]
Euint[]
int[][]
uint[][]
Eint[][]
Euint[][]
long[]
ulong[]
Elong[]
Eulong[]
IntPtr[]
UIntPtr[]
IntPtr[][]
UIntPtr[][]
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }

        [Fact]
        public void CompileTimeRuntimeInstanceofMismatch_02()
        {
            var source =
@"using System;
class Program
{
    static void Main()
    {
        M(new byte[1]);
        M(new sbyte[1]);
    }
    static void M(object o)
    {
        switch (o)
        {
            case byte[] _:
                Console.WriteLine(""byte[]"");
                break;
            case sbyte[] _: // not subsumed, even though it will never occur due to CLR behavior
                Console.WriteLine(""sbyte[]"");
                break;
        }
    }
}
";
            var expectedOutput =
@"byte[]
byte[]
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            CompileAndVerify(compilation, expectedOutput: expectedOutput);
            compilation = CreateCompilation(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics();
            CompileAndVerify(compilation, expectedOutput: expectedOutput);
        }
3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788

        [Fact]
        [WorkItem(36496, "https://github.com/dotnet/roslyn/issues/36496")]
        public void EmptyVarPatternVsDeconstruct()
        {
            var source =
@"using System;
public class C
{
    public static void Main()
    {
        Console.Write(M(new C()));
        Console.Write(M(null));
    }
    public static bool M(C c)
    {
        return c is var ();
    }
    public void Deconstruct() { }
}
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation, expectedOutput: "TrueFalse");
            compVerifier.VerifyIL("C.M(C)",
@"
{
    // Code size        5 (0x5)
    .maxstack  2
    IL_0000:  ldarg.0
    IL_0001:  ldnull
    IL_0002:  cgt.un
    IL_0004:  ret
}
");
        }

        [Fact]
        [WorkItem(36496, "https://github.com/dotnet/roslyn/issues/36496")]
        public void EmptyPositionalPatternVsDeconstruct()
        {
            var source =
@"using System;
public class C
{
    public static void Main()
    {
        Console.Write(M(new C()));
        Console.Write(M(null));
    }
    public static bool M(C c)
    {
        return c is ();
    }
    public void Deconstruct() { }
}
";
            var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
            compilation.VerifyDiagnostics();
            var compVerifier = CompileAndVerify(compilation, expectedOutput: "TrueFalse");
            compVerifier.VerifyIL("C.M(C)",
@"
{
    // Code size        5 (0x5)
    .maxstack  2
    IL_0000:  ldarg.0
    IL_0001:  ldnull
    IL_0002:  cgt.un
    IL_0004:  ret
}
");
        }
3789 3790
    }
}