CodeGenOptimizedNullableOperators.cs 67.1 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.CSharp.UnitTests.Emit;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
{
    public partial class CodeGenOptimizedNullableOperatorTests : CSharpTestBase
    {
        [Fact]
        public void TestNullableBoxingConversionsAlwaysNull()
        {
            // The native compiler does not optimize this case; Roslyn does. We know
            // that the result of boxing default(int?) to object is the same as casting
            // literal null to object, so we do not need to allocate space on the stack 
            // for the nullable int, initialize it, and then box that to a null ref.

23
            string[] sources = {
P
Pilchie 已提交
24 25 26 27 28 29 30
@"class Program
{
    static void Main()
    {
        System.Console.WriteLine((object)default(int?));
    }
}
31
",
P
Pilchie 已提交
32 33 34 35 36 37 38 39

@"class Program
{
    static void Main()
    {
        System.Console.WriteLine((object)(new int?()));
    }
}
40
",
P
Pilchie 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

@"class Program
{
    static void Main()
    {
        System.Console.WriteLine((object)(int?)null);
    }
}
"};

            string expectedOutput = "";
            string expectedIL = @"{
  // Code size        7 (0x7)
  .maxstack  1
  IL_0000:  ldnull
  IL_0001:  call       ""void System.Console.WriteLine(object)""
  IL_0006:  ret
}";

            foreach (string source in sources)
            {
                var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
                comp.VerifyDiagnostics();
                comp.VerifyIL("Program.Main", expectedIL);
            }
        }

        [Fact]
        public void TestNullableBoxingConversionNeverNull()
        {
            // The native compiler does not optimize this case; Roslyn does. We know
            // that the result of boxing default(int?) to object is the same as casting
            // literal null to object, so we do not need to allocate space on the stack 
            // for the nullable int, initialize it, and then box that to a null ref.

76
            string[] sources = {
P
Pilchie 已提交
77 78 79 80 81 82 83
@"class Program
{
    static void Main()
    {
        System.Console.WriteLine((object)new int?(123));
    }
}
84
",
P
Pilchie 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

@"class Program
{
    static void Main()
    {
        System.Console.WriteLine((object)(int?)123);
    }
}
"};

            string expectedOutput = "123";
            string expectedIL = @"{
  // Code size       13 (0xd)
  .maxstack  1
  IL_0000:  ldc.i4.s   123
  IL_0002:  box        ""int""
  IL_0007:  call       ""void System.Console.WriteLine(object)""
  IL_000c:  ret
}";

            foreach (string source in sources)
            {
                var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
                comp.VerifyDiagnostics();
                comp.VerifyIL("Program.Main", expectedIL);
            }
        }

        [Fact]
        public void TestNullableConversionAlwaysNull()
        {
            // A built-in nullable conversion whose argument is known to always be null
            // can simply be optimized away to be the null result.

            string source = @"
class Program
{
    static long? M()
    {
        return new int?();
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init (long? V_0)
  IL_0000:  ldloca.s   V_0
  IL_0002:  initobj    ""long?""
  IL_0008:  ldloc.0
  IL_0009:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M", expectedIL);
        }

        [Fact]
        public void TestNullableConversionNeverNull()
        {
            // A built-in nullable conversion whose argument is known to be non-null
            // can be generated by converting the value to the underlying target type,
            // and then converting that to nullable, without generating the nullable source
            // or checking to see if it has a value.

            string source = @"
class Program
{
    static long? M(int x)
    {
        return new int?(x);
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  conv.i8
  IL_0002:  newobj     ""long?..ctor(long)""
  IL_0007:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M", expectedIL);
        }

        [Fact]
        public void TestLiftedUserDefinedConversionAlwaysNull()
        {
            // A user-defined nullable conversion whose argument is known to always be null
            // can simply be optimized away to be the null result.

            string source = @"
struct S
{
    public static implicit operator S(int x) { return new S(); }
}
class Program
{
    static S? M()
    {
        return new int?();
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init (S? V_0)
  IL_0000:  ldloca.s   V_0
  IL_0002:  initobj    ""S?""
  IL_0008:  ldloc.0
  IL_0009:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M", expectedIL);
        }

        [Fact]
        public void TestLiftedUserDefinedConversionNeverNull()
        {
            // A user-defined nullable conversion whose argument is known to never be null
            // can have the nullable ctor, temporary store and value test optimized away.

            string source = @"
struct S
{
    public static implicit operator S(int x) { return new S(); }
    public static implicit operator string(S s) { return s.ToString(); }
}


class Program
{
    static S? M1(int x)
    {
        return new int?(x);
    }

    static S M2(int x)
    {
        return (S)(new int?(x));
    }

    static string M3(int x)
    {
        // The non-null conversion optimizer should chain well.  That is,
        // we first optimize (string)(S?)(new int?(x)) to (string)(new S?((S)x)), and then
        // to (string)(S)x.

        return (string)(S?)(new int?(x));
    }

    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       12 (0xc)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  call       ""S S.op_Implicit(int)""
  IL_0006:  newobj     ""S?..ctor(S)""
  IL_000b:  ret
}";
            string expectedIL2 = @"{
  // Code size        7 (0x7)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  call       ""S S.op_Implicit(int)""
  IL_0006:  ret
}";

            string expectedIL3 = @"{
  // Code size       12 (0xc)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  call       ""S S.op_Implicit(int)""
  IL_0006:  call       ""string S.op_Implicit(S)""
  IL_000b:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
            comp.VerifyIL("Program.M3", expectedIL3);
        }

        [Fact]
        public void TestNullableUnaryOpsAlwaysNull()
        {
            // A unary operator whose argument is known to always be null
            // can simply be optimized away to be the null result.

            string source = @"
class Program
{
    static int? M()
    {
        return ~(new int?());
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init (int? V_0)
  IL_0000:  ldloca.s   V_0
  IL_0002:  initobj    ""int?""
  IL_0008:  ldloc.0
  IL_0009:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics(
// (6,16): warning CS0458: The result of the expression is always 'null' of type 'int?'
//         return ~(new int?());
Diagnostic(ErrorCode.WRN_AlwaysNull, "~(new int?())").WithArguments("int?"));
            comp.VerifyIL("Program.M", expectedIL);
        }

        [Fact]
        public void TestNullableUnaryOpsAlwaysNullChained()
        {
            // A unary operator whose argument is known to always be null
            // can simply be optimized away to be the null result. These
            // optimizations should "chain" naturally. Here we combine
            // a built-in conversion, two unary operations, and a boxing.
            // The net result should simply be a null reference.
            //
            // The native compiler does not handle these "chained" optimizations,
            // interestingly enough; the native compiler will optimize away only the
            // innermost one; it is then not treated as "always null" and is checked
            // for nullity unnecessarily.
            //
            // Fortunately, the warning logic does not do a deep analysis; it only
            // reports a single warning.

            string source = @"
class Program
{
    static object M()
    {
        return ~-(new short?());
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size        2 (0x2)
  .maxstack  1
  IL_0000:  ldnull
  IL_0001:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics(
357 358
    // (6,17): warning CS0458: The result of the expression is always 'null' of type 'int?'
    //         return ~-(new short?());
P
Pilchie 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 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 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    Diagnostic(ErrorCode.WRN_AlwaysNull, "-(new short?())").WithArguments("int?"));
            comp.VerifyIL("Program.M", expectedIL);
        }

        [Fact]
        public void TestNullableUnaryOpsNeverNull()
        {
            // A unary operator whose argument is known to never be null
            // can be optimized to avoid the null check.

            string source = @"
class Program
{
    static int N() { return 123; }
    static int? M() 
    { 
        // This can be optimized to new int?(~N())
        return ~(new int?(N())); 
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size       12 (0xc)
  .maxstack  1
  IL_0000:  call       ""int Program.N()""
  IL_0005:  not
  IL_0006:  newobj     ""int?..ctor(int)""
  IL_000b:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M", expectedIL);
        }

        [Fact]
        public void TestNullableUnaryOpsNeverNullChained()
        {
            // A unary operator whose argument is known to never be null
            // can simply be optimized away to be the null result. These
            // optimizations should "chain" naturally. Here we combine
            // two unary operations and a boxing. As you can see, we eliminate
            // all the null checks and the "new S?" ctor.

            string source = @"
struct S
{
  public static S operator +(S s) { return s; }
  public static S operator -(S s) { return s; }
  public static S operator ~(S s) { return s; }
}

class Program
{
    static object M(S s)
    {
        return ~-(new S?(s));
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size       17 (0x11)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  call       ""S S.op_UnaryNegation(S)""
  IL_0006:  call       ""S S.op_OnesComplement(S)""
  IL_000b:  box        ""S""
  IL_0010:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M", expectedIL);
        }

        [Fact]
        public void TestLiftedUnaryOpOnTopOfLifted()
        {
            // Here's an optimization that the dev10 compiler does not do. If we have a 
            // lifted unary operator "on top" of another lifted operation, then the unary
            // operation can be "distributed" to both branches of the underlying lifted operation.
            //
            // For example, suppose we have
            //
            // return -(N1() + N2());
            //
            // Where N1() and N2() return int?. The dev10 compiler does this in two steps: first it 
            // computes the int? result of the addition, and then it does a fully lifted negation:
            //
            // int? t1 = N1();
            // int? t2 = N2();
            // int? t3 = t1.HasValue && t2.HasValue ? new int?(t1.Value + t2.Value) : new int?();
            // return t3.HasValue ? new int?(-t3.Value)) : new int?();
            //
            // But t3 is completely unnecessary here. We could realize this as:
            //
            // return -(t1.HasValue && t2.HasValue ? new int?(t1.Value + t2.Value) : new int?())
            //
            // which is the same as distributing the conversion to the consequence and alternative:
            //
            // return (t1.HasValue && t2.HasValue ? - new int?(t1.Value + t2.Value) ): - ( new int?() ) )
            // 
            // and now we can optimize the consequence and alternative down to
            //
            // return (t1.HasValue && t2.HasValue ? new int?(-(t1.Value + t2.Value) ): new int?() )
            //
            // And the int? t3 disappears entirely. 
            //
            // This optimization has the nice property that it composes well with itself, as we'll see.

            string source = @"
struct S
{
  public static S operator -(S s) { return s; }
  public static S operator ~(S s) { return s; }
  public static S operator +(S s1, S s2) { return s1; }
}
class Program
{
    static int? N1() { return 1; }
    static int? N2() { return 2; }
    static S? N3() { return null; }
    static S? N4() { return null; }

    static int? M1()
    {
        return -(N1() + N2());
    }

    static S? M2()
    {
        return -~(N3() + N4());
    }
    static void Main() { }
}

";
            string expectedOutput = "";

            string expectedIL1 = @"{
  // Code size       61 (0x3d)
  .maxstack  2
  .locals init (int? V_0,
  int? V_1,
  int? V_2)
  IL_0000:  call       ""int? Program.N1()""
  IL_0005:  stloc.0
  IL_0006:  call       ""int? Program.N2()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_0
  IL_000e:  call       ""bool int?.HasValue.get""
  IL_0013:  ldloca.s   V_1
  IL_0015:  call       ""bool int?.HasValue.get""
  IL_001a:  and
  IL_001b:  brtrue.s   IL_0027
  IL_001d:  ldloca.s   V_2
  IL_001f:  initobj    ""int?""
  IL_0025:  ldloc.2
  IL_0026:  ret
  IL_0027:  ldloca.s   V_0
  IL_0029:  call       ""int int?.GetValueOrDefault()""
  IL_002e:  ldloca.s   V_1
  IL_0030:  call       ""int int?.GetValueOrDefault()""
  IL_0035:  add
  IL_0036:  neg
  IL_0037:  newobj     ""int?..ctor(int)""
  IL_003c:  ret
}
";
            string expectedIL2 = @"{
  // Code size       74 (0x4a)
  .maxstack  2
  .locals init (S? V_0,
  S? V_1,
  S? V_2)
  IL_0000:  call       ""S? Program.N3()""
  IL_0005:  stloc.0
  IL_0006:  call       ""S? Program.N4()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_0
  IL_000e:  call       ""bool S?.HasValue.get""
  IL_0013:  ldloca.s   V_1
  IL_0015:  call       ""bool S?.HasValue.get""
  IL_001a:  and
  IL_001b:  brtrue.s   IL_0027
  IL_001d:  ldloca.s   V_2
  IL_001f:  initobj    ""S?""
  IL_0025:  ldloc.2
  IL_0026:  ret
  IL_0027:  ldloca.s   V_0
  IL_0029:  call       ""S S?.GetValueOrDefault()""
  IL_002e:  ldloca.s   V_1
  IL_0030:  call       ""S S?.GetValueOrDefault()""
  IL_0035:  call       ""S S.op_Addition(S, S)""
  IL_003a:  call       ""S S.op_OnesComplement(S)""
  IL_003f:  call       ""S S.op_UnaryNegation(S)""
  IL_0044:  newobj     ""S?..ctor(S)""
  IL_0049:  ret
}";
            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
        }

        [Fact]
        public void TestLiftedBinaryOpWithConstantOnTopOfLifted()
        {
            // If we have a lifted binary operation "on top" of a lifted operation,
            // and the right side of the outer operation is a constant, then we
            // can eliminate several temporaries.
            //
            // For example, suppose we have
            //
            // return N1() * N2() + 1;
            //
            // Where N1() and N2() return int?. We could do this the obvious way:
            //
            // int? n1 = N1();
            // int? n2 = N2();
            // int? r = n1.HasValue && n2.HasValue ? new int?(n1.Value * n2.Value) : new int?();
            // int v = 1;
            // return r.HasValue ? new int?(r.Value + v)) : new int?();
            //
            // But r and v are both unnecessary. We could instead realize this as:
            //
            // int? n1 = N1();
            // int? n2 = N2();
            // return n1.HasValue && n2.HasValue ? new int?(n1.Value * n2.Value + 1) : new int?();
            //
            // We want to do this optimization in particular because it makes codegen for i++
            // and i+=1 better.
            //
            // The dev10 compiler does this optimization *only* on i++ and not on expressions
            // like N1() * N2() + 1 or sh+=1;

            string source = @"
class Program
{
    static int? N1() { return 1; }
    static int? N2() { return 2; }
    static short? sh;

    static int? M1()
    {
        return N1() * N2() + 1;
    }

    static short? M2()
    {
       return sh++;
    }

    static short? M3()
    {
       return ++sh;
    }

    static short? M4()
    {
       return sh += 1;
    }

    static void Main() { }
}
";
            string expectedOutput = "";

            string expectedIL1 = @"{
  // Code size       62 (0x3e)
  .maxstack  2
  .locals init (int? V_0,
  int? V_1,
  int? V_2)
  IL_0000:  call       ""int? Program.N1()""
  IL_0005:  stloc.0
  IL_0006:  call       ""int? Program.N2()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_0
  IL_000e:  call       ""bool int?.HasValue.get""
  IL_0013:  ldloca.s   V_1
  IL_0015:  call       ""bool int?.HasValue.get""
  IL_001a:  and
  IL_001b:  brtrue.s   IL_0027
  IL_001d:  ldloca.s   V_2
  IL_001f:  initobj    ""int?""
  IL_0025:  ldloc.2
  IL_0026:  ret
  IL_0027:  ldloca.s   V_0
  IL_0029:  call       ""int int?.GetValueOrDefault()""
  IL_002e:  ldloca.s   V_1
  IL_0030:  call       ""int int?.GetValueOrDefault()""
  IL_0035:  mul
  IL_0036:  ldc.i4.1
  IL_0037:  add
  IL_0038:  newobj     ""int?..ctor(int)""
  IL_003d:  ret
}";

            string expectedIL2 = @"{
  // Code size       48 (0x30)
  .maxstack  3
  .locals init (short? V_0,
  short? V_1)
  IL_0000:  ldsfld     ""short? Program.sh""
  IL_0005:  dup
  IL_0006:  stloc.0
  IL_0007:  ldloca.s   V_0
  IL_0009:  call       ""bool short?.HasValue.get""
  IL_000e:  brtrue.s   IL_001b
  IL_0010:  ldloca.s   V_1
  IL_0012:  initobj    ""short?""
  IL_0018:  ldloc.1
  IL_0019:  br.s       IL_002a
  IL_001b:  ldloca.s   V_0
  IL_001d:  call       ""short short?.GetValueOrDefault()""
  IL_0022:  ldc.i4.1
  IL_0023:  add
  IL_0024:  conv.i2
  IL_0025:  newobj     ""short?..ctor(short)""
  IL_002a:  stsfld     ""short? Program.sh""
  IL_002f:  ret
}";
            string expectedIL3 = @"{
  // Code size       48 (0x30)
  .maxstack  2
  .locals init (short? V_0,
  short? V_1)
  IL_0000:  ldsfld     ""short? Program.sh""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool short?.HasValue.get""
  IL_000d:  brtrue.s   IL_001a
  IL_000f:  ldloca.s   V_1
  IL_0011:  initobj    ""short?""
  IL_0017:  ldloc.1
  IL_0018:  br.s       IL_0029
  IL_001a:  ldloca.s   V_0
  IL_001c:  call       ""short short?.GetValueOrDefault()""
  IL_0021:  ldc.i4.1
  IL_0022:  add
  IL_0023:  conv.i2
  IL_0024:  newobj     ""short?..ctor(short)""
  IL_0029:  dup
  IL_002a:  stsfld     ""short? Program.sh""
  IL_002f:  ret
}";
            string expectedIL4 = @"{
  // Code size       48 (0x30)
  .maxstack  2
  .locals init (short? V_0,
  short? V_1)
  IL_0000:  ldsfld     ""short? Program.sh""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool short?.HasValue.get""
  IL_000d:  brtrue.s   IL_001a
  IL_000f:  ldloca.s   V_1
  IL_0011:  initobj    ""short?""
  IL_0017:  ldloc.1
  IL_0018:  br.s       IL_0029
  IL_001a:  ldloca.s   V_0
  IL_001c:  call       ""short short?.GetValueOrDefault()""
  IL_0021:  ldc.i4.1
  IL_0022:  add
  IL_0023:  conv.i2
  IL_0024:  newobj     ""short?..ctor(short)""
  IL_0029:  dup
  IL_002a:  stsfld     ""short? Program.sh""
  IL_002f:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
            comp.VerifyIL("Program.M3", expectedIL3);
            comp.VerifyIL("Program.M4", expectedIL4);
        }



        [Fact]
        public void TestNullableComparisonOpsBothAlwaysNull()
        {
            // An ==, !=, <, >, <= or >= operation where both operands
            // are null is always true for equality, and always false otherwise.

            // Note that the native compiler has a bug; it does not produce the warning
            // "comparing null with S? always produces false" -- it incorrectly warns
            // that it produces a null of type bool? !  Roslyn does not reproduce this bug.

            string source = @"
struct S // User-defined relational ops
{
  public static bool operator ==(S x, S y) { return true; }
  public static bool operator !=(S x, S y) { return true; }
  public static bool operator <(S x, S y) { return true; }
  public static bool operator >(S x, S y) { return true; }
  public static bool operator <=(S x, S y) { return true; }
  public static bool operator >=(S x, S y) { return true; }
  public override bool Equals(object x) { return true; }
  public override int GetHashCode() { return 0; }
}
struct T  // no user-defined relational ops
{
}
class Program
{
    static bool M1()
    {
        return new int?() == new short?();
    }
    static bool M2()
    {
        return default(double?) != new short?();
    }
    static bool M3()
    {
        return ((int?)null) < new decimal?();
    }
    static bool M4()
    {
        return new S?() == new S?();
    }
    static bool M5()
    {
        return default(S?) != new S?();
    }
    static bool M6()
    {
        return ((S?)null) < new S?();
    }
    static bool M7() // Special case for equality with null literal and no overloaded operators.
    {
        return default(T?) == null;
    }
    static bool M8()
    {
        return null != new T?();
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedILTrue = @"{
  // Code size        2 (0x2)
  .maxstack  1
  IL_0000:  ldc.i4.1
  IL_0001:  ret
}";
            string expectedILFalse = @"{
  // Code size        2 (0x2)
  .maxstack  1
  IL_0000:  ldc.i4.0
  IL_0001:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics(
823 824
// (25,16): warning CS0464: Comparing with null of type 'decimal?' always produces 'false'
//         return ((int?)null) < new decimal?();
P
Pilchie 已提交
825
Diagnostic(ErrorCode.WRN_CmpAlwaysFalse, "((int?)null) < new decimal?()").WithArguments("decimal?"),
826 827
// (37,16): warning CS0464: Comparing with null of type 'S?' always produces 'false'
//         return ((S?)null) < new S?();
P
Pilchie 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 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 885 886 887 888 889 890 891 892 893 894 895 896 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 923 924 925 926 927 928 929 930 931 932 933 934 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
Diagnostic(ErrorCode.WRN_CmpAlwaysFalse, "((S?)null) < new S?()").WithArguments("S?"));
            comp.VerifyIL("Program.M1", expectedILTrue);
            comp.VerifyIL("Program.M2", expectedILFalse);
            comp.VerifyIL("Program.M3", expectedILFalse);
            comp.VerifyIL("Program.M4", expectedILTrue);
            comp.VerifyIL("Program.M5", expectedILFalse);
            comp.VerifyIL("Program.M6", expectedILFalse);
            comp.VerifyIL("Program.M7", expectedILTrue);
            comp.VerifyIL("Program.M8", expectedILFalse);
        }

        [Fact]
        public void TestNullableComparisonNonNullWithLiteralNull()
        {
            // We can optimize this away to simply evaluating N() for its side effects
            // and returning false.
            string source = @"
struct S {}
class Program
{
    static S N() { System.Console.WriteLine(123); return new S(); }
    static bool M()
    {
        return new S?(N()) == null;
    }
    static void Main() {M();}
}
";
            string expectedOutput = "123";
            string expectedIL = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  call       ""S Program.N()""
  IL_0005:  pop
  IL_0006:  ldc.i4.0
  IL_0007:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyIL("Program.M", expectedIL);
        }

        [Fact]
        public void TestNullableComparisonOpsBothNeverNull()
        {
            // An ==, !=, <, >, <= or >= operation where both operands
            // are not null simply drops the lifting logic entirely.

            string source = @"
struct S // User-defined relational ops
{
  public static bool operator ==(S x, S y) { return true; }
  public static bool operator !=(S x, S y) { return true; }
  public static bool operator <(S x, S y) { return true; }
  public static bool operator >(S x, S y) { return true; }
  public static bool operator <=(S x, S y) { return true; }
  public static bool operator >=(S x, S y) { return true; }
  public override bool Equals(object x) { return true; }
  public override int GetHashCode() { return 0; }
}
class Program
{
    static int N1() { return 123; }
    static short N2() { return 123; }
    static double N3() { return 123; }
    static decimal N4() { return 123; }
    static S N5() { return new S(); }
    static bool M1()
    {
        // Notice that there are two optimizations here and in the next few cases. 
        // First we optimize the conversion from short? to int? so that the right 
        // hand side is new int?((int)N2()). Second, we optimize the comparison to 
        // N1() == (int)N2(), eliminating all the lifting.

        return new int?(N1()) == new short?(N2());
    }
    static bool M2()
    {
        return new double?(N3()) != new short?(N2());
    }
    
    static bool M3()
    {
        return new int?(N1()) < new decimal?(N4());
    }
    static bool M4()
    {
        return new S?(N5()) == new S?(N5());
    }
    static bool M5()
    {
        return new S?(N5()) != new S?(N5());
    }
    static bool M6()
    {
        return new S?(N5()) < new S?(N5());
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       13 (0xd)
  .maxstack  2
  IL_0000:  call       ""int Program.N1()""
  IL_0005:  call       ""short Program.N2()""
  IL_000a:  ceq
  IL_000c:  ret
}";
            string expectedIL2 = @"{
  // Code size       17 (0x11)
  .maxstack  2
  IL_0000:  call       ""double Program.N3()""
  IL_0005:  call       ""short Program.N2()""
  IL_000a:  conv.r8
  IL_000b:  ceq
  IL_000d:  ldc.i4.0
  IL_000e:  ceq
  IL_0010:  ret
}";
            string expectedIL3 = @"{
  // Code size       21 (0x15)
  .maxstack  2
  IL_0000:  call       ""int Program.N1()""
  IL_0005:  call       ""decimal decimal.op_Implicit(int)""
  IL_000a:  call       ""decimal Program.N4()""
  IL_000f:  call       ""bool decimal.op_LessThan(decimal, decimal)""
  IL_0014:  ret
}
";
            string expectedIL4 = @"{
  // Code size       16 (0x10)
  .maxstack  2
  IL_0000:  call       ""S Program.N5()""
  IL_0005:  call       ""S Program.N5()""
  IL_000a:  call       ""bool S.op_Equality(S, S)""
  IL_000f:  ret
}";
            string expectedIL5 = @"{
  // Code size       16 (0x10)
  .maxstack  2
  IL_0000:  call       ""S Program.N5()""
  IL_0005:  call       ""S Program.N5()""
  IL_000a:  call       ""bool S.op_Inequality(S, S)""
  IL_000f:  ret
}";
            string expectedIL6 = @"{
  // Code size       16 (0x10)
  .maxstack  2
  IL_0000:  call       ""S Program.N5()""
  IL_0005:  call       ""S Program.N5()""
  IL_000a:  call       ""bool S.op_LessThan(S, S)""
  IL_000f:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
            comp.VerifyIL("Program.M3", expectedIL3);
            comp.VerifyIL("Program.M4", expectedIL4);
            comp.VerifyIL("Program.M5", expectedIL5);
            comp.VerifyIL("Program.M6", expectedIL6);
        }

993
        [Fact, WorkItem(663, "https://github.com/dotnet/roslyn/issues/663")]
P
Pilchie 已提交
994 995 996 997 998 999 1000 1001 1002
        public void TestNullableComparisonOpsOneNullOneNonNull()
        {
            // An ==, !=, <, >, <= or >= operation where one operand is null and the
            // other is non-null is always false except for inequality, which is true.
            // We can skip the lifting and just generate the side effect.

            // Note that Roslyn produces considerably more warnings here than the
            // native compiler; the native compiler only produces warnings for
            // "((int?)null) < new decimal?(N3())" and "((S?)null) < new S?(N4())".
1003 1004 1005 1006
            // For compatibility Roslyn reports the same diagnostics by default,
            // but in "strict" mode (which will be part of the "warning waves" once
            // we do that) Roslyn will report warnings for
            // new S() == null and new S() != null.
P
Pilchie 已提交
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

            string source = @"
struct S // User-defined relational ops
{
  public static bool operator ==(S x, S y) { return true; }
  public static bool operator !=(S x, S y) { return true; }
  public static bool operator <(S x, S y) { return true; }
  public static bool operator >(S x, S y) { return true; }
  public static bool operator <=(S x, S y) { return true; }
  public static bool operator >=(S x, S y) { return true; }
  public override bool Equals(object x) { return true; }
  public override int GetHashCode() { return 0; }
}
class Program
{
    static int N1() { return 1; }
    static short N2() { return 1; }
    static decimal N3() { return 1; }
    static S N4() { return new S(); }
    static bool M1()
    {
        return new int?(N1()) == new short?();
    }
    static bool M2()
    {
        return default(double?) != new short?(N2());
    }
    static bool M3()
    {
        return ((int?)null) < new decimal?(N3());
    }
    static bool M4()
    {
        return new S?() == new S?(N4());
    }
    static bool M5()
    {
        return default(S?) != new S?(N4());
    }
    static bool M6()
    {
        return ((S?)null) < new S?(N4());
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  call       ""int Program.N1()""
  IL_0005:  pop
  IL_0006:  ldc.i4.0
  IL_0007:  ret
}";
            string expectedIL2 = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  call       ""short Program.N2()""
  IL_0005:  pop
  IL_0006:  ldc.i4.1
  IL_0007:  ret
}";
            string expectedIL3 = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  call       ""decimal Program.N3()""
  IL_0005:  pop
  IL_0006:  ldc.i4.0
  IL_0007:  ret
}";
            string expectedIL4 = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  call       ""S Program.N4()""
  IL_0005:  pop
  IL_0006:  ldc.i4.0
  IL_0007:  ret
}";
            string expectedIL5 = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  call       ""S Program.N4()""
  IL_0005:  pop
  IL_0006:  ldc.i4.1
  IL_0007:  ret
}";
            string expectedIL6 = expectedIL4;

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
            CompileAndVerify(source, expectedOutput: expectedOutput).VerifyDiagnostics(
                // (21,16): warning CS0472: The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'short?'
                //         return new int?(N1()) == new short?();
                Diagnostic(ErrorCode.WRN_NubExprIsConstBool, "new int?(N1()) == new short?()").WithArguments("false", "int", "short?").WithLocation(21, 16),
                // (25,16): warning CS0472: The result of the expression is always 'true' since a value of type 'double' is never equal to 'null' of type 'double?'
                //         return default(double?) != new short?(N2());
                Diagnostic(ErrorCode.WRN_NubExprIsConstBool, "default(double?) != new short?(N2())").WithArguments("true", "double", "double?").WithLocation(25, 16),
                // (29,16): warning CS0464: Comparing with null of type 'int?' always produces 'false'
                //         return ((int?)null) < new decimal?(N3());
                Diagnostic(ErrorCode.WRN_CmpAlwaysFalse, "((int?)null) < new decimal?(N3())").WithArguments("int?").WithLocation(29, 16),
                // (41,16): warning CS0464: Comparing with null of type 'S?' always produces 'false'
                //         return ((S?)null) < new S?(N4());
                Diagnostic(ErrorCode.WRN_CmpAlwaysFalse, "((S?)null) < new S?(N4())").WithArguments("S?").WithLocation(41, 16)
                );
1110
            var comp = CompileAndVerify(source, expectedOutput: expectedOutput, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular.WithStrictFeature());
P
Pilchie 已提交
1111
            comp.VerifyDiagnostics(
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
                // (21,16): warning CS0472: The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'short?'
                //         return new int?(N1()) == new short?();
                Diagnostic(ErrorCode.WRN_NubExprIsConstBool, "new int?(N1()) == new short?()").WithArguments("false", "int", "short?").WithLocation(21, 16),
                // (25,16): warning CS0472: The result of the expression is always 'true' since a value of type 'double' is never equal to 'null' of type 'double?'
                //         return default(double?) != new short?(N2());
                Diagnostic(ErrorCode.WRN_NubExprIsConstBool, "default(double?) != new short?(N2())").WithArguments("true", "double", "double?").WithLocation(25, 16),
                // (29,16): warning CS0464: Comparing with null of type 'int?' always produces 'false'
                //         return ((int?)null) < new decimal?(N3());
                Diagnostic(ErrorCode.WRN_CmpAlwaysFalse, "((int?)null) < new decimal?(N3())").WithArguments("int?").WithLocation(29, 16),
                // (33,16): warning CS8073: The result of the expression is always 'false' since a value of type 'S' is never equal to 'null' of type 'S?'
                //         return new S?() == new S?(N4());
                Diagnostic(ErrorCode.WRN_NubExprIsConstBool2, "new S?() == new S?(N4())").WithArguments("false", "S", "S?").WithLocation(33, 16),
                // (37,16): warning CS8073: The result of the expression is always 'true' since a value of type 'S' is never equal to 'null' of type 'S?'
                //         return default(S?) != new S?(N4());
                Diagnostic(ErrorCode.WRN_NubExprIsConstBool2, "default(S?) != new S?(N4())").WithArguments("true", "S", "S?").WithLocation(37, 16),
                // (41,16): warning CS0464: Comparing with null of type 'S?' always produces 'false'
                //         return ((S?)null) < new S?(N4());
                Diagnostic(ErrorCode.WRN_CmpAlwaysFalse, "((S?)null) < new S?(N4())").WithArguments("S?").WithLocation(41, 16)
                );
P
Pilchie 已提交
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 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 1933 1934 1935 1936 1937 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 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
            comp.VerifyIL("Program.M3", expectedIL3);
            comp.VerifyIL("Program.M4", expectedIL4);
            comp.VerifyIL("Program.M5", expectedIL5);
            comp.VerifyIL("Program.M6", expectedIL6);
        }

        [Fact]
        public void TestNullableComparisonOpsOneNullOneUnknown()
        {
            // An <, >, <= or >= operation where one operand is null and the
            // other is unknown is always false; we can skip the lifting and
            // generate the side effect.
            // 
            // An == or != operation where one operand is null and the other is 
            // unknown turns into a call to HasValue.
            //
            // As mentioned above, the native compiler gets one of the warnings wrong;
            // Roslyn gets it right.

            string source = @"
struct S // User-defined relational ops
{
  public static bool operator ==(S x, S y) { return true; }
  public static bool operator !=(S x, S y) { return true; }
  public static bool operator <(S x, S y) { return true; }
  public static bool operator >(S x, S y) { return true; }
  public static bool operator <=(S x, S y) { return true; }
  public static bool operator >=(S x, S y) { return true; }
  public override bool Equals(object x) { return true; }
  public override int GetHashCode() { return 0; }
}
class Program
{
    static int? N1() { return 1; }
    static short? N2() { return 1; }
    static decimal? N3() { return 1; }
    static S? N4() { return new S(); }
    static bool M1()
    {
        return N1() == new short?();
    }
    static bool M2()
    {
        return default(double?) != N2();
    }
    static bool M3()
    {
        return ((int?)null) < N3();
    }
    static bool M4()
    {
        return new S?() == N4();
    }
    static bool M5()
    {
        return default(S?) != N4();
    }
    static bool M6()
    {
        return ((S?)null) < N4();
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       17 (0x11)
  .maxstack  2
  .locals init (int? V_0)
  IL_0000:  call       ""int? Program.N1()""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool int?.HasValue.get""
  IL_000d:  ldc.i4.0
  IL_000e:  ceq
  IL_0010:  ret
}";
            // TODO: Roslyn and the native compiler both produce this sub-optimal code for
            // TODO: "default(double?) != N2()". We are essentially generating:
            // TODO:
            // TODO: short? temp1 = N2();
            // TODO: double? temp2 = temp1.HasValue ? new double?((double)temp1.GetValueOrDefault()) : new double?();
            // TODO: return temp2.HasValue;
            // TODO:
            // TODO: We could be instead simply generating
            // TODO:
            // TODO: return N2().HasValue();

            string expectedIL2 = @"{
  // Code size       48 (0x30)
  .maxstack  1
  .locals init (short? V_0,
  double? V_1)
  IL_0000:  call       ""short? Program.N2()""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool short?.HasValue.get""
  IL_000d:  brtrue.s   IL_001a
  IL_000f:  ldloca.s   V_1
  IL_0011:  initobj    ""double?""
  IL_0017:  ldloc.1
  IL_0018:  br.s       IL_0027
  IL_001a:  ldloca.s   V_0
  IL_001c:  call       ""short short?.GetValueOrDefault()""
  IL_0021:  conv.r8
  IL_0022:  newobj     ""double?..ctor(double)""
  IL_0027:  stloc.1
  IL_0028:  ldloca.s   V_1
  IL_002a:  call       ""bool double?.HasValue.get""
  IL_002f:  ret
}";
            string expectedIL3 = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  call       ""decimal? Program.N3()""
  IL_0005:  pop
  IL_0006:  ldc.i4.0
  IL_0007:  ret
}";
            string expectedIL4 = @"{
  // Code size       17 (0x11)
  .maxstack  2
  .locals init (S? V_0)
  IL_0000:  call       ""S? Program.N4()""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool S?.HasValue.get""
  IL_000d:  ldc.i4.0
  IL_000e:  ceq
  IL_0010:  ret
}";
            string expectedIL5 = @"{
  // Code size       14 (0xe)
  .maxstack  1
  .locals init (S? V_0)
  IL_0000:  call       ""S? Program.N4()""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool S?.HasValue.get""
  IL_000d:  ret
}";
            string expectedIL6 = @"{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  call       ""S? Program.N4()""
  IL_0005:  pop
  IL_0006:  ldc.i4.0
  IL_0007:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics(
// (29,16): warning CS0464: Comparing with null of type 'int?' always produces 'false'
//         return ((int?)null) < N3();
Diagnostic(ErrorCode.WRN_CmpAlwaysFalse, "((int?)null) < N3()").WithArguments("int?"),
// (41,16): warning CS0464: Comparing with null of type 'S?' always produces 'false'
//         return ((S?)null) < N4();
Diagnostic(ErrorCode.WRN_CmpAlwaysFalse, "((S?)null) < N4()").WithArguments("S?")
            );
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
            comp.VerifyIL("Program.M3", expectedIL3);
            comp.VerifyIL("Program.M4", expectedIL4);
            comp.VerifyIL("Program.M5", expectedIL5);
            comp.VerifyIL("Program.M6", expectedIL6);
        }


        [Fact]
        public void TestNullableComparisonOpsOneNonNullOneUnknown()
        {
            // When we have a lifted comparison where we know that one side
            // is definitely not null, but know nothing about the other, then
            // we make a slight modification to the code generation. For example,
            // suppose X() and Y() return int?. For "return X() < Y();" we would generate:
            // int? x = X(); 
            // int? y = Y();
            // return x.GetValueOrDefault() < y.GetValueOrDefault() ? x.HasValue & y.HasValue : false;
            // 
            // But suppose Z() returns int. For X() < Z(), rather than converting Z() to int? and doing the
            // same codegen as before, we simplify the codegen to:
            //
            // int? x = X(); 
            // int z = Z();
            // return x.GetValueOrDefault() < z ? x.HasValue : false;
            //
            // We apply this same pattern to all lifted comparison operators.

            string source = @"
struct S // User-defined relational ops
{
    public static bool operator ==(S x, S y) { return true; }
    public static bool operator !=(S x, S y) { return true; }
    public static bool operator <(S x, S y) { return true; }
    public static bool operator >(S x, S y) { return true; }
    public static bool operator <=(S x, S y) { return true; }
    public static bool operator >=(S x, S y) { return true; }
    public override bool Equals(object x) { return true; }
    public override int GetHashCode() { return 0; }
}
class Program
{
    static int? N1() { return 1; }
    static short? N2() { return 1; }
    static decimal? N3() { return 1; }
    static S? N4() { return new S(); }

    static int V1() { return 1; }
    static short V2() { return 1; }
    static decimal V3() { return 1; }
    static S V4() { return new S(); }

    static bool M1()
    {
        return N1() == new short?(V2());
    }
    static bool M2()
    {
        return N1() < new decimal?(V3());
    }
    static bool M3()
    {
        return new S?(V4()) == N4();
    }
    static bool M4()
    {
        return new S?(V4()) != N4();
    }
    static bool M5()
    {
        return new S?(V4()) < N4();
    }
    static void Main() { }
}

";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       32 (0x20)
  .maxstack  2
  .locals init (int? V_0,
  int V_1)
  IL_0000:  call       ""int? Program.N1()""
  IL_0005:  stloc.0
  IL_0006:  call       ""short Program.V2()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_0
  IL_000e:  call       ""int int?.GetValueOrDefault()""
  IL_0013:  ldloc.1
  IL_0014:  beq.s      IL_0018
  IL_0016:  ldc.i4.0
  IL_0017:  ret
  IL_0018:  ldloca.s   V_0
  IL_001a:  call       ""bool int?.HasValue.get""
  IL_001f:  ret
}";

            // TODO: We do a worse job than the native compiler here. Find out why.

            string expectedIL2 = @"{
  // Code size       75 (0x4b)
  .maxstack  2
  .locals init (decimal? V_0,
  decimal V_1,
  int? V_2,
  decimal? V_3)
  IL_0000:  call       ""int? Program.N1()""
  IL_0005:  stloc.2
  IL_0006:  ldloca.s   V_2
  IL_0008:  call       ""bool int?.HasValue.get""
  IL_000d:  brtrue.s   IL_001a
  IL_000f:  ldloca.s   V_3
  IL_0011:  initobj    ""decimal?""
  IL_0017:  ldloc.3
  IL_0018:  br.s       IL_002b
  IL_001a:  ldloca.s   V_2
  IL_001c:  call       ""int int?.GetValueOrDefault()""
  IL_0021:  call       ""decimal decimal.op_Implicit(int)""
  IL_0026:  newobj     ""decimal?..ctor(decimal)""
  IL_002b:  stloc.0
  IL_002c:  call       ""decimal Program.V3()""
  IL_0031:  stloc.1
  IL_0032:  ldloca.s   V_0
  IL_0034:  call       ""decimal decimal?.GetValueOrDefault()""
  IL_0039:  ldloc.1
  IL_003a:  call       ""bool decimal.op_LessThan(decimal, decimal)""
  IL_003f:  brtrue.s   IL_0043
  IL_0041:  ldc.i4.0
  IL_0042:  ret
  IL_0043:  ldloca.s   V_0
  IL_0045:  call       ""bool decimal?.HasValue.get""
  IL_004a:  ret
}";
            string expectedIL3 = @"{
  // Code size       37 (0x25)
  .maxstack  2
  .locals init (S V_0,
  S? V_1)
  IL_0000:  call       ""S Program.V4()""
  IL_0005:  stloc.0
  IL_0006:  call       ""S? Program.N4()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_1
  IL_000e:  call       ""bool S?.HasValue.get""
  IL_0013:  brtrue.s   IL_0017
  IL_0015:  ldc.i4.0
  IL_0016:  ret
  IL_0017:  ldloc.0
  IL_0018:  ldloca.s   V_1
  IL_001a:  call       ""S S?.GetValueOrDefault()""
  IL_001f:  call       ""bool S.op_Equality(S, S)""
  IL_0024:  ret
}";
            string expectedIL4 = @"{
  // Code size       37 (0x25)
  .maxstack  2
  .locals init (S V_0,
  S? V_1)
  IL_0000:  call       ""S Program.V4()""
  IL_0005:  stloc.0
  IL_0006:  call       ""S? Program.N4()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_1
  IL_000e:  call       ""bool S?.HasValue.get""
  IL_0013:  brtrue.s   IL_0017
  IL_0015:  ldc.i4.1
  IL_0016:  ret
  IL_0017:  ldloc.0
  IL_0018:  ldloca.s   V_1
  IL_001a:  call       ""S S?.GetValueOrDefault()""
  IL_001f:  call       ""bool S.op_Inequality(S, S)""
  IL_0024:  ret
}";
            string expectedIL5 = @"{
  // Code size       37 (0x25)
  .maxstack  2
  .locals init (S V_0,
  S? V_1)
  IL_0000:  call       ""S Program.V4()""
  IL_0005:  stloc.0
  IL_0006:  call       ""S? Program.N4()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_1
  IL_000e:  call       ""bool S?.HasValue.get""
  IL_0013:  brtrue.s   IL_0017
  IL_0015:  ldc.i4.0
  IL_0016:  ret
  IL_0017:  ldloc.0
  IL_0018:  ldloca.s   V_1
  IL_001a:  call       ""S S?.GetValueOrDefault()""
  IL_001f:  call       ""bool S.op_LessThan(S, S)""
  IL_0024:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
            comp.VerifyIL("Program.M3", expectedIL3);
            comp.VerifyIL("Program.M4", expectedIL4);
            comp.VerifyIL("Program.M5", expectedIL5);
        }

        [Fact]
        public void TestLiftedConversionOnTopOfLifted()
        {
            // Here's an optimization that the dev10 compiler does not do except in some special cases.
            // If we have a lifted conversion "on top" of another lifted operation, then the lifted
            // conversion can be "distributed" to both branches of the underlying lifted operation.
            //
            // For example, suppose we have
            //
            // return (double?)(N1() + N2());
            //
            // Where N1 and N2 return int?. The dev10 compiler does this in two steps: first it 
            // computes the int? result of the addition, and then it converts that int? to double?
            // with a lifted conversion. Basically, it generates:
            //
            // int? t1 = N1();
            // int? t2 = N2();
            // int? t3 = t1.HasValue && t2.HasValue ? new int?(t1.Value + t2.Value) : new int?();
            // double? t4 = t3.HasValue ? new double?((double)t3.Value)) : new double?();
            //
            // But t3 is completely unnecessary here. We observe that the lifted conversion:
            //
            // (double?) (t1.HasValue && t2.HasValue ? new int?(t1.Value + t2.Value) : new int?())
            //
            // Is the same as distributing the conversion to the consequence and alternative:
            //
            // (t1.HasValue && t2.HasValue ? (double?)( new int?(t1.Value + t2.Value) ): (double?) ( new int?() ) )
            // 
            // And now we can optimize the consequence and alternative down to
            //
            // (t1.HasValue && t2.HasValue ? new double?((double)(t1.Value + t2.Value) ): new double?() )
            //
            // And the int? t3 disappears entirely. 
            //
            // This optimization has the nice property that it composes well with itself. 


            string source = @"
struct S
{
  public static S operator -(S s) { return s; }
  public static implicit operator int(S s) { return 1; }
}
class Program
{
    static int? N1() { return 1; }
    static int? N2() { return 1; }
    static S? N3() { return null; }

    // Start with a nice simple case; we have a lifted numeric conversion on top of a
    // lifted addition.
    static long? M1()
    {
        return N1() + N2();
    }

    // This should also work with lifted user-defined conversions, and work on top
    // of a lifted unary operator.
    static int? M2()
    {
        return -N3();
    }
    static void Main() { }
}

";
            string expectedOutput = "";

            string expectedIL1 = @"{
  // Code size       61 (0x3d)
  .maxstack  2
  .locals init (int? V_0,
  int? V_1,
  long? V_2)
  IL_0000:  call       ""int? Program.N1()""
  IL_0005:  stloc.0
  IL_0006:  call       ""int? Program.N2()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_0
  IL_000e:  call       ""bool int?.HasValue.get""
  IL_0013:  ldloca.s   V_1
  IL_0015:  call       ""bool int?.HasValue.get""
  IL_001a:  and
  IL_001b:  brtrue.s   IL_0027
  IL_001d:  ldloca.s   V_2
  IL_001f:  initobj    ""long?""
  IL_0025:  ldloc.2
  IL_0026:  ret
  IL_0027:  ldloca.s   V_0
  IL_0029:  call       ""int int?.GetValueOrDefault()""
  IL_002e:  ldloca.s   V_1
  IL_0030:  call       ""int int?.GetValueOrDefault()""
  IL_0035:  add
  IL_0036:  conv.i8
  IL_0037:  newobj     ""long?..ctor(long)""
  IL_003c:  ret
}";
            string expectedIL2 = @"{
  // Code size       48 (0x30)
  .maxstack  1
  .locals init (S? V_0,
  int? V_1)
  IL_0000:  call       ""S? Program.N3()""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool S?.HasValue.get""
  IL_000d:  brtrue.s   IL_0019
  IL_000f:  ldloca.s   V_1
  IL_0011:  initobj    ""int?""
  IL_0017:  ldloc.1
  IL_0018:  ret
  IL_0019:  ldloca.s   V_0
  IL_001b:  call       ""S S?.GetValueOrDefault()""
  IL_0020:  call       ""S S.op_UnaryNegation(S)""
  IL_0025:  call       ""int S.op_Implicit(S)""
  IL_002a:  newobj     ""int?..ctor(int)""
  IL_002f:  ret
}";
            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
        }

        [Fact]
        public void TestNullableBoolBinOpsBothAlwaysNull()
        {
            // x & y and x | y are null if both operands are null.

            string source = @"
class Program
{
    static bool? M1()
    {
        return new bool?() | new bool?();
    }
    static bool? M2()
    {
        return (bool?)null & default(bool?);
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init (bool? V_0)
  IL_0000:  ldloca.s   V_0
  IL_0002:  initobj    ""bool?""
  IL_0008:  ldloc.0
  IL_0009:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL);
            comp.VerifyIL("Program.M2", expectedIL);
        }

        [Fact]
        public void TestNullableBoolBinOpsBothNotNull()
        {
            // x & y and x | y can be reduced to their non-lifted forms
            // if both operands are known to be non-null. 
            //
            // Roslyn does a slightly better job than the native compiler here.
            // The native compiler effectively generates code as though you'd written:
            //
            // bool temp1 = N();
            // bool? temp2 = new bool?(N() & N());
            // return temp1 ? new bool?(true) : temp2;
            //
            // Whereas Roslyn simply generates code as though you'd written:
            //
            // return new bool?(N() | N() & N())

            string source = @"
class Program
{
    static bool N() { return true; }
    static bool? M1()
    {
        return new bool?(N()) | new bool?(N()) & new bool?(N());
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size       23 (0x17)
  .maxstack  3
  IL_0000:  call       ""bool Program.N()""
  IL_0005:  call       ""bool Program.N()""
  IL_000a:  call       ""bool Program.N()""
  IL_000f:  and
  IL_0010:  or
  IL_0011:  newobj     ""bool?..ctor(bool)""
  IL_0016:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL);
        }

        [Fact]
        public void TestNullableBoolBinOpsOneNull()
        {
            // codegen for x & y and x | y can be simplified if one operand is known to be null, 
            // and simplified even further if the other operand is known to be non-null.

            string source = @"
class Program
{
    static bool? N() { return false; }
    static bool B() { return false; }
    static bool? M1()
    {
        // Generated as temp = N(), temp.GetValueOrDefault() ? null : temp
        return N() & new bool?();
    }
    static bool? M2()
    {
        // Generated as temp = N(), temp.GetValueOrDefault() ? null : temp
        return default(bool?) & N();
    }
    static bool? M3()
    {
        // Generated as temp = N(), temp.GetValueOrDefault() ? temp : null
        return N() | new bool?();
    }
    static bool? M4()
    {
        // Generated as temp = N(), temp.GetValueOrDefault() ? temp : null
        return default(bool?) | N();
    }
    static bool? M5()
    {
        // Generated as B() ? null : new bool?(false)
        return new bool?(B()) & new bool?();
    }
    static bool? M6()
    {
        // Generated as B() ? null : new bool?(false)
        return default(bool?) & new bool?(B());
    }
    static bool? M7()
    {
        // Generated as B() ? new bool?(true) : null
        return new bool?(B()) | new bool?();
    }
    static bool? M8()
    {
        // Generated as B() ? new bool?(true) : null
        return default(bool?) | new bool?(B());
    }
    static void Main() {}
}

";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       27 (0x1b)
  .maxstack  1
  .locals init (bool? V_0,
  bool? V_1)
  IL_0000:  call       ""bool? Program.N()""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool bool?.GetValueOrDefault()""
  IL_000d:  brtrue.s   IL_0011
  IL_000f:  ldloc.0
  IL_0010:  ret
  IL_0011:  ldloca.s   V_1
  IL_0013:  initobj    ""bool?""
  IL_0019:  ldloc.1
  IL_001a:  ret
}";
            string expectedIL2 = expectedIL1;
            string expectedIL3 = @"{
  // Code size       27 (0x1b)
  .maxstack  1
  .locals init (bool? V_0,
  bool? V_1)
  IL_0000:  call       ""bool? Program.N()""
  IL_0005:  stloc.0
  IL_0006:  ldloca.s   V_0
  IL_0008:  call       ""bool bool?.GetValueOrDefault()""
  IL_000d:  brtrue.s   IL_0019
  IL_000f:  ldloca.s   V_1
  IL_0011:  initobj    ""bool?""
  IL_0017:  ldloc.1
  IL_0018:  ret
  IL_0019:  ldloc.0
  IL_001a:  ret
}";
            string expectedIL4 = expectedIL3;
            string expectedIL5 = @"{
  // Code size       24 (0x18)
  .maxstack  1
  .locals init (bool? V_0)
  IL_0000:  call       ""bool Program.B()""
  IL_0005:  brtrue.s   IL_000e
  IL_0007:  ldc.i4.0
  IL_0008:  newobj     ""bool?..ctor(bool)""
  IL_000d:  ret
  IL_000e:  ldloca.s   V_0
  IL_0010:  initobj    ""bool?""
  IL_0016:  ldloc.0
  IL_0017:  ret
}";
            string expectedIL6 = expectedIL5;
            string expectedIL7 = @"{
  // Code size       24 (0x18)
  .maxstack  1
  .locals init (bool? V_0)
  IL_0000:  call       ""bool Program.B()""
  IL_0005:  brtrue.s   IL_0011
  IL_0007:  ldloca.s   V_0
  IL_0009:  initobj    ""bool?""
  IL_000f:  ldloc.0
  IL_0010:  ret
  IL_0011:  ldc.i4.1
  IL_0012:  newobj     ""bool?..ctor(bool)""
  IL_0017:  ret
}
";
            string expectedIL8 = expectedIL7;

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
            comp.VerifyIL("Program.M3", expectedIL3);
            comp.VerifyIL("Program.M4", expectedIL4);
            comp.VerifyIL("Program.M5", expectedIL5);
            comp.VerifyIL("Program.M6", expectedIL6);
            comp.VerifyIL("Program.M7", expectedIL7);
            comp.VerifyIL("Program.M8", expectedIL8);
        }

        [Fact]
        public void TestNullableBoolBinOpsOneNonNull()
        {
            // Codegen for x & y and x | y can be simplified if one operand is known to be non null.
            // Note that we have already considered the case where one operand is null and the 
            // other is non null, in the test case above.

            string source = @"
class Program
{
    static bool? N() { return false; }
    static bool B() { return false; }
    static bool? M1()
    {
        return new bool?(B()) & N();
    }
    static bool? M2()
    {
        return N() & new bool?(B());
    }
    static bool? M3()
    {
        return new bool?(B()) | N();
    }
    static bool? M4()
    {
        return N() | new bool?(B());
    }
    static void Main() {}
}

";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       22 (0x16)
  .maxstack  1
  .locals init (bool? V_0)
  IL_0000:  call       ""bool? Program.N()""
  IL_0005:  stloc.0
  IL_0006:  call       ""bool Program.B()""
  IL_000b:  brtrue.s   IL_0014
  IL_000d:  ldc.i4.0
  IL_000e:  newobj     ""bool?..ctor(bool)""
  IL_0013:  ret
  IL_0014:  ldloc.0
  IL_0015:  ret
}";
            string expectedIL2 = @"{
  // Code size       22 (0x16)
  .maxstack  2
  .locals init (bool? V_0)
  IL_0000:  call       ""bool Program.B()""
  IL_0005:  call       ""bool? Program.N()""
  IL_000a:  stloc.0
  IL_000b:  brtrue.s   IL_0014
  IL_000d:  ldc.i4.0
  IL_000e:  newobj     ""bool?..ctor(bool)""
  IL_0013:  ret
  IL_0014:  ldloc.0
  IL_0015:  ret
}";
            string expectedIL3 = @"{
  // Code size       22 (0x16)
  .maxstack  1
  .locals init (bool? V_0)
  IL_0000:  call       ""bool? Program.N()""
  IL_0005:  stloc.0
  IL_0006:  call       ""bool Program.B()""
  IL_000b:  brtrue.s   IL_000f
  IL_000d:  ldloc.0
  IL_000e:  ret
  IL_000f:  ldc.i4.1
  IL_0010:  newobj     ""bool?..ctor(bool)""
  IL_0015:  ret
}";
            string expectedIL4 = @"{
  // Code size       22 (0x16)
  .maxstack  2
  .locals init (bool? V_0)
  IL_0000:  call       ""bool Program.B()""
  IL_0005:  call       ""bool? Program.N()""
  IL_000a:  stloc.0
  IL_000b:  brtrue.s   IL_000f
  IL_000d:  ldloc.0
  IL_000e:  ret
  IL_000f:  ldc.i4.1
  IL_0010:  newobj     ""bool?..ctor(bool)""
  IL_0015:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
            comp.VerifyIL("Program.M3", expectedIL3);
            comp.VerifyIL("Program.M4", expectedIL4);
        }

        [Fact]
        public void TestNullableBinOpsBothAlwaysNull()
        {
            // x op y is null if both ops are null for the binary operators
            // * / % + - << >> and for non-bool & ^ | 

            string source = @"
class Program
{
    static long? M1()
    {
        return new int?() + new long?();
    }
    static decimal? M2()
    {
        return (short?)null * default(decimal?);
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init (long? V_0)
  IL_0000:  ldloca.s   V_0
  IL_0002:  initobj    ""long?""
  IL_0008:  ldloc.0
  IL_0009:  ret
}";
            string expectedIL2 = @"{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init (decimal? V_0)
  IL_0000:  ldloca.s   V_0
  IL_0002:  initobj    ""decimal?""
  IL_0008:  ldloc.0
  IL_0009:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
1977
            comp.VerifyDiagnostics(
P
Pilchie 已提交
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 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
// (6,16): warning CS0458: The result of the expression is always 'null' of type 'long?'
//         return new int?() + new long?();
Diagnostic(ErrorCode.WRN_AlwaysNull, "new int?() + new long?()").WithArguments("long?"),
// (10,16): warning CS0458: The result of the expression is always 'null' of type 'decimal?'
//         return (short?)null * default(decimal?);
Diagnostic(ErrorCode.WRN_AlwaysNull, "(short?)null * default(decimal?)").WithArguments("decimal?"));
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
        }

        [Fact]
        public void TestNullableBinOpsBothNonNull()
        {
            // Lifted x op y is generated as non-lifted if both operands are known to be non-null
            // for operators * / % + - << >> and for non-bool & ^ | 
            //
            // Roslyn does a far better job of this optimization than the native compiler.

            string source = @"
class Program
{
    static int N() { return 1; }
    static int? M1()
    {
        return 
            new int?(N()) * 
            new int?(N()) / 
            new int?(N()) % 
            new int?(N()) +
            new int?(N()) - 
            new int?(N()) << 
            new int?(N()) >>
            new int?(N()) &
            new int?(N()) ^ 
            new int?(N()) |
            new int?(N());
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL = @"{
  // Code size       77 (0x4d)
  .maxstack  3
  IL_0000:  call       ""int Program.N()""
  IL_0005:  call       ""int Program.N()""
  IL_000a:  mul
  IL_000b:  call       ""int Program.N()""
  IL_0010:  div
  IL_0011:  call       ""int Program.N()""
  IL_0016:  rem
  IL_0017:  call       ""int Program.N()""
  IL_001c:  add
  IL_001d:  call       ""int Program.N()""
  IL_0022:  sub
  IL_0023:  call       ""int Program.N()""
  IL_0028:  ldc.i4.s   31
  IL_002a:  and
  IL_002b:  shl
  IL_002c:  call       ""int Program.N()""
  IL_0031:  ldc.i4.s   31
  IL_0033:  and
  IL_0034:  shr
  IL_0035:  call       ""int Program.N()""
  IL_003a:  and
  IL_003b:  call       ""int Program.N()""
  IL_0040:  xor
  IL_0041:  call       ""int Program.N()""
  IL_0046:  or
  IL_0047:  newobj     ""int?..ctor(int)""
  IL_004c:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL);
        }

        [Fact]
        public void TestNullableBinOpsOneNull()
        {
            // If we have null + N() or null + new int?(B()) 
            // then we simply generate M() as a side effect and result in null.

            string source = @"
class Program
{
    static int? N() { return 1; }
    static int B() { return 1; }

    static int? M1()
    {
        return new int?() + N();
    }
    static int? M2()
    {
        return new int?(B()) * default(int?);
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       16 (0x10)
  .maxstack  1
  .locals init (int? V_0)
  IL_0000:  call       ""int? Program.N()""
  IL_0005:  pop
  IL_0006:  ldloca.s   V_0
  IL_0008:  initobj    ""int?""
  IL_000e:  ldloc.0
  IL_000f:  ret
}";
            string expectedIL2 = @"{
  // Code size       16 (0x10)
  .maxstack  1
  .locals init (int? V_0)
  IL_0000:  call       ""int Program.B()""
  IL_0005:  pop
  IL_0006:  ldloca.s   V_0
  IL_0008:  initobj    ""int?""
  IL_000e:  ldloc.0
  IL_000f:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
2104
            comp.VerifyDiagnostics(
P
Pilchie 已提交
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
// (9,16): warning CS0458: The result of the expression is always 'null' of type 'int?'
//         return new int?() + N();
Diagnostic(ErrorCode.WRN_AlwaysNull, "new int?() + N()").WithArguments("int?"),
// (13,16): warning CS0458: The result of the expression is always 'null' of type 'int?'
//         return new int?(B()) * default(int?);
Diagnostic(ErrorCode.WRN_AlwaysNull, "new int?(B()) * default(int?)").WithArguments("int?"));
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
        }

        [Fact]
        public void TestNullableBinOpsOneNonNull()
        {
            // If one side of the nullable binop is non-null then we skip calling HasValue and GetValueOrDefault
            // on that side.

            string source = @"
class Program
{
    static int B() { return 1; }
    static int? N() { return 1; }
    static int? M1()
    {
        return new int?(B()) + N();
    }
    static int? M2()
    {
        return new int?(1) + N();
    }
    static void Main() {}
}
";
            string expectedOutput = "";
            string expectedIL1 = @"{
  // Code size       46 (0x2e)
  .maxstack  2
  .locals init (int V_0,
  int? V_1,
  int? V_2)
  IL_0000:  call       ""int Program.B()""
  IL_0005:  stloc.0
  IL_0006:  call       ""int? Program.N()""
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_1
  IL_000e:  call       ""bool int?.HasValue.get""
  IL_0013:  brtrue.s   IL_001f
  IL_0015:  ldloca.s   V_2
  IL_0017:  initobj    ""int?""
  IL_001d:  ldloc.2
  IL_001e:  ret
  IL_001f:  ldloc.0
  IL_0020:  ldloca.s   V_1
  IL_0022:  call       ""int int?.GetValueOrDefault()""
  IL_0027:  add
  IL_0028:  newobj     ""int?..ctor(int)""
  IL_002d:  ret
}";


            // TODO: Roslyn does a slightly worse job here than the native compiler does.
            // TODO: The native compiler knows that the constant need not be stored in a temporary.
            // TODO: We will clean this up in a later checkin.
            // TODO: When we do so, add tests for ++ -- +=, etc.

            string expectedIL2 = @"{
  // Code size       42 (0x2a)
  .maxstack  2
  .locals init (int V_0,
  int? V_1,
  int? V_2)
  IL_0000:  ldc.i4.1
  IL_0001:  stloc.0
  IL_0002:  call       ""int? Program.N()""
  IL_0007:  stloc.1
  IL_0008:  ldloca.s   V_1
  IL_000a:  call       ""bool int?.HasValue.get""
  IL_000f:  brtrue.s   IL_001b
  IL_0011:  ldloca.s   V_2
  IL_0013:  initobj    ""int?""
  IL_0019:  ldloc.2
  IL_001a:  ret
  IL_001b:  ldloc.0
  IL_001c:  ldloca.s   V_1
  IL_001e:  call       ""int int?.GetValueOrDefault()""
  IL_0023:  add
  IL_0024:  newobj     ""int?..ctor(int)""
  IL_0029:  ret
}";

            var comp = CompileAndVerify(source, expectedOutput: expectedOutput);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.M1", expectedIL1);
            comp.VerifyIL("Program.M2", expectedIL2);
        }
    }
}