CodeGenOperators.cs 106.8 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 76 77 78 79 80 81 82 83 84 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

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.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
{
    public class CodeGenOperatorTests : CSharpTestBase
    {
        [Fact]
        public void TestDelegateAndStringOperators()
        {
            var source = @"
using System;
class C 
{ 
    delegate void D();
    public static void M123()
    {
        Console.WriteLine(123);
    }

    public static void M456()
    {
        Console.WriteLine(456);
    }

    public static void Main() 
    { 
        D d123 = M123;
        D d456 = M456;
        D d123456 = d123 + d456;
        d123456();
        Console.WriteLine(d123456 - d123 == d456);
        Console.WriteLine(d123456 - d123 == d123);
        string s123 = 123.ToString();
        object s456 = 456.ToString();
        Console.WriteLine(s123 + s456);
        Console.WriteLine(s123 + s456 + s123);
    }
}
";
            string expectedOutput =
@"123
456
True
False
123456
123456123
";
            var compilation = CompileAndVerify(source, expectedOutput: expectedOutput);
        }

        [Fact]
        public void TestCompoundAssignmentOperators()
        {
            var source = @"
using System;
class C 
{ 
    delegate void D();
    public static void M123()
    {
        Console.Write(123);
    }

    public static void M456()
    {
        Console.Write(456);
    }

    static C c;
    static C GetC() { Console.Write(""GetC""); return c; }
    byte f;

    public static void Main() 
    { 
        C.c = new C();
        D d123 = M123;
        D d456 = M456;
        D d = null;
        d += d123;
        d();
        d += d456;
        d();
        d -= d123;
        d();
        Console.WriteLine();
        short b = 100;
        b -= 70;
        Console.Write(b);
        b *= 2;
        Console.Write(b);
        Console.WriteLine();
        string s = string.Empty;
        s += 789.ToString();
        Console.Write(s);
        s += 987.ToString();
        Console.Write(s);
        Console.WriteLine();
        GetC().f += 100;
        Console.Write(C.c.f);
        Console.WriteLine();
        int[] arr = { 10 };
        arr[0] += 1;
        Console.Write(arr[0]);
        Console.WriteLine();
    }
}
";
            string expectedOutput =
@"123123456456
3060
789789987
GetC100
11";
            var compilation = CompileAndVerify(source, expectedOutput: expectedOutput);
        }

        // TODO: Add VerifyIL for is and as Codegen tests
        [Fact]
        public void TestIsOperator()
        {
            var source = @"
namespace TestIsOperator
{
    public interface MyInter { }
    public class TestType : MyInter { }
    public class AnotherType { }
    public class MyBase { }
    public class MyDerived : MyBase { }
    public class C
    {
        static void Main()
        {

            string myStr = ""foo"";

            object o = myStr;
            bool b = o is string;

            int myInt = 3;
            b = myInt is int;

            TestType tt = null;
            o = tt;
            b = o is TestType;

            tt = new TestType();
            o = tt;
            b = o is AnotherType;

            b = o is MyInter;

            MyInter mi = new TestType();
            o = mi;
            b = o is MyInter;

            MyBase mb = new MyBase();
            o = mb;
            b = o is MyDerived;

            MyDerived md = new MyDerived();
            o = md;
            b = o is MyBase;

            b = null is MyBase;
        }
    }
}
";
            var compilation = CompileAndVerify(source);
        }

        // TODO: Add VerifyIL for is and as Codegen tests
        [Fact]
        public void TestIsOperatorGeneric()
        {
            var source = @"
namespace TestIsOperatorGeneric
{
    public class C
    {
        public static void Main() { }
        public static void M<T, U, W>(T t, U u, W w)
            where T : class
            where U : class
            where W : class
        {
            bool test = t is int;
            test = u is object;
            test = t is U;
            test = t is T;
            test = u is U;
            test = u is T;
            test = w is int;
            test = w is object;
            test = w is U;
            test = u is W;
            test = t is W;
            test = w is W;
        }
    }
}
";
            var compilation = CompileAndVerify(source);
        }

        [Fact]
        public void CS0184WRN_IsAlwaysFalse()
        {
            var text = @"
class MyClass
{
    public static int Main()
    {
        int i = 0;
        bool b = i is string;   // CS0184
        System.Console.WriteLine(b);
        return 0;
    }
}
";

            var comp = CompileAndVerify(text, expectedOutput: "False");
            comp.VerifyDiagnostics(
                // (7,18): warning CS0184: The given expression is never of the provided ('string') type
                //         bool b = i is string;   // CS0184
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "i is string").WithArguments("string"));
            comp.VerifyIL("MyClass.Main", @"
{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  ldc.i4.0
  IL_0001:  call       ""void System.Console.WriteLine(bool)""
  IL_0006:  ldc.i4.0
  IL_0007:  ret
}");
        }

246
        [WorkItem(542466, "DevDiv")]
P
Pilchie 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        [Fact]
        public void CS0184WRN_IsAlwaysFalse_Enum()
        {
            var text = @"
class IsTest
{
    static void Main()
    {
        var b = 1 is color;
        System.Console.WriteLine(b);
    }
}
enum color
{ }
";

            var comp = CompileAndVerify(text, expectedOutput: "False");
            comp.VerifyDiagnostics(
265 266
            // (6,17): warning CS0184: The given expression is never of the provided ('color') type
            //         var b = 1 is color;
P
Pilchie 已提交
267 268 269 270 271 272 273 274 275 276 277
            Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1 is color").WithArguments("color"));
            comp.VerifyIL("IsTest.Main", @"
{
  // Code size        7 (0x7)
  .maxstack  1
  IL_0000:  ldc.i4.0
  IL_0001:  call       ""void System.Console.WriteLine(bool)""
  IL_0006:  ret
}");
        }

278
        [WorkItem(542466, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void CS0184WRN_IsAlwaysFalse_ExplicitEnumeration()
        {
            var text = @"
class IsTest
{
    static void Main()
    {
        var b = default(color) is int;
        System.Console.WriteLine(b);
    }
}
enum color
{ }
";

            var comp = CompileAndVerify(text, expectedOutput: "False");
            comp.VerifyDiagnostics(
                // (6,17): warning CS0184: The given expression is never of the provided ('int') type
                //         var b = default(color) is int;
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "default(color) is int").WithArguments("int"));
            comp.VerifyIL("IsTest.Main", @"
{
  // Code size        7 (0x7)
  .maxstack  1
  IL_0000:  ldc.i4.0
  IL_0001:  call       ""void System.Console.WriteLine(bool)""
  IL_0006:  ret
}");
        }

310
        [WorkItem(542466, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void CS0184WRN_IsAlwaysFalse_ImplicitNumeric()
        {
            var text = @"
class IsTest
{
    static void Main()
    {
        var b = 1 is double;
        System.Console.WriteLine(b);
    }
}
";

            var comp = CompileAndVerify(text, expectedOutput: "False");
            comp.VerifyDiagnostics(
                // (6,17): warning CS0184: The given expression is never of the provided ('double') type
                //         var b = 1 is double;
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1 is double").WithArguments("double"));
            comp.VerifyIL("IsTest.Main", @"
{
  // Code size        7 (0x7)
  .maxstack  1
  IL_0000:  ldc.i4.0
  IL_0001:  call       ""void System.Console.WriteLine(bool)""
  IL_0006:  ret
}");
        }

340
        [Fact, WorkItem(542466, "DevDiv")]
P
Pilchie 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 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
        public void CS0184WRN_IsAlwaysFalse_ExplicitNumeric()
        {
            var text = @"
class IsTest
{
    static void Main()
    {
        var b = 1.0 is int;
        System.Console.WriteLine(b);
        b = 1.0 is float;
        System.Console.WriteLine(b);
        b = 1 is byte;
        System.Console.WriteLine(b);
    }
}
";

            var comp = CompileAndVerify(text, expectedOutput: @"False
False
False");
            comp.VerifyDiagnostics(
                // (6,17): warning CS0184: The given expression is never of the provided ('int') type
                //         var b = 1.0 is int;
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1.0 is int").WithArguments("int"),
                // (7,13): warning CS0184: The given expression is never of the provided ('float') type
                //         b = 1.0 is float;
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1.0 is float").WithArguments("float"),
                // (8,13): warning CS0184: The given expression is never of the provided ('byte') type
                //         b = 1 is byte;
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1 is byte").WithArguments("byte"));
            comp.VerifyIL("IsTest.Main", @"
{
  // Code size       19 (0x13)
  .maxstack  1
  IL_0000:  ldc.i4.0
  IL_0001:  call       ""void System.Console.WriteLine(bool)""
  IL_0006:  ldc.i4.0
  IL_0007:  call       ""void System.Console.WriteLine(bool)""
  IL_000c:  ldc.i4.0
  IL_000d:  call       ""void System.Console.WriteLine(bool)""
  IL_0012:  ret
}");
        }

385
        [Fact, WorkItem(546371, "DevDiv")]
P
Pilchie 已提交
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
        public void CS0184WRN_IsAlwaysFalse_NumericIsNullable()
        {
            #region src
            var text = @"using System;

public class Test
    {
        const char v0 = default(char);
        public static sbyte v1 = default(sbyte);
        internal static byte v2 = 0;// default(byte);
        protected static short v3 = 0; // default(short);
        private static ushort v4 = default(ushort);

        static void Main()
        {
            const int v5 = 0; //  default(int);
            uint v6 = 0; // default(uint);
            long v7 = 0; // default(long);
            const ulong v8 = default(ulong);
            float v9 = default(float);
            // char
            if (v0 is ushort?)
                Console.Write(0);
            else
                Console.Write(1);
            
            // sbyte & byte
            if (v1 is short? || v2 is short?)
                Console.Write(0);
            else
                Console.Write(2);

            // short & ushort
            if (v3 is int? || v4 is int?)
                Console.Write(0);
            else
                Console.Write(3);

            // int & uint
            if (v5 is long? || v6 is long?)
                Console.Write(0);
            else
                Console.Write(4);

            // long & ulong
            if (v7 is float? || v8 is float?)
                Console.Write(0);
            else
                Console.Write(5);

            // float
            if (v9 is int?)
                Console.Write(0);
            else
                Console.Write(6);
        }
    }
";
            #endregion

            var comp = CompileAndVerify(text, expectedOutput: @"123456");
            comp.VerifyDiagnostics(
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v0 is ushort?").WithArguments("ushort?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v1 is short?").WithArguments("short?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v2 is short?").WithArguments("short?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v3 is int?").WithArguments("int?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v4 is int?").WithArguments("int?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v5 is long?").WithArguments("long?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v6 is long?").WithArguments("long?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v7 is float?").WithArguments("float?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v8 is float?").WithArguments("float?"),
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "v9 is int?").WithArguments("int?")
                );
            comp.VerifyIL("Test.Main", @"
{
  // Code size       61 (0x3d)
  .maxstack  1
  IL_0000:  ldc.i4.1
  IL_0001:  call       ""void System.Console.Write(int)""
  IL_0006:  ldsfld     ""sbyte Test.v1""
  IL_000b:  pop
  IL_000c:  ldsfld     ""byte Test.v2""
  IL_0011:  pop
  IL_0012:  ldc.i4.2
  IL_0013:  call       ""void System.Console.Write(int)""
  IL_0018:  ldsfld     ""short Test.v3""
  IL_001d:  pop
  IL_001e:  ldsfld     ""ushort Test.v4""
  IL_0023:  pop
  IL_0024:  ldc.i4.3
  IL_0025:  call       ""void System.Console.Write(int)""
  IL_002a:  ldc.i4.4
  IL_002b:  call       ""void System.Console.Write(int)""
  IL_0030:  ldc.i4.5
  IL_0031:  call       ""void System.Console.Write(int)""
  IL_0036:  ldc.i4.6
  IL_0037:  call       ""void System.Console.Write(int)""
  IL_003c:  ret
}
");
        }

        // TODO: Add VerifyIL for is and as Codegen tests
        [Fact]
        public void TestAsOperator()
        {
            var source = @"
namespace TestAsOperator
{
    public interface MyInter {}
    public class TestType : MyInter {}
    public class AnotherType {}
    public class MyBase {}
    public class MyDerived : MyBase { }
    public class C
    {
        static void Main()
        {
            string myStr = ""foo"";
            object o = myStr;
            object b = o as string;            

            TestType tt = null;
            o = tt;
            b = o as TestType;

            tt = new TestType();
            o = tt;
            b = o as AnotherType;
        
            b = o as MyInter;

            MyInter mi = new TestType();
            o = mi;
            b = o as MyInter;

            MyBase mb = new MyBase();
            o = mb;
            b = o as MyDerived;      

            MyDerived md = new MyDerived();
            o = md;
            b = o as MyBase;

            b = null as MyBase;            
        }
    }
}
";
            var compilation = CompileAndVerify(source);
        }

        [Fact]
        public void TestAsOperatorGeneric()
        {
            var source = @"
public class TestAsOperatorGeneric
{
    public static void Main() { }
    public static void M<T, U, W>(T t, U u, W w)
        where T : class
        where U : class
        where W : class
    {
        object test2 = u as object;
        System.Console.WriteLine(test2);

        U test3 = t as U;
        System.Console.WriteLine(test3);

        T test4 = t as T;
        System.Console.WriteLine(test4);

        U test5 = u as U;
        System.Console.WriteLine(test5);

        T test12 = u as T;
        System.Console.WriteLine(test12);

        object test7 = w as object;
        System.Console.WriteLine(test7);

        U test8 = w as U;
        System.Console.WriteLine(test8);

        W test9 = u as W;
        System.Console.WriteLine(test9);

        W test10 = t as W;
        System.Console.WriteLine(test10);

        W test11 = w as W;
        System.Console.WriteLine(test11);
    }
}
";
            var compilation = CompileAndVerify(source);
            compilation.VerifyIL("TestAsOperatorGeneric.M<T, U, W>(T, U, W)",
@"{
  // Code size      186 (0xba)
  .maxstack  1
  IL_0000:  ldarg.1
  IL_0001:  box        ""U""
  IL_0006:  call       ""void System.Console.WriteLine(object)""
  IL_000b:  ldarg.0
  IL_000c:  box        ""T""
  IL_0011:  isinst     ""U""
  IL_0016:  unbox.any  ""U""
  IL_001b:  box        ""U""
  IL_0020:  call       ""void System.Console.WriteLine(object)""
  IL_0025:  ldarg.0
  IL_0026:  box        ""T""
  IL_002b:  call       ""void System.Console.WriteLine(object)""
  IL_0030:  ldarg.1
  IL_0031:  box        ""U""
  IL_0036:  call       ""void System.Console.WriteLine(object)""
  IL_003b:  ldarg.1
  IL_003c:  box        ""U""
  IL_0041:  isinst     ""T""
  IL_0046:  unbox.any  ""T""
  IL_004b:  box        ""T""
  IL_0050:  call       ""void System.Console.WriteLine(object)""
  IL_0055:  ldarg.2
  IL_0056:  box        ""W""
  IL_005b:  call       ""void System.Console.WriteLine(object)""
  IL_0060:  ldarg.2
  IL_0061:  box        ""W""
  IL_0066:  isinst     ""U""
  IL_006b:  unbox.any  ""U""
  IL_0070:  box        ""U""
  IL_0075:  call       ""void System.Console.WriteLine(object)""
  IL_007a:  ldarg.1
  IL_007b:  box        ""U""
  IL_0080:  isinst     ""W""
  IL_0085:  unbox.any  ""W""
  IL_008a:  box        ""W""
  IL_008f:  call       ""void System.Console.WriteLine(object)""
  IL_0094:  ldarg.0
  IL_0095:  box        ""T""
  IL_009a:  isinst     ""W""
  IL_009f:  unbox.any  ""W""
  IL_00a4:  box        ""W""
  IL_00a9:  call       ""void System.Console.WriteLine(object)""
  IL_00ae:  ldarg.2
  IL_00af:  box        ""W""
  IL_00b4:  call       ""void System.Console.WriteLine(object)""
  IL_00b9:  ret
}");
        }

636
        [Fact, WorkItem(754408, "DevDiv")]
P
Pilchie 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
        public void TestNullCoalesce_DynamicAndObject()
        {
            var source = @"using System;
public class A {}
public class C
{
    public static dynamic Get()
    {
        object a = new A();
        dynamic b = new A();
        return a ?? b;
    }

    public static void Main()
    {
        var c = Get();
    }
}";
            var comp = CompileAndVerify(source,
                additionalRefs: new[] { CSharpRef, SystemCoreRef_v4_0_30319_17929 },
                expectedOutput: string.Empty);
658
            comp.VerifyIL("C.Get",
P
Pilchie 已提交
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 823 824 825 826 827 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 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
@"{
  // Code size       17 (0x11)
  .maxstack  2
  .locals init (object V_0) //b
  IL_0000:  newobj     ""A..ctor()""
  IL_0005:  newobj     ""A..ctor()""
  IL_000a:  stloc.0
  IL_000b:  dup
  IL_000c:  brtrue.s   IL_0010
  IL_000e:  pop
  IL_000f:  ldloc.0
  IL_0010:  ret
}");
        }

        [Fact]
        public void TestNullCoalesce_Implicit_b_To_a_nullable()
        {
            var source = @"
using System;

// a ?? b

public class E : D { } 
public class D { }
public class C
{
    public static int Main()
    {
        Nullable_a_Implicit_b_to_a0_null_a('a');
        Nullable_a_Implicit_b_to_a0_constant_non_null_a('a');
        Nullable_a_Implicit_b_to_a0_not_null_a('a', 10);
        return 0;
    }

    public static int Nullable_a_Implicit_b_to_a0_null_a(char ch)
    {        
        char b = ch;
        int? a = null;
        int z = a ?? b;
        return z;
    }
    public static int Nullable_a_Implicit_b_to_a0_constant_non_null_a(char ch)
    {
        char b = ch;
        int? a = 10;
        int z = a ?? b;
        return z;
    }
    public static int Nullable_a_Implicit_b_to_a0_not_null_a(char ch, int? i)
    {
        char b = ch;
        int? a = i;
        int z = a ?? b;
        return z;
    }
}
";
            var compilation = CompileAndVerify(source, expectedOutput: string.Empty);
            compilation.VerifyIL("C.Nullable_a_Implicit_b_to_a0_null_a", @"
{
  // Code size       31 (0x1f)
  .maxstack  1
  .locals init (char V_0, //b
  int? V_1)
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldloca.s   V_1
  IL_0004:  initobj    ""int?""
  IL_000a:  ldloc.1
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_1
  IL_000e:  call       ""bool int?.HasValue.get""
  IL_0013:  brtrue.s   IL_0017
  IL_0015:  ldloc.0
  IL_0016:  ret
  IL_0017:  ldloca.s   V_1
  IL_0019:  call       ""int int?.GetValueOrDefault()""
  IL_001e:  ret
}
");
            compilation.VerifyIL("C.Nullable_a_Implicit_b_to_a0_constant_non_null_a", @"
{
  // Code size       29 (0x1d)
  .maxstack  1
  .locals init (char V_0, //b
  int? V_1)
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldc.i4.s   10
  IL_0004:  newobj     ""int?..ctor(int)""
  IL_0009:  stloc.1
  IL_000a:  ldloca.s   V_1
  IL_000c:  call       ""bool int?.HasValue.get""
  IL_0011:  brtrue.s   IL_0015
  IL_0013:  ldloc.0
  IL_0014:  ret
  IL_0015:  ldloca.s   V_1
  IL_0017:  call       ""int int?.GetValueOrDefault()""
  IL_001c:  ret
}");
            compilation.VerifyIL("C.Nullable_a_Implicit_b_to_a0_not_null_a", @"
{
  // Code size       23 (0x17)
  .maxstack  1
  .locals init (char V_0, //b
  int? V_1)
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldarg.1
  IL_0003:  stloc.1
  IL_0004:  ldloca.s   V_1
  IL_0006:  call       ""bool int?.HasValue.get""
  IL_000b:  brtrue.s   IL_000f
  IL_000d:  ldloc.0
  IL_000e:  ret
  IL_000f:  ldloca.s   V_1
  IL_0011:  call       ""int int?.GetValueOrDefault()""
  IL_0016:  ret
}
");
        }

        [Fact]
        public void TestNullCoalesce_Nullable_a_Implicit_b_to_a0()
        {
            var source = @"
using System;

// a ?? b

public class E : D { } 
public class D { }
public class C
{
    public static int Main()
    {
        Nullable_a_Implicit_b_to_a0_null_a('a');
        Nullable_a_Implicit_b_to_a0_constant_non_null_a('a');
        Nullable_a_Implicit_b_to_a0_not_null_a('a', 10);
        Nullable_a_Implicit_b_to_a_null_a('a');
        Nullable_a_Implicit_b_to_a_constant_non_null_a('a');
        Nullable_a_Implicit_b_to_a_not_null_a('a', 10);
        return 0;
    }

    public static int Nullable_a_Implicit_b_to_a0_null_a(char ch)
    {        
        char b = ch;
        int? a = null;
        int z = a ?? b;
        return z;
    }
    public static int Nullable_a_Implicit_b_to_a0_constant_non_null_a(char ch)
    {
        char b = ch;
        int? a = 10;
        int z = a ?? b;
        return z;
    }
    public static int Nullable_a_Implicit_b_to_a0_not_null_a(char ch, int? i)
    {
        char b = ch;
        int? a = i;
        int z = a ?? b;
        return z;
    }

    public static int? Nullable_a_Implicit_b_to_a_null_a(char? ch)
    {
        char? b = ch;
        int? a = null;
        int? z = a ?? b;
        return z;
    }
    public static int? Nullable_a_Implicit_b_to_a_constant_non_null_a(char? ch)
    {
        char? b = ch;
        int? a = 10;
        int? z = a ?? b;
        return z;
    }
    public static int? Nullable_a_Implicit_b_to_a_not_null_a(char ch, int? i)
    {
        char? b = ch;
        int? a = i;
        int? z = a ?? b;
        return z;
    }
}
";
            var compilation = CompileAndVerify(source, expectedOutput: string.Empty);
            compilation.VerifyIL("C.Nullable_a_Implicit_b_to_a0_null_a", @"
{
  // Code size       31 (0x1f)
  .maxstack  1
  .locals init (char V_0, //b
  int? V_1)
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldloca.s   V_1
  IL_0004:  initobj    ""int?""
  IL_000a:  ldloc.1
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_1
  IL_000e:  call       ""bool int?.HasValue.get""
  IL_0013:  brtrue.s   IL_0017
  IL_0015:  ldloc.0
  IL_0016:  ret
  IL_0017:  ldloca.s   V_1
  IL_0019:  call       ""int int?.GetValueOrDefault()""
  IL_001e:  ret
}");
        }

        [Fact]
        public void TestNullCoalesce_Nullable_a_ImplicitReference_a_to_b()
        {
            var source = @"
using System;

// a ?? b

public class E : D { } 
public class D { }
public class C
{
    public static int Main()
    {
        ImplicitReference_a_to_b_null_a_nullable(10);
        ImplicitReference_a_to_b_constant_nonnull_a_nullable(10);
        ImplicitReference_a_to_b_not_null_a_nullable(10, 'a');
        Null_Literal_a('a');
        return 0;
    }

    public static int? ImplicitReference_a_to_b_null_a_nullable(int? c)
    {
        int? b = c;
        char? a = null;
        int? z = a ?? b;
        return z;
    }
    public static int? ImplicitReference_a_to_b_constant_nonnull_a_nullable(int? c)
    {
        int? b = c;
        char? a = 'a';
        int? z = a ?? b;
        return z;
    }
    public static int? ImplicitReference_a_to_b_not_null_a_nullable(int? c, char? d)
    {
        int? b = c;
        char? a = d;
        int? z = a ?? b;
        return z;
    }
    public static char? Null_Literal_a(char? ch)
    {
        char? b = ch;
        char? z = null ?? b;
        return z;
    }
}
";
            var compilation = CompileAndVerify(source, expectedOutput: string.Empty);
            compilation.VerifyIL("C.ImplicitReference_a_to_b_null_a_nullable", @"
{
  // Code size       36 (0x24)
  .maxstack  1
  .locals init (int? V_0, //b
  char? V_1)
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldloca.s   V_1
  IL_0004:  initobj    ""char?""
  IL_000a:  ldloc.1
  IL_000b:  stloc.1
  IL_000c:  ldloca.s   V_1
  IL_000e:  call       ""bool char?.HasValue.get""
  IL_0013:  brtrue.s   IL_0017
  IL_0015:  ldloc.0
  IL_0016:  ret
  IL_0017:  ldloca.s   V_1
  IL_0019:  call       ""char char?.GetValueOrDefault()""
  IL_001e:  newobj     ""int?..ctor(int)""
  IL_0023:  ret
}");

            compilation.VerifyIL("C.Null_Literal_a", @"
{
  // Code size        2 (0x2)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  ret
}");
        }

        [Fact]
        public void TestNullCoalesce_ImplicitReference_a_to_b()
        {
            var source = @"
using System;

// a ?? b

public class E : D { } 
public class D { }
public class C
{
    public static int Main()
    {
        ImplicitReference_a_to_b_null_a();
        ImplicitReference_a_to_b_not_null_a();
        return 0;
    }
    public static D ImplicitReference_a_to_b_null_a()
    {
        D b = new D();
        E a = null;
        D z = a ?? b;
        return z;
    }
    public static D ImplicitReference_a_to_b_not_null_a()
    {
        D b = new D();
        E a = new E();
        D z = a ?? b;
        return z;
    }
}
";
            var compilation = CompileAndVerify(source, expectedOutput: string.Empty);
            compilation.VerifyIL("C.ImplicitReference_a_to_b_null_a", @"
{
  // Code size       13 (0xd)
  .maxstack  2
  .locals init (D V_0) //b
  IL_0000:  newobj     ""D..ctor()""
  IL_0005:  stloc.0
  IL_0006:  ldnull
  IL_0007:  dup
  IL_0008:  brtrue.s   IL_000c
  IL_000a:  pop
  IL_000b:  ldloc.0
  IL_000c:  ret
}
");
            compilation.VerifyIL("C.ImplicitReference_a_to_b_not_null_a",
@"
{
  // Code size       17 (0x11)
  .maxstack  2
  .locals init (D V_0) //b
  IL_0000:  newobj     ""D..ctor()""
  IL_0005:  stloc.0
  IL_0006:  newobj     ""E..ctor()""
  IL_000b:  dup
  IL_000c:  brtrue.s   IL_0010
  IL_000e:  pop
  IL_000f:  ldloc.0
  IL_0010:  ret
}
");
        }

1025
        [WorkItem(541337, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void TestNullCoalesce_TypeParameter_Bug8008()
        {
            var source = @"
static class Program
{
    static void Main()
    {
    }
 
    static void Foo<T>(T x)
    {
        var y = default(T) ?? x;
    }
}
";
            CreateCompilationWithMscorlib(source).VerifyDiagnostics(
                // (10,17): error CS0019: Operator '??' cannot be applied to operands of type 'T' and 'T'
                //         var y = default(T) ?? x;
                Diagnostic(ErrorCode.ERR_BadBinaryOps, "default(T) ?? x").WithArguments("??", "T", "T").WithLocation(10, 17)); ;
        }

        [Fact]
        public void TestNullCoalesce_NoDuplicateCallsToFoo()
        {
            var source = @"
// a ?? b

public class Test
{
    static void Main()
    {
        object o = Foo() ?? Bar();
    }

    static object Foo()
    {
        System.Console.Write(""Foo"");
        return new object();
    }

    static object Bar()
    {
        System.Console.Write(""Bar"");
        return new object();
    }
}
";
            var compilation = CompileAndVerify(source, expectedOutput: "Foo");
            compilation.VerifyIL("Test.Main", @"
{
  // Code size       14 (0xe)
  .maxstack  1
  IL_0000:  call       ""object Test.Foo()""
  IL_0005:  brtrue.s   IL_000d
  IL_0007:  call       ""object Test.Bar()""
  IL_000c:  pop
  IL_000d:  ret
}
");
        }

1088
        [WorkItem(541232, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void TestNullCoalesce_MethodGroups()
        {
            var source =
@"class C
{
    static void M()
    {
        System.Action a = null;
        a = a ?? M;
        a = M ?? a;
    }
}";
            CreateCompilationWithMscorlib(source).VerifyDiagnostics(
                // (7,13): error CS0019: Operator '??' cannot be applied to operands of type 'method group' and 'System.Action'
                Diagnostic(ErrorCode.ERR_BadBinaryOps, "M ?? a").WithArguments("??", "method group", "System.Action").WithLocation(7, 13));
        }

        /// <summary>
        /// From orcas bug #42645.  PEVerify fails.
        /// </summary>
        [Fact]
        public void TestNullCoalesce_InterfaceRegression1()
        {
            var source = @"
using System.Collections.Generic;
using System.Security;

[assembly: SecurityTransparent()]

public class Test
{
    static void Main()
    {
        int[] a = new int[] { };
        List<int> b = new List<int>();

        IEnumerable<int> c = a ?? (IEnumerable<int>)b;
        Foo(c);
    }

    static void Foo<T>(T x)
    {
        System.Console.WriteLine(typeof(T));
    }
}";
            // Note the explicit casts, even though the conversions are
            // implicit reference conversions.
            var comp = CompileAndVerify(source, expectedOutput: "System.Collections.Generic.IEnumerable`1[System.Int32]");
            comp.VerifyDiagnostics();
            comp.VerifyIL("Test.Main", @"
{
  // Code size       25 (0x19)
  .maxstack  2
  .locals init (System.Collections.Generic.List<int> V_0, //b
  System.Collections.Generic.IEnumerable<int> V_1)
  IL_0000:  ldc.i4.0
  IL_0001:  newarr     ""int""
  IL_0006:  newobj     ""System.Collections.Generic.List<int>..ctor()""
  IL_000b:  stloc.0
  IL_000c:  stloc.1
  IL_000d:  ldloc.1
  IL_000e:  dup
  IL_000f:  brtrue.s   IL_0013
  IL_0011:  pop
  IL_0012:  ldloc.0
  IL_0013:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>)""
  IL_0018:  ret
}
");
        }

        [Fact]
        public void TestNullCoalesce_InterfaceRegression1a()
        {
            var source = @"
using System.Collections.Generic;

public class Test
{
    static void Main()
    {
        int[] a = new int[] { };
        IEnumerable<int> b = new List<int>();

        IEnumerable<int> c = b ?? a;
        Foo(c);
    }

    static void Foo<T>(T x)
    {
        System.Console.WriteLine(typeof(T));
    }
}";

            var comp = CompileAndVerify(source, expectedOutput: "System.Collections.Generic.IEnumerable`1[System.Int32]");
            comp.VerifyDiagnostics();
            comp.VerifyIL("Test.Main", @"
{
  // Code size       25 (0x19)
  .maxstack  2
  .locals init (int[] V_0, //a
  System.Collections.Generic.IEnumerable<int> V_1)
  IL_0000:  ldc.i4.0
  IL_0001:  newarr     ""int""
  IL_0006:  stloc.0
  IL_0007:  newobj     ""System.Collections.Generic.List<int>..ctor()""
  IL_000c:  stloc.1
  IL_000d:  ldloc.1
  IL_000e:  dup
  IL_000f:  brtrue.s   IL_0013
  IL_0011:  pop
  IL_0012:  ldloc.0
  IL_0013:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>)""
  IL_0018:  ret
}");
        }

        [Fact]
        public void TestNullCoalesce_InterfaceRegression1b()
        {
            var source = @"
using System.Collections.Generic;

public class Test
{
    static void Main()
    {
        int[] a = new int[] { };
        IEnumerable<int> b;

        IEnumerable<int> c = (b = (IEnumerable<int>)new List<int>()) ?? a;
        Foo(c);
        Foo(b);
    }

    static void Foo<T>(T x)
    {
        System.Console.Write(typeof(T));
    }
}";

            var comp = CompileAndVerify(source, expectedOutput: "System.Collections.Generic.IEnumerable`1[System.Int32]System.Collections.Generic.IEnumerable`1[System.Int32]");
            comp.VerifyDiagnostics();
            comp.VerifyIL("Test.Main", @"
{
  // Code size       31 (0x1f)
  .maxstack  3
  .locals init (int[] V_0, //a
  System.Collections.Generic.IEnumerable<int> V_1)
  IL_0000:  ldc.i4.0
  IL_0001:  newarr     ""int""
  IL_0006:  stloc.0
  IL_0007:  newobj     ""System.Collections.Generic.List<int>..ctor()""
  IL_000c:  dup
  IL_000d:  stloc.1
  IL_000e:  ldloc.1
  IL_000f:  dup
  IL_0010:  brtrue.s   IL_0014
  IL_0012:  pop
  IL_0013:  ldloc.0
  IL_0014:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>)""
  IL_0019:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>)""
  IL_001e:  ret
}");
        }

        [Fact]
        public void TestNullCoalesce_InterfaceRegression1c()
        {
            var source = @"
using System.Collections.Generic;

public class Test
{
    static void Main()
    {
        int[] a = new int[] { };
        IEnumerable<int> b = new List<int>();

        Foo(b, b ?? a);
    }

    static void Foo<T, U>(T x, U y)
    {
        System.Console.Write(typeof(T));
    }
}";

            var comp = CompileAndVerify(source, expectedOutput: "System.Collections.Generic.IEnumerable`1[System.Int32]");
            comp.VerifyDiagnostics();
            comp.VerifyIL("Test.Main", @"
{
  // Code size       26 (0x1a)
  .maxstack  3
  .locals init (int[] V_0, //a
  System.Collections.Generic.IEnumerable<int> V_1)
  IL_0000:  ldc.i4.0
  IL_0001:  newarr     ""int""
  IL_0006:  stloc.0
  IL_0007:  newobj     ""System.Collections.Generic.List<int>..ctor()""
  IL_000c:  dup
  IL_000d:  stloc.1
  IL_000e:  ldloc.1
  IL_000f:  dup
  IL_0010:  brtrue.s   IL_0014
  IL_0012:  pop
  IL_0013:  ldloc.0
  IL_0014:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>, System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>, System.Collections.Generic.IEnumerable<int>)""
  IL_0019:  ret
}
");
        }

        /// <summary>
        /// From whidbey bug #49619.  PEVerify fails.
        /// </summary>
        /// <remarks>
        /// Dev10 does not produce verifiable code for this example.
        /// </remarks>
        [Fact]
        public void TestNullCoalesce_InterfaceRegression2()
        {
            var source = @"
public interface IA { }
public interface IB { int f(); }
public class AB1 : IA, IB { public int f() { return 42; } }
public class AB2 : IA, IB { public int f() { return 1; } }

class MainClass
{
    public static void g(AB1 ab1)
    {
        ((IB)ab1 ?? (IB)new AB2()).f();
    }
}";
            // Note the explicit casts, even though the conversions are
            // implicit reference conversions.
            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("MainClass.g", @"
{
  // Code size       19 (0x13)
  .maxstack  2
  .locals init (IB V_0)
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldloc.0
  IL_0003:  dup
  IL_0004:  brtrue.s   IL_000c
  IL_0006:  pop
  IL_0007:  newobj     ""AB2..ctor()""
  IL_000c:  callvirt   ""int IB.f()""
  IL_0011:  pop
  IL_0012:  ret
}");
        }

1347
        [Fact, WorkItem(638289, "DevDiv")]
P
Pilchie 已提交
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
        public void TestNullCoalesce_Nested()
        {
            var src =
@"interface I { void DoNothing(); }
class A : I { public void DoNothing() {} }
class B : I { public void DoNothing() {} }
class C : I
{
    public void DoNothing() {}

    static I Tester(A a, B b)
    {
        I i = a ?? (b ?? (I)new C());
        i.DoNothing();
        i = a ?? ((I)b ?? new C());
        i.DoNothing();
        i = ((I)a ?? b) ?? new C();
        i.DoNothing();
        return i;
    }

    static void Main()
    {
        System.Console.Write(Tester(null, null).GetType());
    }
}";
1374
            var verify = CompileAndVerify(src,
1375
                options: TestOptions.DebugExe, expectedOutput: "C");
P
Pilchie 已提交
1376 1377
            verify.VerifyIL("C.Tester", @"
{
1378
  // Code size       86 (0x56)
P
Pilchie 已提交
1379 1380
  .maxstack  2
  .locals init (I V_0, //i
1381 1382
                I V_1,
                I V_2)
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
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  stloc.1
  IL_0003:  ldloc.1
  IL_0004:  dup
  IL_0005:  brtrue.s   IL_0014
  IL_0007:  pop
  IL_0008:  ldarg.1
  IL_0009:  stloc.1
  IL_000a:  ldloc.1
  IL_000b:  dup
  IL_000c:  brtrue.s   IL_0014
  IL_000e:  pop
  IL_000f:  newobj     ""C..ctor()""
  IL_0014:  stloc.0
  IL_0015:  ldloc.0
  IL_0016:  callvirt   ""void I.DoNothing()""
  IL_001b:  nop
  IL_001c:  ldarg.0
  IL_001d:  stloc.1
  IL_001e:  ldloc.1
  IL_001f:  dup
  IL_0020:  brtrue.s   IL_002f
  IL_0022:  pop
  IL_0023:  ldarg.1
  IL_0024:  stloc.1
  IL_0025:  ldloc.1
  IL_0026:  dup
  IL_0027:  brtrue.s   IL_002f
  IL_0029:  pop
  IL_002a:  newobj     ""C..ctor()""
  IL_002f:  stloc.0
  IL_0030:  ldloc.0
  IL_0031:  callvirt   ""void I.DoNothing()""
  IL_0036:  nop
  IL_0037:  ldarg.0
  IL_0038:  stloc.1
  IL_0039:  ldloc.1
  IL_003a:  dup
  IL_003b:  brtrue.s   IL_003f
  IL_003d:  pop
  IL_003e:  ldarg.1
  IL_003f:  dup
  IL_0040:  brtrue.s   IL_0048
  IL_0042:  pop
  IL_0043:  newobj     ""C..ctor()""
  IL_0048:  stloc.0
  IL_0049:  ldloc.0
  IL_004a:  callvirt   ""void I.DoNothing()""
  IL_004f:  nop
  IL_0050:  ldloc.0
1434
  IL_0051:  stloc.2
1435
  IL_0052:  br.s       IL_0054
1436
  IL_0054:  ldloc.2
1437
  IL_0055:  ret
P
Pilchie 已提交
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
}");
            // Optimized
            verify = CompileAndVerify(src, expectedOutput: "C");
            verify.VerifyIL("C.Tester", @"
{
  // Code size       72 (0x48)
  .maxstack  2
  .locals init (I V_0)
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldloc.0
  IL_0003:  dup
  IL_0004:  brtrue.s   IL_0013
  IL_0006:  pop
  IL_0007:  ldarg.1
  IL_0008:  stloc.0
  IL_0009:  ldloc.0
  IL_000a:  dup
  IL_000b:  brtrue.s   IL_0013
  IL_000d:  pop
  IL_000e:  newobj     ""C..ctor()""
  IL_0013:  callvirt   ""void I.DoNothing()""
  IL_0018:  ldarg.0
  IL_0019:  stloc.0
  IL_001a:  ldloc.0
  IL_001b:  dup
  IL_001c:  brtrue.s   IL_002b
  IL_001e:  pop
  IL_001f:  ldarg.1
  IL_0020:  stloc.0
  IL_0021:  ldloc.0
  IL_0022:  dup
  IL_0023:  brtrue.s   IL_002b
  IL_0025:  pop
  IL_0026:  newobj     ""C..ctor()""
  IL_002b:  callvirt   ""void I.DoNothing()""
  IL_0030:  ldarg.0
  IL_0031:  stloc.0
  IL_0032:  ldloc.0
  IL_0033:  dup
  IL_0034:  brtrue.s   IL_0038
  IL_0036:  pop
  IL_0037:  ldarg.1
  IL_0038:  dup
  IL_0039:  brtrue.s   IL_0041
  IL_003b:  pop
  IL_003c:  newobj     ""C..ctor()""
  IL_0041:  dup
  IL_0042:  callvirt   ""void I.DoNothing()""
  IL_0047:  ret
}");
        }

        [Fact]
        public void TestNullCoalesce_FuncVariance()
        {
            var source = @"
using System;
using System.Collections.Generic;

    class Program
    {
        static void Main(string[] args)
        {
            Func<Exception[]> f1 = null;

            Func<IEnumerable<object>> f2 = null;

            var oo = f1 ?? f2;
            Console.WriteLine(oo);

            oo = f2 ?? f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       31 (0x1f)
  .maxstack  2
  .locals init (System.Func<System.Exception[]> V_0, //f1
  System.Func<System.Collections.Generic.IEnumerable<object>> V_1, //f2
  System.Func<System.Collections.Generic.IEnumerable<object>> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  ldloc.0
  IL_0005:  stloc.2
  IL_0006:  ldloc.2
  IL_0007:  dup
  IL_0008:  brtrue.s   IL_000c
  IL_000a:  pop
  IL_000b:  ldloc.1
  IL_000c:  call       ""void System.Console.WriteLine(object)""
  IL_0011:  ldloc.1
  IL_0012:  dup
  IL_0013:  brtrue.s   IL_0019
  IL_0015:  pop
  IL_0016:  ldloc.0
  IL_0017:  stloc.2
  IL_0018:  ldloc.2
  IL_0019:  call       ""void System.Console.WriteLine(object)""
  IL_001e:  ret
}
");
        }

        [Fact]
        public void TestNullCoalesce_FuncVariance01()
        {
            var source = @"
using System;
using System.Collections;
using System.Collections.Generic;

    class Program
    {
        static void Main(string[] args)
        {
            Func<Exception[]> f1 = null;

            Func<IEnumerable<object>> f2 = null;

            var oo = (Func<IEnumerable>)f1 ?? (Func<IEnumerable>)f2;
            Console.WriteLine(oo);

            oo = (Func<IEnumerable>)f2 ?? (Func<IEnumerable>)f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       35 (0x23)
  .maxstack  2
  .locals init (System.Func<System.Exception[]> V_0, //f1
  System.Func<System.Collections.Generic.IEnumerable<object>> V_1, //f2
  System.Func<System.Collections.IEnumerable> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  ldloc.0
  IL_0005:  stloc.2
  IL_0006:  ldloc.2
  IL_0007:  dup
  IL_0008:  brtrue.s   IL_000e
  IL_000a:  pop
  IL_000b:  ldloc.1
  IL_000c:  stloc.2
  IL_000d:  ldloc.2
  IL_000e:  call       ""void System.Console.WriteLine(object)""
  IL_0013:  ldloc.1
  IL_0014:  stloc.2
  IL_0015:  ldloc.2
  IL_0016:  dup
  IL_0017:  brtrue.s   IL_001d
  IL_0019:  pop
  IL_001a:  ldloc.0
  IL_001b:  stloc.2
  IL_001c:  ldloc.2
  IL_001d:  call       ""void System.Console.WriteLine(object)""
  IL_0022:  ret
}
");
        }

        [Fact]
        public void TestNullCoalesce_InterfaceVariance()
        {
            var source = @"
using System;
using System.Collections.Generic;

    class Program
    {
        interface CoInter<out T>
        {
        }

        static void Main(string[] args)
        {
            CoInter<Exception[]> f1 = null;

            CoInter<IEnumerable<object>> f2 = null;

            var oo = f1 ?? f2;
            Console.WriteLine(oo);

            oo = f2 ?? f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       31 (0x1f)
  .maxstack  2
  .locals init (Program.CoInter<System.Exception[]> V_0, //f1
  Program.CoInter<System.Collections.Generic.IEnumerable<object>> V_1, //f2
  Program.CoInter<System.Collections.Generic.IEnumerable<object>> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  ldloc.0
  IL_0005:  stloc.2
  IL_0006:  ldloc.2
  IL_0007:  dup
  IL_0008:  brtrue.s   IL_000c
  IL_000a:  pop
  IL_000b:  ldloc.1
  IL_000c:  call       ""void System.Console.WriteLine(object)""
  IL_0011:  ldloc.1
  IL_0012:  dup
  IL_0013:  brtrue.s   IL_0019
  IL_0015:  pop
  IL_0016:  ldloc.0
  IL_0017:  stloc.2
  IL_0018:  ldloc.2
  IL_0019:  call       ""void System.Console.WriteLine(object)""
  IL_001e:  ret
}
");
        }

        [Fact]
        public void TestNullCoalesce_InterfaceVariance01()
        {
            var source = @"
using System;
using System.Collections;
using System.Collections.Generic;

    class Program
    {
        interface CoInter<out T>
        {
        }

        static void Main(string[] args)
        {
            CoInter<Exception[]> f1 = null;

            CoInter<IEnumerable<object>> f2 = null;

            var oo = (CoInter<IEnumerable>)f1 ?? (CoInter<IEnumerable>)f2;
            Console.WriteLine(oo);

            oo = (CoInter<IEnumerable>)f2 ?? (CoInter<IEnumerable>)f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       35 (0x23)
  .maxstack  2
  .locals init (Program.CoInter<System.Exception[]> V_0, //f1
  Program.CoInter<System.Collections.Generic.IEnumerable<object>> V_1, //f2
  Program.CoInter<System.Collections.IEnumerable> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  ldloc.0
  IL_0005:  stloc.2
  IL_0006:  ldloc.2
  IL_0007:  dup
  IL_0008:  brtrue.s   IL_000e
  IL_000a:  pop
  IL_000b:  ldloc.1
  IL_000c:  stloc.2
  IL_000d:  ldloc.2
  IL_000e:  call       ""void System.Console.WriteLine(object)""
  IL_0013:  ldloc.1
  IL_0014:  stloc.2
  IL_0015:  ldloc.2
  IL_0016:  dup
  IL_0017:  brtrue.s   IL_001d
  IL_0019:  pop
  IL_001a:  ldloc.0
  IL_001b:  stloc.2
  IL_001c:  ldloc.2
  IL_001d:  call       ""void System.Console.WriteLine(object)""
  IL_0022:  ret
}
");
        }

1741
        [WorkItem(543074, "DevDiv")]
P
Pilchie 已提交
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
        [Fact]
        public void TestEqualEqualOnNestedStructGuid()
        {
            var source = @"
using System;

public class Parent
{
    public System.Guid Foo(int d = 0, System.Guid g = default(System.Guid)) { return g; }
}

public class Test
{
    public static void Main()
    {
        var x = new Parent().Foo();
        var ret = x == default(System.Guid); 
        Console.Write(ret);
    }
}
";
            CompileAndVerify(source, expectedOutput: "True");
        }

1766
        [WorkItem(543092, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void ShortCircuitConditionalOperator()
        {
            var source = @"
using System;
using System.Linq;
using System.Collections.Generic;

class X
{
    public int selectCount = 0;
    public bool Select<T>(Func<int, T> selector)
    {
        selectCount++;
        return true;
    }
}

class P
{
    static int Main()
    {
        int errCount = 0;
        var src = new X();
        // QE is not 'executed'
        var b = false && from x in src select x; // WRN CS0429
        if (src.selectCount == 1)
            errCount++;

        Console.Write(errCount);
        return (errCount > 0) ? 1 : 0;
    }
}";
            CompileAndVerify(source, additionalRefs: new[] { LinqAssemblyRef },
                expectedOutput: "0");
        }

1804
        [WorkItem(543109, "DevDiv")]
P
Pilchie 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
        [Fact()]
        public void ShortCircuitConditionalOperator02()
        {
            var source = @"
using System;

class P
{
    static int Main()
    {
        int errCount = 0;
        var f = false;
        var b = f && (0 == errCount++);
        Console.Write(errCount);
        return (errCount > 0) ? 1 : 0;
    }
}";
            CompileAndVerify(source, additionalRefs: new[] { LinqAssemblyRef },
                expectedOutput: "0");
        }

1826
        [WorkItem(543377, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void DecimalComparison()
        {
            var source = @"
class Program
{
    static void Main()
    {
        decimal d1 = 1.0201m;
        if (d1 == 10201M)
        {
        }
    }
}";
            CompileAndVerify(source).
VerifyIL("Program.Main", @"
{
  // Code size       31 (0x1f)
  .maxstack  5
  IL_0000:  ldc.i4     0x27d9
  IL_0005:  ldc.i4.0
  IL_0006:  ldc.i4.0
  IL_0007:  ldc.i4.0
  IL_0008:  ldc.i4.4
  IL_0009:  newobj     ""decimal..ctor(int, int, int, bool, byte)""
  IL_000e:  ldc.i4     0x27d9
  IL_0013:  newobj     ""decimal..ctor(int)""
  IL_0018:  call       ""bool decimal.op_Equality(decimal, decimal)""
  IL_001d:  pop
  IL_001e:  ret
}");
        }

1860
        [WorkItem(543453, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void TestIncrementDecrementOperator_Generic()
        {
            var source = @"
using System;
class BaseType<T> where T : BaseType<T>, new()
{
    public static T operator ++(BaseType<T> x)
    {
        return null;
    }
    public static implicit operator T(BaseType<T> x)
    {
        return null;
    }
}

class DrivedType : BaseType<DrivedType>
{
    public static void Main()
    {
        BaseType<DrivedType> test = new BaseType<DrivedType>();
        DrivedType test2 = test++;
    }
}
";
            CompileAndVerify(source);
        }

1890
        [WorkItem(543474, "DevDiv")]
P
Pilchie 已提交
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
        [Fact]
        public void LogicalComplementOperator()
        {
            var source = @"
class Program
{
    static void Main(string[] args)
    {
        checked
        {
            var b = false;
            if (!b) {System.Console.Write(""1""); }
        }
    }
}";
            CompileAndVerify(source,
                expectedOutput: "1");
        }

1910
        [WorkItem(543500, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void BuiltInLeftShiftOperators()
        {
            var source = @"
using System;

class Program
{
    static void Main(string[] args)
    {
        int i = 1;
        uint ui = 1;
        long l = 1;
        ulong ul = 1;

        Console.WriteLine(i << 31);
        Console.WriteLine(i << 32);
        Console.WriteLine(i << 33);
        Console.WriteLine(i << -1);
        Console.WriteLine();

        Console.WriteLine(ui << 31);
        Console.WriteLine(ui << 32);
        Console.WriteLine(ui << 33);
        Console.WriteLine(ui << -1);
        Console.WriteLine();

        Console.WriteLine(l << 63);
        Console.WriteLine(l << 64);
        Console.WriteLine(l << 65);
        Console.WriteLine(l << -1);
        Console.WriteLine();

        Console.WriteLine(ul << 63);
        Console.WriteLine(ul << 64);
        Console.WriteLine(ul << 65);
        Console.WriteLine(ul << -1);
        Console.WriteLine();
    }
}";
            CompileAndVerify(source,
                expectedOutput: @"
-2147483648
1
2
-2147483648

2147483648
1
2
2147483648

-9223372036854775808
1
2
-9223372036854775808

9223372036854775808
1
2
9223372036854775808
");
        }

1975
        [WorkItem(543500, "DevDiv")]
P
Pilchie 已提交
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 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
        [Fact]
        public void BuiltInRightShiftOperators()
        {
            var source = @"
using System;

class Program
{
    static void Main(string[] args)
    {
        int i = int.MaxValue;
        uint ui = uint.MaxValue;
        long l = long.MaxValue;
        ulong ul = ulong.MaxValue;

        Console.WriteLine(i >> 31);
        Console.WriteLine(i >> 32);
        Console.WriteLine(i >> 33);
        Console.WriteLine(i >> -1);
        Console.WriteLine();

        Console.WriteLine(ui >> 31);
        Console.WriteLine(ui >> 32);
        Console.WriteLine(ui >> 33);
        Console.WriteLine(ui >> -1);
        Console.WriteLine();

        Console.WriteLine(l >> 63);
        Console.WriteLine(l >> 64);
        Console.WriteLine(l >> 65);
        Console.WriteLine(l >> -1);
        Console.WriteLine();

        Console.WriteLine(ul >> 63);
        Console.WriteLine(ul >> 64);
        Console.WriteLine(ul >> 65);
        Console.WriteLine(ul >> -1);
        Console.WriteLine();
    }
}";
            CompileAndVerify(source,
                expectedOutput: @"
0
2147483647
1073741823
0

1
4294967295
2147483647
1

0
9223372036854775807
4611686018427387903
0

1
18446744073709551615
9223372036854775807
1
");
        }

2040
        [WorkItem(543500, "DevDiv")]
P
Pilchie 已提交
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 2104 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
        [Fact]
        public void BuiltInShiftOperators()
        {
            var source = @"
using System;

class Program
{
    static void Main(string[] args)
    {
        int i = 1;
        long l = long.MaxValue;
        int amount = 1;

        Console.WriteLine(i << 31); // 31
        Console.WriteLine(i << 32); // no shift
        Console.WriteLine(i << 33); // 1
        Console.WriteLine(i << -1); // 31
        Console.WriteLine(i >> amount); // & 31
        Console.WriteLine();

        Console.WriteLine(l >> 63); // 63
        Console.WriteLine(l >> 64); // no shift
        Console.WriteLine(l >> 65); // 1
        Console.WriteLine(l >> -1); // 63
        Console.WriteLine(l >> amount); // & 63
        Console.WriteLine();
    }
}";
            var verifier = CompileAndVerify(source,
                expectedOutput: @"
-2147483648
1
2
-2147483648
0

0
9223372036854775807
4611686018427387903
0
4611686018427387903
");

            verifier.VerifyIL("Program.Main", @"
{
  // Code size      109 (0x6d)
  .maxstack  3
  .locals init (long V_0, //l
  int V_1) //amount
  IL_0000:  ldc.i4.1
  IL_0001:  ldc.i8     0x7fffffffffffffff
  IL_000a:  stloc.0
  IL_000b:  ldc.i4.1
  IL_000c:  stloc.1
  IL_000d:  dup
  IL_000e:  ldc.i4.s   31
  IL_0010:  shl
  IL_0011:  call       ""void System.Console.WriteLine(int)""
  IL_0016:  dup
  IL_0017:  call       ""void System.Console.WriteLine(int)""
  IL_001c:  dup
  IL_001d:  ldc.i4.1
  IL_001e:  shl
  IL_001f:  call       ""void System.Console.WriteLine(int)""
  IL_0024:  dup
  IL_0025:  ldc.i4.s   31
  IL_0027:  shl
  IL_0028:  call       ""void System.Console.WriteLine(int)""
  IL_002d:  ldloc.1
  IL_002e:  ldc.i4.s   31
  IL_0030:  and
  IL_0031:  shr
  IL_0032:  call       ""void System.Console.WriteLine(int)""
  IL_0037:  call       ""void System.Console.WriteLine()""
  IL_003c:  ldloc.0
  IL_003d:  ldc.i4.s   63
  IL_003f:  shr
  IL_0040:  call       ""void System.Console.WriteLine(long)""
  IL_0045:  ldloc.0
  IL_0046:  call       ""void System.Console.WriteLine(long)""
  IL_004b:  ldloc.0
  IL_004c:  ldc.i4.1
  IL_004d:  shr
  IL_004e:  call       ""void System.Console.WriteLine(long)""
  IL_0053:  ldloc.0
  IL_0054:  ldc.i4.s   63
  IL_0056:  shr
  IL_0057:  call       ""void System.Console.WriteLine(long)""
  IL_005c:  ldloc.0
  IL_005d:  ldloc.1
  IL_005e:  ldc.i4.s   63
  IL_0060:  and
  IL_0061:  shr
  IL_0062:  call       ""void System.Console.WriteLine(long)""
  IL_0067:  call       ""void System.Console.WriteLine()""
  IL_006c:  ret
}
");
        }

2142
        [Fact, WorkItem(543993, "DevDiv")]
P
Pilchie 已提交
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 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
        public void BuiltInShiftOperators01()
        {
            var source = @"
using System;

public class Test
{
    public static void Main()
    {
        int n = 2;
        int v1 = (1 << n) << 3;
        int v2 = 1 << n << 3;
        Console.Write(""{0}=={1} "", v1, v2);
        v1 = (1 >> n) >> 3;
        v2 = 1 >> n >> 3;
        Console.Write(""{0}=={1}"", v1, v2);
    }
}
";
            var verifier = CompileAndVerify(source,
                expectedOutput: @"32==32 0==0");

            verifier.VerifyIL("Test.Main", @"
{
  // Code size       83 (0x53)
  .maxstack  3
  .locals init (int V_0, //n
  int V_1, //v1
  int V_2) //v2
  IL_0000:  ldc.i4.2
  IL_0001:  stloc.0
  IL_0002:  ldc.i4.1
  IL_0003:  ldloc.0
  IL_0004:  ldc.i4.s   31
  IL_0006:  and
  IL_0007:  shl
  IL_0008:  ldc.i4.3
  IL_0009:  shl
  IL_000a:  stloc.1
  IL_000b:  ldc.i4.1
  IL_000c:  ldloc.0
  IL_000d:  ldc.i4.s   31
  IL_000f:  and
  IL_0010:  shl
  IL_0011:  ldc.i4.3
  IL_0012:  shl
  IL_0013:  stloc.2
  IL_0014:  ldstr      ""{0}=={1} ""
  IL_0019:  ldloc.1
  IL_001a:  box        ""int""
  IL_001f:  ldloc.2
  IL_0020:  box        ""int""
  IL_0025:  call       ""void System.Console.Write(string, object, object)""
  IL_002a:  ldc.i4.1
  IL_002b:  ldloc.0
  IL_002c:  ldc.i4.s   31
  IL_002e:  and
  IL_002f:  shr
  IL_0030:  ldc.i4.3
  IL_0031:  shr
  IL_0032:  stloc.1
  IL_0033:  ldc.i4.1
  IL_0034:  ldloc.0
  IL_0035:  ldc.i4.s   31
  IL_0037:  and
  IL_0038:  shr
  IL_0039:  ldc.i4.3
  IL_003a:  shr
  IL_003b:  stloc.2
  IL_003c:  ldstr      ""{0}=={1}""
  IL_0041:  ldloc.1
  IL_0042:  box        ""int""
  IL_0047:  ldloc.2
  IL_0048:  box        ""int""
  IL_004d:  call       ""void System.Console.Write(string, object, object)""
  IL_0052:  ret
}
");
        }

2223
        [Fact, WorkItem(543993, "DevDiv")]
P
Pilchie 已提交
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
        public void BuiltInShiftOperators02()
        {
            var source = @"
class Program
{
    static void Main()
    {
        System.Console.WriteLine(1 << 2 << 3);
        System.Console.WriteLine((1 << 2) << 3);
        System.Console.WriteLine(1 << (2 << 3));
    }
}
";
            CompileAndVerify(source, expectedOutput: @"
32
32
65536");
        }

2243
        [WorkItem(543568, "DevDiv")]
P
Pilchie 已提交
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
        [Fact]
        public void ImplicitConversionOperatorWithOptionalParam()
        {
            var source = @"
using System;

public class C
{
    static public implicit operator int(C d = null) // warning CS1066
    {
        if (d != null) return 0;
        return 1;
    }
}

class TestFunction
{
    public static void Main()
    {
        var tf = new C();
        int result = tf;
        Console.Write(result);
    }
}
";
            var verifier = CompileAndVerify(source, expectedOutput: @"0");
        }

2272
        [WorkItem(543569, "DevDiv")]
P
Pilchie 已提交
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
        [Fact()]
        public void AssignmentInOperandOfIsAlwaysFalse()
        {
            var source = @"
using System;

public class Program
{
    public static void Main()
    {
        int[] a = null;
        bool result = (a = new[] { 4, 5, 6 }) is char[]; // warning CS0184
        if (a == null)
        {
            Console.WriteLine(""FAIL"");
        }
        else
        {
            Console.WriteLine(""PASS"");
        }
    }
}
";
            var verifier = CompileAndVerify(source, expectedOutput: @"PASS");
        }

2299
        [WorkItem(543577, "DevDiv")]
P
Pilchie 已提交
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
        [Fact()]
        public void IsOperatorAlwaysFalseInLambda()
        {
            var source = @"
using System;

class C
{
    static int counter = 0;
    public static void Increment()
    {
        counter++;
    }

    public static void Main()
    {
        Func<bool> testExpr = () => Increment() is object; // warning CS0184
        Console.WriteLine(counter);
    }
}
";
            var verifier = CompileAndVerify(source, expectedOutput: @"0");
        }

2324
        [WorkItem(543446, "DevDiv"), WorkItem(543446, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void ThrowExceptionByConversion()
        {
            var source = @"using System;

namespace Test
{
    class DException : Exception
    {
        public static implicit operator DException(Action d)
        {
            return new DException();
        }
    }

    class Program
    {
        static void M()  {    }

        static void Main()
        {
            try
            {
                throw (DException) M;
            }
            catch (DException)
            {
                Console.Write(0);
            }
            Console.Write(1);
        }
    }
}
";
            var verifier = CompileAndVerify(source, expectedOutput: @"01");
        }

2362
        [WorkItem(543586, "DevDiv")]
P
Pilchie 已提交
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
        [Fact]
        public void ImplicitVsExplicitOverloadOperators()
        {
            var source = @"using System;
class Test
{
    static void Main()
    {
        Str str = (Str)1;
        Console.WriteLine(str.num);
    }
}

struct Str
{
    public int num;
    public static explicit operator Str(int i)
    {
        Str temp;
        temp.num = 10;
        return temp;
    }

    public static implicit operator Str(double i)
    {
        Str temp;
        temp.num = 100;
        return temp;
    }
}
";
            var verifier = CompileAndVerify(source, expectedOutput: @"10");
        }

2397 2398
        [WorkItem(543602, "DevDiv")]
        [WorkItem(543660, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void SwitchExpressionWithImplicitConversion()
        {
            var source = @"using System;
public class Test
{
    public static implicit operator int(Test val)
    {
        return 1;
    }

    public static implicit operator float(Test val)
    {
        return 2.1f;
    }

    public static int Main()
    {
        Test t = new Test();
        switch (t)
        {
            case 1:
                Console.WriteLine(0);
                return 0;
            default:
                Console.WriteLine(1);
                return 1;
        }
    }
}
";
            var verifier = CompileAndVerify(source, expectedOutput: @"0");
        }

2433
        [WorkItem(543498, "DevDiv")]
P
Pilchie 已提交
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 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 2581 2582 2583 2584 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
        [Fact]
        public void UserDefinedConversionAfterUserDefinedIncrement()
        {
            var source =
@"using System;

class A
{
    public static C operator ++(A x)
    {
        Console.Write('3');
        return new C();
    }
}

class C : A
{
    public static implicit operator B(C x)
    {
        Console.Write('4');
        return new B();
    }
}

class B : A
{
    static void Main()
    {
        Console.Write('1');
        B b = new B();
        Console.Write('2');
        b++;
        Console.Write('5');
    }
}";
            CompileAndVerify(source, expectedOutput: "12345");
        }

        [Fact]
        public void TestXor()
        {
            var source = @"
using System;

class Program
{
    static bool t() { return true; }
    static bool f() { return false; }
    static void write(bool b) { Console.WriteLine(b); }

    static void Main(string[] args)
    {
        write(t() ^ t());
        write(t() ^ f());
        write(f() ^ t());
        write(f() ^ f());
        Console.WriteLine(""---"");
        write(!(t() ^ t()));
        write(!(t() ^ f()));
        write(!(f() ^ t()));
        write(!(f() ^ f()));
        Console.WriteLine(""---"");
        write((t() ^ t()) || (t() ^ f()));
        write((t() ^ f()) || (t() ^ t()));
        write((f() ^ t()) || (f() ^ f()));
        write((f() ^ f()) || (t() ^ t()));
        Console.WriteLine(""---"");
        write((t() ^ t()) && (t() ^ f()));
        write((t() ^ f()) && (t() ^ t()));
        write((f() ^ t()) && (f() ^ f()));
        write((f() ^ f()) && (f() ^ t()));
        Console.WriteLine(""---"");
        write((t() ^ t()) || !(t() ^ f()));
        write((t() ^ f()) || !(t() ^ t()));
        write(!(f() ^ t()) || (f() ^ f()));
        write(!(f() ^ f()) || (f() ^ t()));
        Console.WriteLine(""---"");
        write((t() ^ t()) && !(t() ^ f()));
        write((t() ^ f()) && !(t() ^ t()));
        write(!(f() ^ t()) && (f() ^ f()));
        write(!(f() ^ f()) && (f() ^ t()));
    }
}
";
            string expectedOutput =
@"False
True
True
False
---
True
False
False
True
---
True
True
True
False
---
False
False
False
False
---
False
True
False
True
---
False
True
False
True
";
            var compilation = CompileAndVerify(source, expectedOutput: expectedOutput);
        }

        [Fact]
        public void TestXorInIf()
        {
            var source = @"
using System;

class Program
{
    static bool t() { return true; }
    static bool f() { return false; }
    static void Main(string[] args)
    {
        if (t() ^ t())
        {
            Console.WriteLine(""1"");
        }
        if (!(t() ^ f()))
        {
            Console.WriteLine(""2"");
        }
        if (f() ^ t())
        {
            Console.WriteLine(""3"");
        }
        if (f() ^ f())
        {
            Console.WriteLine(""4"");
        }
        if ((t() ^ f()) && (f() ^ t()))
        {
            Console.WriteLine(""5"");
        }
        if ((t() ^ t()) || (t() ^ f()))
        {
            Console.WriteLine(""6"");
        }
        if ((t() ^ t()) || (f() ^ f()))
        {
            Console.WriteLine(""7"");
        }
    }
}
";
            string expectedOutput =
@"3
5
6";
            var compilation = CompileAndVerify(source, expectedOutput: expectedOutput);
        }

        [Fact]
        public void XorIl()
        {
            var text = @"using System;
class MyClass
{
    public static bool f = false, t = true;
    public static bool r;
    public static void Main()
    {
        r = f ^ t;
        r = !(f ^ t);
    }
}
";

            var comp = CompileAndVerify(text, expectedOutput: "");

            comp.VerifyIL("MyClass.Main", @"
{
  // Code size       34 (0x22)
  .maxstack  2
  IL_0000:  ldsfld     ""bool MyClass.f""
  IL_0005:  ldsfld     ""bool MyClass.t""
  IL_000a:  xor
  IL_000b:  stsfld     ""bool MyClass.r""
  IL_0010:  ldsfld     ""bool MyClass.f""
  IL_0015:  ldsfld     ""bool MyClass.t""
  IL_001a:  ceq
  IL_001c:  stsfld     ""bool MyClass.r""
  IL_0021:  ret
}");
        }

2636
        [Fact, WorkItem(543446, "DevDiv")]
P
Pilchie 已提交
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 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
        public void UserDefinedConversionAfterUserDefinedConvert()
        {
            var source =
@"using System;
delegate void D(int p1);
class DException : Exception
{
    public D d;
    public static implicit operator DException(D d)
    {
        DException e = new DException();
        e.d = d;
        return e;
    }
}
class Program
{
    static void PM(int p1)
    {
    }
    static void Main()
    {
        throw (DException)PM;
    }
}
";
            CompileAndVerify(source);
        }

        [Fact]
        public void UserDefinedOperatorAfterUserDefinedConversion()
        {
            var source =
@"using System;

class Program
{
    public static void Main(string[] args)
    {
        var c = new C();
        var trash = c + c; // which +?
    }
}

class C
{
    public static string operator +(C c, string s)
    {
        Console.WriteLine(""+(C,string)"");
        return ""+s"";
    }
    public static string operator +(C c, object o)
    {
        Console.WriteLine(""+(C,object)"");
        return ""+o"";
    }
    public static implicit operator string(C c)
    {
        Console.WriteLine(""C->string"");
        return ""C->string"";
    }
    public override string ToString()
    {
        Console.WriteLine(""C.ToString()"");
        return ""C2"";
    }
}";
            CompileAndVerify(source, expectedOutput:
@"C->string
+(C,string)
");
        }

2710
        [Fact, WorkItem(529248, "DevDiv")]
P
Pilchie 已提交
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
        public void TestNullCoalescingOperatorWithNullableConversions()
        {
            // Native compiler violates the language specification while binding the type for the null coalescing operator (??).
            // The last bullet in section 7.13 of the specification for binding the type of ?? operator states that:

            // SPEC:    Otherwise, if b has a type B and an implicit conversion exists from a to B, the result type is B.
            // SPEC:    At run-time, a is first evaluated. If a is not null, a is unwrapped to type A0 (if A exists and is nullable)
            // SPEC:    and converted to type B, and this becomes the result. Otherwise, b is evaluated and becomes the result.

            // Note that for this test there is no implicit conversion from 's' -> int (SnapshotPoint? -> int), but there is an implicit conversion
            // from stripped type SnapshotPoint -> int.

            // Native compiler instead implements this part based on whether A is a nullable type or not. We maintain comptability with the native compiler:

            // SPEC PROPOSAL:    Otherwise, if A exists and is a nullable type and if b has a type B and an implicit  conversion exists from A0 to B,
            // SPEC PROPOSAL:    the result type is B. At run-time, a is first evaluated. If a is not null, a is unwrapped to type A0 and converted to type B,
            // SPEC PROPOSAL:    and this becomes the result. Otherwise, b is evaluated and becomes the result.
            //
            // SPEC PROPOSAL:    Otherwise, if A does not exist or is a non-nullable type and if b has a type B and an implicit conversion exists from a to B,
            // SPEC PROPOSAL:    the result type is B. At run-time, a is first evaluated. If a is not null, a is converted to type B, and this becomes the result.
            // SPEC PROPOSAL:    Otherwise, b is evaluated and becomes the result.


            string source = @"
struct SnapshotPoint
{
    public static implicit operator int(SnapshotPoint snapshotPoint)
    {
        System.Console.WriteLine(""Pass"");
        return 0;
    }
}
class Program
{
    static void Main(string[] args)
    {
       SnapshotPoint? s = new SnapshotPoint();
       var r = s ?? -1;
       SnapshotPoint? s2 = null;
       r = s2 ?? -1;
    }
}
";
2754
            var verifier = CompileAndVerify(source: source, emitters: TestEmitters.CCI, expectedOutput: "Pass");
P
Pilchie 已提交
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

            verifier.VerifyIL("Program.Main", @"
{
  // Code size       70 (0x46)
  .maxstack  1
  .locals init (SnapshotPoint V_0,
  SnapshotPoint? V_1)
  IL_0000:  ldloca.s   V_0
  IL_0002:  initobj    ""SnapshotPoint""
  IL_0008:  ldloc.0
  IL_0009:  newobj     ""SnapshotPoint?..ctor(SnapshotPoint)""
  IL_000e:  stloc.1
  IL_000f:  ldloca.s   V_1
  IL_0011:  call       ""bool SnapshotPoint?.HasValue.get""
  IL_0016:  brfalse.s  IL_0025
  IL_0018:  ldloca.s   V_1
  IL_001a:  call       ""SnapshotPoint SnapshotPoint?.GetValueOrDefault()""
  IL_001f:  call       ""int SnapshotPoint.op_Implicit(SnapshotPoint)""
  IL_0024:  pop
  IL_0025:  ldloca.s   V_1
  IL_0027:  initobj    ""SnapshotPoint?""
  IL_002d:  ldloc.1
  IL_002e:  stloc.1
  IL_002f:  ldloca.s   V_1
  IL_0031:  call       ""bool SnapshotPoint?.HasValue.get""
  IL_0036:  brfalse.s  IL_0045
  IL_0038:  ldloca.s   V_1
  IL_003a:  call       ""SnapshotPoint SnapshotPoint?.GetValueOrDefault()""
  IL_003f:  call       ""int SnapshotPoint.op_Implicit(SnapshotPoint)""
  IL_0044:  pop
  IL_0045:  ret
}");
        }

2789
        [Fact, WorkItem(543980, "DevDiv")]
P
Pilchie 已提交
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 2816 2817 2818 2819
        public void IsOperatorOnEnumAndTypeParameterConstraintToStruct()
        {
            string source = @"using System;

public enum E {  One }

class Gen<T> where T : struct
{
    public static void TestIsOperatorEnum(T t)
    {
        Console.WriteLine(t is Enum);
        Console.WriteLine(t is E);
        Console.WriteLine(t as Enum);
    }
}

public class Test
{
    public static void Main()
    {
        Gen<E>.TestIsOperatorEnum(new E());
    }
}
";
            string expectedOutput = @"True
True
One";
            CompileAndVerify(source, expectedOutput: expectedOutput);
        }

2820
        [Fact, WorkItem(543982, "DevDiv")]
P
Pilchie 已提交
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
        public void OverloadAdditionOperatorOnGenericClass()
        {
            var source = @"
using System;

public class G<T>
{
    public static G<T> operator ~(G<T> g)
    {
        Console.WriteLine(""G<{0}> unary negation"", typeof(T));
        return new G<T>();
    }
    public static G<T> operator +(G<T> G1, G<T> G2)
    {
        Console.WriteLine(""G<{0}> binary addition"", typeof(T));
        return new G<T>();
    }
}

public class Gen<T, U> where T : G<U>
{
    public static void TestLookupOnT(T obj, U val)
    {
        G<U> t = obj + ~obj;
    }
}

public class Test
{
    public static void Main()
    {
        Gen<G<int>, int>.TestLookupOnT(new G<int>(), 1);
    }
}
";
            string expected = @"G<System.Int32> unary negation
G<System.Int32> binary addition";

            CompileAndVerify(
                source: source,
                expectedOutput: expected);
        }

2864
        [Fact, WorkItem(544539, "DevDiv"), WorkItem(544540, "DevDiv")]
P
Pilchie 已提交
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 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 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
        public void NonShortCircuitBoolean()
        {
            var source = @"using System;
struct Program
{
    public static bool P()
    {
        Console.WriteLine(""P"");
        return true;
    }
    public static void Main(string[] args)
    {
        bool x = true | P();
        Console.WriteLine(P() & false);
    }
}";
            string expected = @"P
P
False";

            CompileAndVerify(
                source: source,
                expectedOutput: expected);
        }

        [Fact]
        public void EqualZero()
        {
            var text = @"
using System;
class MyClass
{
    public enum E1
    {
        A,
        B
    }

    public static void Main()
    {
        Test1((object)null, 0);
    }

    public static void Test1<T>(T x, E1 e) where T : class
    {
        if (x == null)
        {
            Console.WriteLine(!(x == null));
        }
        
        if (e == E1.A)
        {
            Console.WriteLine(!(e == E1.A));
        }
    }
}
";

            var comp = CompileAndVerify(text, expectedOutput: @"False
False
");

            comp.VerifyIL("MyClass.Test1<T>", @"
{
  // Code size       35 (0x23)
  .maxstack  2
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  brtrue.s   IL_0016
  IL_0008:  ldarg.0
  IL_0009:  box        ""T""
  IL_000e:  ldnull
  IL_000f:  cgt.un
  IL_0011:  call       ""void System.Console.WriteLine(bool)""
  IL_0016:  ldarg.1
  IL_0017:  brtrue.s   IL_0022
  IL_0019:  ldarg.1
  IL_001a:  ldc.i4.0
  IL_001b:  cgt.un
  IL_001d:  call       ""void System.Console.WriteLine(bool)""
  IL_0022:  ret
}
");
        }

        [Fact]
2951
        public void EqualZeroUnoptimized()
P
Pilchie 已提交
2952 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
        {
            var text = @"
using System;
class MyClass
{
    public enum E1
    {
        A,
        B
    }

    public static void Main()
    {
        Test1((object)null, 0);
    }

    public static void Test1<T>(T x, E1 e) where T : class
    {
        if (x == null)
        {
            Console.WriteLine(x == null);
        }
        
        if (e == E1.A)
        {
            Console.WriteLine(e == E1.A);
        }
    }
}
";

2983
            var comp = CompileAndVerify(text, options: TestOptions.DebugExe, expectedOutput: @"True
P
Pilchie 已提交
2984 2985 2986 2987 2988 2989 2990
True
");

            comp.VerifyIL("MyClass.Test1<T>", @"
{
  // Code size       52 (0x34)
  .maxstack  2
2991 2992 2993 2994
  .locals init (bool V_0,
                bool V_1)
 -IL_0000:  nop
 -IL_0001:  ldarg.0
P
Pilchie 已提交
2995
  IL_0002:  box        ""T""
2996 2997 2998 2999
  IL_0007:  ldnull
  IL_0008:  ceq
  IL_000a:  stloc.0
 ~IL_000b:  ldloc.0
3000
  IL_000c:  brfalse.s  IL_001f
3001 3002
 -IL_000e:  nop
 -IL_000f:  ldarg.0
P
Pilchie 已提交
3003
  IL_0010:  box        ""T""
3004 3005
  IL_0015:  ldnull
  IL_0016:  ceq
P
Pilchie 已提交
3006
  IL_0018:  call       ""void System.Console.WriteLine(bool)""
3007 3008 3009 3010 3011 3012 3013
  IL_001d:  nop
 -IL_001e:  nop
 -IL_001f:  ldarg.1
  IL_0020:  ldc.i4.0
  IL_0021:  ceq
  IL_0023:  stloc.1
 ~IL_0024:  ldloc.1
3014
  IL_0025:  brfalse.s  IL_0033
3015 3016 3017 3018
 -IL_0027:  nop
 -IL_0028:  ldarg.1
  IL_0029:  ldc.i4.0
  IL_002a:  ceq
P
Pilchie 已提交
3019
  IL_002c:  call       ""void System.Console.WriteLine(bool)""
3020 3021 3022
  IL_0031:  nop
 -IL_0032:  nop
 -IL_0033:  ret
P
Pilchie 已提交
3023
}
3024
", sequencePoints: "MyClass.Test1");
P
Pilchie 已提交
3025 3026
        }

3027
        [WorkItem(543893, "DevDiv")]
P
Pilchie 已提交
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 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 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
        [Fact]
        public void EnumBitwiseComplement()
        {
            var text = @"
public class A
{
    enum E : ushort { one = 1, two = 2, four = 4 }
    public static void Main()
    {
checked {
        E e = E.one;
        e &= ~E.two;
        System.Console.WriteLine(e);
}
    }
}
";

            var comp = CompileAndVerify(text, expectedOutput: @"one");

            // Can't actually see an unchecked cast here since only constant values are emitted.
            comp.VerifyIL("A.Main", @"
{
  // Code size       18 (0x12)
  .maxstack  2
  IL_0000:  ldc.i4.1
  IL_0001:  ldc.i4     0xfffd
  IL_0006:  and
  IL_0007:  box        ""A.E""
  IL_000c:  call       ""void System.Console.WriteLine(object)""
  IL_0011:  ret
}
");
            text = @"
public class A
{
    enum E : ushort { one = 1, two = 2, four = 4 }
    public static void Main()
    {
        checked {
            E e = E.one;
            int i = 5 + (int)~e;
            System.Console.WriteLine(i);
        }
    }
}
";

            comp = CompileAndVerify(text, expectedOutput: @"65539");

            // Can't actually see an unchecked cast here since only constant values are emitted.
            comp.VerifyIL("A.Main", @"
{ 
  // Code size       13 (0xd)
  .maxstack  2
  .locals init (A.E V_0) //e
  IL_0000:  ldc.i4.1
  IL_0001:  stloc.0
  IL_0002:  ldc.i4.5
  IL_0003:  ldloc.0
  IL_0004:  not
  IL_0005:  conv.u2
  IL_0006:  add.ovf
  IL_0007:  call       ""void System.Console.WriteLine(int)""
  IL_000c:  ret
}
");
        }

        [Fact]
        public void EnumXor()
        {
            var text = @"
using System;

enum e : sbyte
{
    x = sbyte.MinValue,
    y = sbyte.MaxValue,
    z = 1
}

enum e1 : byte
{
    x = byte.MinValue,
    y = byte.MaxValue,
    z = 1
}

public static class Test
{
    public static void Main()
    {
        TestE();
        TestE1();
    }

    private static void TestE()
    {
        var x = e.x;
        var y = e.y;

        var z = x ^ y;
        System.Console.WriteLine((int)z);

        x ^= e.z;
        y ^= unchecked((e)(-1));
        x ^= e.z;
        y ^= unchecked((e)(-1));

        z = x ^ y;

        System.Console.WriteLine((int)z);
    }

    private static void TestE1()
    {
        var x = e1.x;
        var y = e1.y;

        var z = x ^ y;
        System.Console.WriteLine((int)z);

        x ^= e1.z;
        y ^= unchecked((e1)(-1));
        x ^= e1.z;
        y ^= unchecked((e1)(-1));

        z = x ^ y;

        System.Console.WriteLine((int)z);
    }
}

";

3164
            var comp = CompileAndVerify(text, expectedOutput: @"
3165
-1
P
Pilchie 已提交
3166 3167 3168 3169 3170 3171 3172
-1
255
255
");

            comp.VerifyIL("Test.TestE()", @"
{
3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
  // Code size       33 (0x21)
  .maxstack  3
  .locals init (e V_0) //y
  IL_0000:  ldc.i4.s   -128
  IL_0002:  ldc.i4.s   127
  IL_0004:  stloc.0
  IL_0005:  dup
  IL_0006:  ldloc.0
  IL_0007:  xor
  IL_0008:  call       ""void System.Console.WriteLine(int)""
  IL_000d:  ldc.i4.1
  IL_000e:  xor
  IL_000f:  ldloc.0
  IL_0010:  ldc.i4.m1
  IL_0011:  xor
  IL_0012:  stloc.0
P
Pilchie 已提交
3189 3190
  IL_0013:  ldc.i4.1
  IL_0014:  xor
3191 3192 3193 3194 3195 3196 3197 3198
  IL_0015:  ldloc.0
  IL_0016:  ldc.i4.m1
  IL_0017:  xor
  IL_0018:  stloc.0
  IL_0019:  ldloc.0
  IL_001a:  xor
  IL_001b:  call       ""void System.Console.WriteLine(int)""
  IL_0020:  ret
P
Pilchie 已提交
3199 3200 3201 3202 3203
}
");

            comp.VerifyIL("Test.TestE1()", @"
{
3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229
  // Code size       43 (0x2b)
  .maxstack  3
  .locals init (e1 V_0) //y
  IL_0000:  ldc.i4.0
  IL_0001:  ldc.i4     0xff
  IL_0006:  stloc.0
  IL_0007:  dup
  IL_0008:  ldloc.0
  IL_0009:  xor
  IL_000a:  call       ""void System.Console.WriteLine(int)""
  IL_000f:  ldc.i4.1
  IL_0010:  xor
  IL_0011:  ldloc.0
  IL_0012:  ldc.i4     0xff
  IL_0017:  xor
  IL_0018:  stloc.0
  IL_0019:  ldc.i4.1
  IL_001a:  xor
  IL_001b:  ldloc.0
  IL_001c:  ldc.i4     0xff
  IL_0021:  xor
  IL_0022:  stloc.0
  IL_0023:  ldloc.0
  IL_0024:  xor
  IL_0025:  call       ""void System.Console.WriteLine(int)""
  IL_002a:  ret
P
Pilchie 已提交
3230 3231 3232 3233
}
");
        }

3234
        [WorkItem(544452, "DevDiv")]
P
Pilchie 已提交
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 3299 3300
        [Fact]
        public void LiftedByteEnumAddition()
        {
            var text =
@"using System;
public class Program
{
    private static bool ThrowsException(Action action)
    {
        try 
        {  
            action(); 
            return false;
        } 
        catch(Exception)
        {
            return true;
        }
    }
    private static void Test(bool b)
    {
        Console.Write(b ? 't' : 'f');
    }

    enum Color : byte { Red, Green, Blue }
    static void Main()
    {
        Color? c = Color.Blue;
        byte? b = byte.MaxValue - 1;
        Color? r = 0;
        Test(ThrowsException(()=>{ r = checked(c + b); }));
        Test(ThrowsException(()=>{ r = checked(c.Value + b.Value); }));
        Test(ThrowsException(()=>{ r = unchecked(c + b); }));
        Test(ThrowsException(()=>{ r = unchecked(c.Value + b.Value); }));
    }

    static void M(Color c, byte b)
    {
        Console.WriteLine(checked(c + b));
        Console.WriteLine(unchecked(c + b));
    }
}";

            string il = @"{
  // Code size       29 (0x1d)
  .maxstack  2
  IL_0000:  ldarg.0
  IL_0001:  ldarg.1
  IL_0002:  add.ovf
  IL_0003:  conv.ovf.u1
  IL_0004:  box        ""Program.Color""
  IL_0009:  call       ""void System.Console.WriteLine(object)""
  IL_000e:  ldarg.0
  IL_000f:  ldarg.1
  IL_0010:  add
  IL_0011:  conv.u1
  IL_0012:  box        ""Program.Color""
  IL_0017:  call       ""void System.Console.WriteLine(object)""
  IL_001c:  ret
}";

            var comp = CompileAndVerify(text, expectedOutput: @"ttff");

            comp.VerifyIL("Program.M", il);
        }

3301
        [WorkItem(544943, "DevDiv")]
P
Pilchie 已提交
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
        [Fact]
        public void OptimizedXor()
        {
            var text = @"
using System;

class C
{
    void M(bool b)
    {
        Console.WriteLine(b ^ true);
        Console.WriteLine(b ^ false);
        Console.WriteLine(true ^ b);
        Console.WriteLine(false ^ b);
    }
}";

            //NOTE: all xors optimized away
            var comp = CompileAndVerify(text).VerifyIL("C.M", @"
{
  // Code size       31 (0x1f)
  .maxstack  2
  IL_0000:  ldarg.1
  IL_0001:  ldc.i4.0
  IL_0002:  ceq
  IL_0004:  call       ""void System.Console.WriteLine(bool)""
  IL_0009:  ldarg.1
  IL_000a:  call       ""void System.Console.WriteLine(bool)""
  IL_000f:  ldarg.1
  IL_0010:  ldc.i4.0
  IL_0011:  ceq
  IL_0013:  call       ""void System.Console.WriteLine(bool)""
  IL_0018:  ldarg.1
  IL_0019:  call       ""void System.Console.WriteLine(bool)""
  IL_001e:  ret
}");
        }

3340
        [WorkItem(544943, "DevDiv")]
P
Pilchie 已提交
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
        [Fact]
        public void XorMalformedTrue()
        {
            var text = @"
using System;
 
class C
{
    static void Main()
    {
        byte[] x = { 0xFF };
        bool[] y = { true };
        Buffer.BlockCopy(x, 0, y, 0, 1);
 
        Console.WriteLine(y[0]);
        Console.WriteLine(y[0] ^ true);
    }
}";

            var comp = CompileAndVerify(text, expectedOutput: @"True
False");
        }

3364
        [WorkItem(539398, "DevDiv")]
3365
        [WorkItem(1043494, "DevDiv")]
3366
        [Fact(Skip = "1043494")]
P
Pilchie 已提交
3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
        public void TestFloatNegativeZero()
        {
            var text = @"
using System;
 
class C
{
    static void Main()
    {
        Console.WriteLine(+0f == -0f);
        Console.WriteLine(1f / 0f);
        Console.WriteLine(1f / -0f);
        Console.WriteLine(-1f / 0f);
        Console.WriteLine(-1f / -0f);
        Console.WriteLine(1f / (1f * 0f));
        Console.WriteLine(1f / (1f * -0f));
        Console.WriteLine(1f / (-1f * 0f));
        Console.WriteLine(1f / (-1f * -0f));
    }
}";

            var comp = CompileAndVerify(text, expectedOutput: @"
True
Infinity
-Infinity
-Infinity
Infinity
Infinity
-Infinity
-Infinity
Infinity");
        }

3400
        [WorkItem(539398, "DevDiv")]
3401 3402
        [WorkItem(1043494, "DevDiv")]
        [Fact(Skip = "1043494")]
P
Pilchie 已提交
3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
        public void TestDoubleNegativeZero()
        {
            var text = @"
using System;
 
class C
{
    static void Main()
    {
        Console.WriteLine(+0d == -0d);
        Console.WriteLine(1d / 0d);
        Console.WriteLine(1d / -0d);
        Console.WriteLine(-1d / 0d);
        Console.WriteLine(-1d / -0d);
        Console.WriteLine(1d / (1d * 0d));
        Console.WriteLine(1d / (1d * -0d));
        Console.WriteLine(1d / (-1d * 0d));
        Console.WriteLine(1d / (-1d * -0d));
    }
}";

            var comp = CompileAndVerify(text, expectedOutput: @"
True
Infinity
-Infinity
-Infinity
Infinity
Infinity
-Infinity
-Infinity
Infinity");
        }

        // NOTE: decimal doesn't have infinity, so we convert to double.
3437
        [WorkItem(539398, "DevDiv")]
3438 3439
        [WorkItem(1043494, "DevDiv")]
        [Fact(Skip = "1043494")]
P
Pilchie 已提交
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
        public void TestDecimalNegativeZero()
        {
            var text = @"
using System;
 
class C
{
    static void Main()
    {
        Console.WriteLine(+0m == -0m);
        Console.WriteLine(1d / (double)(0m));
        Console.WriteLine(1d / (double)(-0m));
        Console.WriteLine(-1d / (double)(0m));
        Console.WriteLine(-1d / (double)(-0m));
        Console.WriteLine(1d / (double)(1m * 0m));
        Console.WriteLine(1d / (double)(1m * -0m));
        Console.WriteLine(1d / (double)(-1m * 0m));
        Console.WriteLine(1d / (double)(-1m * -0m));
    }
}";

            var comp = CompileAndVerify(text, expectedOutput: @"
True
Infinity
-Infinity
-Infinity
Infinity
Infinity
-Infinity
-Infinity
Infinity");
        }

3473
        [WorkItem(545239, "DevDiv")]
P
Pilchie 已提交
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 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
        [Fact()]
        public void IncrementPropertyOfTypeParameterReturnValue()
        {
            var text = @"
public interface I
{
    int IntPropI { get; set; }
}

struct S1 : I
{
    public int x;

    public int IntPropI
    {
        get
        {
            x ++;
            return x;
        }
        set
        {
            x ++;
            System.Console.WriteLine(x);
        }
    }
}

public class Test
{

    public static void Main()
    {
        S1 s = new S1();
        TestINop(s);
    }

    public static T Nop<T>(T t) { return t; }
    public static void TestINop<T>(T t) where T : I
    {
        Nop(t).IntPropI++;
        Nop(t).IntPropI++;
    }
}
";

            CompileAndVerify(text, expectedOutput: @"
2
2").VerifyIL("Test.TestINop<T>", @"
{
  // Code size       73 (0x49)
  .maxstack  3
  .locals init (int V_0,
  T V_1,
  T V_2)
  IL_0000:  ldarg.0
  IL_0001:  call       ""T Test.Nop<T>(T)""
  IL_0006:  stloc.1
  IL_0007:  ldloca.s   V_1
  IL_0009:  dup
  IL_000a:  constrained. ""T""
  IL_0010:  callvirt   ""int I.IntPropI.get""
  IL_0015:  stloc.0
  IL_0016:  ldloc.0
  IL_0017:  ldc.i4.1
  IL_0018:  add
  IL_0019:  constrained. ""T""
  IL_001f:  callvirt   ""void I.IntPropI.set""
  IL_0024:  ldarg.0
  IL_0025:  call       ""T Test.Nop<T>(T)""
  IL_002a:  stloc.2
  IL_002b:  ldloca.s   V_2
  IL_002d:  dup
  IL_002e:  constrained. ""T""
  IL_0034:  callvirt   ""int I.IntPropI.get""
  IL_0039:  stloc.0
  IL_003a:  ldloc.0
  IL_003b:  ldc.i4.1
  IL_003c:  add
  IL_003d:  constrained. ""T""
  IL_0043:  callvirt   ""void I.IntPropI.set""
  IL_0048:  ret
}
");
        }

3560
        [WorkItem(546750, "DevDiv")]
P
Pilchie 已提交
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 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 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018
        [Fact()]
        public void IncrementStructFieldWithReceiverThis()
        {
            var text = @"
struct S
{
    int x;

    void Test()
    {
        x++;
    }
}
";

            // NOTE: don't need a ref local in this case.
            CompileAndVerify(text).VerifyIL("S.Test", @"
{
  // Code size       15 (0xf)
  .maxstack  3
  IL_0000:  ldarg.0
  IL_0001:  ldarg.0
  IL_0002:  ldfld      ""int S.x""
  IL_0007:  ldc.i4.1
  IL_0008:  add
  IL_0009:  stfld      ""int S.x""
  IL_000e:  ret
}
");
        }



        [Fact]
        public void TestTernary_InterfaceRegression1a()
        {
            var source = @"
using System.Collections.Generic;

public class Test
{
    private static bool C() { return true;}

    static void Main()
    {
        int[] a = new int[] { };
        IEnumerable<int> b = new List<int>();

        IEnumerable<int> c = C()? b : a;
        Foo(c);
    }

    static void Foo<T>(T x)
    {
        System.Console.WriteLine(typeof(T));
    }
}";

            var comp = CompileAndVerify(source, expectedOutput: "System.Collections.Generic.IEnumerable`1[System.Int32]");
            comp.VerifyDiagnostics();
            comp.VerifyIL("Test.Main", @"
{
  // Code size       32 (0x20)
  .maxstack  1
  .locals init (int[] V_0, //a
  System.Collections.Generic.IEnumerable<int> V_1, //b
  System.Collections.Generic.IEnumerable<int> V_2)
  IL_0000:  ldc.i4.0
  IL_0001:  newarr     ""int""
  IL_0006:  stloc.0
  IL_0007:  newobj     ""System.Collections.Generic.List<int>..ctor()""
  IL_000c:  stloc.1
  IL_000d:  call       ""bool Test.C()""
  IL_0012:  brtrue.s   IL_0019
  IL_0014:  ldloc.0
  IL_0015:  stloc.2
  IL_0016:  ldloc.2
  IL_0017:  br.s       IL_001a
  IL_0019:  ldloc.1
  IL_001a:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>)""
  IL_001f:  ret
}");
        }

        [Fact]
        public void TestTernary_InterfaceRegression1b()
        {
            var source = @"
using System.Collections.Generic;

public class Test
{
    private static bool C() { return true;}

    static void Main()
    {
        int[] a = new int[] { };
        IEnumerable<int> b = null;

        IEnumerable<int> c = C()? (b = (IEnumerable<int>)new List<int>()) : a;
        Foo(c);
        Foo(b);
    }

    static void Foo<T>(T x)
    {
        System.Console.Write(typeof(T));
    }
}";

            var comp = CompileAndVerify(source, expectedOutput: "System.Collections.Generic.IEnumerable`1[System.Int32]System.Collections.Generic.IEnumerable`1[System.Int32]");
            comp.VerifyDiagnostics();
            comp.VerifyIL("Test.Main", @"
{
  // Code size       42 (0x2a)
  .maxstack  2
  .locals init (int[] V_0, //a
  System.Collections.Generic.IEnumerable<int> V_1, //b
  System.Collections.Generic.IEnumerable<int> V_2)
  IL_0000:  ldc.i4.0
  IL_0001:  newarr     ""int""
  IL_0006:  stloc.0
  IL_0007:  ldnull
  IL_0008:  stloc.1
  IL_0009:  call       ""bool Test.C()""
  IL_000e:  brtrue.s   IL_0015
  IL_0010:  ldloc.0
  IL_0011:  stloc.2
  IL_0012:  ldloc.2
  IL_0013:  br.s       IL_001e
  IL_0015:  newobj     ""System.Collections.Generic.List<int>..ctor()""
  IL_001a:  dup
  IL_001b:  stloc.1
  IL_001c:  stloc.2
  IL_001d:  ldloc.2
  IL_001e:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>)""
  IL_0023:  ldloc.1
  IL_0024:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>)""
  IL_0029:  ret
}");
        }

        [Fact]
        public void TestTernary_InterfaceRegression1c()
        {
            var source = @"
using System.Collections.Generic;

public class Test
{
    static void Main()
    {
        int[] a = new int[] { };
        IEnumerable<int> b = new List<int>();

        Foo(b, b != null ? b : a);
    }

    static void Foo<T, U>(T x, U y)
    {
        System.Console.Write(typeof(T));
    }
}";

            var comp = CompileAndVerify(source, expectedOutput: "System.Collections.Generic.IEnumerable`1[System.Int32]");
            comp.VerifyDiagnostics();
            comp.VerifyIL("Test.Main", @"
{
  // Code size       29 (0x1d)
  .maxstack  2
  .locals init (int[] V_0, //a
  System.Collections.Generic.IEnumerable<int> V_1, //b
  System.Collections.Generic.IEnumerable<int> V_2)
  IL_0000:  ldc.i4.0
  IL_0001:  newarr     ""int""
  IL_0006:  stloc.0
  IL_0007:  newobj     ""System.Collections.Generic.List<int>..ctor()""
  IL_000c:  stloc.1
  IL_000d:  ldloc.1
  IL_000e:  dup
  IL_000f:  brtrue.s   IL_0016
  IL_0011:  ldloc.0
  IL_0012:  stloc.2
  IL_0013:  ldloc.2
  IL_0014:  br.s       IL_0017
  IL_0016:  ldloc.1
  IL_0017:  call       ""void Test.Foo<System.Collections.Generic.IEnumerable<int>, System.Collections.Generic.IEnumerable<int>>(System.Collections.Generic.IEnumerable<int>, System.Collections.Generic.IEnumerable<int>)""
  IL_001c:  ret
}");
        }


        [Fact]
        public void TestTernary_InterfaceRegression2()
        {
            var source = @"
public interface IA { }
public interface IB { int f(); }
public class AB1 : IA, IB { public int f() { return 42; } }
public class AB2 : IA, IB { public int f() { return 1; } }

class MainClass
{
    private static bool C() { return true;}

    public static void g(AB1 ab1)
    {
        (C()? (IB)ab1 : (IB)new AB2()).f();
    }
}";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("MainClass.g", @"
{
  // Code size       26 (0x1a)
  .maxstack  1
  .locals init (IB V_0)
  IL_0000:  call       ""bool MainClass.C()""
  IL_0005:  brtrue.s   IL_0010
  IL_0007:  newobj     ""AB2..ctor()""
  IL_000c:  stloc.0
  IL_000d:  ldloc.0
  IL_000e:  br.s       IL_0013
  IL_0010:  ldarg.0
  IL_0011:  stloc.0
  IL_0012:  ldloc.0
  IL_0013:  callvirt   ""int IB.f()""
  IL_0018:  pop
  IL_0019:  ret
}");
        }

        [Fact]
        public void TestTernary_FuncVariance()
        {
            var source = @"
using System;
using System.Collections.Generic;

    class Program
    {
        private static bool C() { return true;}

        static void Main(string[] args)
        {
            Func<Exception[]> f1 = null;

            Func<IEnumerable<object>> f2 = null;

            var oo = C()? f1 : f2;
            Console.WriteLine(oo);

            oo = C()? f2 : f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       41 (0x29)
  .maxstack  1
  .locals init (System.Func<System.Exception[]> V_0, //f1
  System.Func<System.Collections.Generic.IEnumerable<object>> V_1, //f2
  System.Func<System.Collections.Generic.IEnumerable<object>> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  call       ""bool Program.C()""
  IL_0009:  brtrue.s   IL_000e
  IL_000b:  ldloc.1
  IL_000c:  br.s       IL_0011
  IL_000e:  ldloc.0
  IL_000f:  stloc.2
  IL_0010:  ldloc.2
  IL_0011:  call       ""void System.Console.WriteLine(object)""
  IL_0016:  call       ""bool Program.C()""
  IL_001b:  brtrue.s   IL_0022
  IL_001d:  ldloc.0
  IL_001e:  stloc.2
  IL_001f:  ldloc.2
  IL_0020:  br.s       IL_0023
  IL_0022:  ldloc.1
  IL_0023:  call       ""void System.Console.WriteLine(object)""
  IL_0028:  ret
}
");
        }

        [Fact]
        public void TestTernary_FuncVariance01()
        {
            var source = @"
using System;
using System.Collections;
using System.Collections.Generic;

    class Program
    {
        private static bool C() { return true;}

        static void Main(string[] args)
        {
            Func<Exception[]> f1 = null;

            Func<IEnumerable<object>> f2 = null;

            var oo = C()? (Func<IEnumerable>)f1 : (Func<IEnumerable>)f2;
            Console.WriteLine(oo);

            oo = C()? (Func<IEnumerable>)f2 : (Func<IEnumerable>)f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       45 (0x2d)
  .maxstack  1
  .locals init (System.Func<System.Exception[]> V_0, //f1
  System.Func<System.Collections.Generic.IEnumerable<object>> V_1, //f2
  System.Func<System.Collections.IEnumerable> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  call       ""bool Program.C()""
  IL_0009:  brtrue.s   IL_0010
  IL_000b:  ldloc.1
  IL_000c:  stloc.2
  IL_000d:  ldloc.2
  IL_000e:  br.s       IL_0013
  IL_0010:  ldloc.0
  IL_0011:  stloc.2
  IL_0012:  ldloc.2
  IL_0013:  call       ""void System.Console.WriteLine(object)""
  IL_0018:  call       ""bool Program.C()""
  IL_001d:  brtrue.s   IL_0024
  IL_001f:  ldloc.0
  IL_0020:  stloc.2
  IL_0021:  ldloc.2
  IL_0022:  br.s       IL_0027
  IL_0024:  ldloc.1
  IL_0025:  stloc.2
  IL_0026:  ldloc.2
  IL_0027:  call       ""void System.Console.WriteLine(object)""
  IL_002c:  ret
}
");
        }

        [Fact]
        public void TestTernary_InterfaceVariance()
        {
            var source = @"
using System;
using System.Collections.Generic;
using System.Security;

[assembly: SecurityTransparent()]

    class Program
    {
        private static bool C() { return true;}

        interface CoInter<out T>
        {
        }

        static void Main(string[] args)
        {
            CoInter<Exception[]> f1 = null;

            CoInter<IEnumerable<object>> f2 = null;

            var oo = C()? f1 : f2;
            Console.WriteLine(oo);

            oo = C()? f2 : f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       41 (0x29)
  .maxstack  1
  .locals init (Program.CoInter<System.Exception[]> V_0, //f1
  Program.CoInter<System.Collections.Generic.IEnumerable<object>> V_1, //f2
  Program.CoInter<System.Collections.Generic.IEnumerable<object>> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  call       ""bool Program.C()""
  IL_0009:  brtrue.s   IL_000e
  IL_000b:  ldloc.1
  IL_000c:  br.s       IL_0011
  IL_000e:  ldloc.0
  IL_000f:  stloc.2
  IL_0010:  ldloc.2
  IL_0011:  call       ""void System.Console.WriteLine(object)""
  IL_0016:  call       ""bool Program.C()""
  IL_001b:  brtrue.s   IL_0022
  IL_001d:  ldloc.0
  IL_001e:  stloc.2
  IL_001f:  ldloc.2
  IL_0020:  br.s       IL_0023
  IL_0022:  ldloc.1
  IL_0023:  call       ""void System.Console.WriteLine(object)""
  IL_0028:  ret
}
");
        }

        [Fact]
        public void TestTernary_InterfaceVarianceA()
        {
            var source = @"
using System;
using System.Security;

[assembly: SecurityTransparent()]

    class Program
    {
        private static bool C() { return true;}

        interface CoInter<out T>
        {
        }

        static void Main(string[] args)
        {
            CoInter<Exception> f1 = null;

            CoInter<object> f2 = null;

            var oo = C()? f1 : f2;
            Console.WriteLine(oo);

            oo = C()? f2 : f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(new string[] { source }, additionalRefs: new[] { SystemCoreRef }, expectedOutput: @"");
4019
            //            var comp = CompileAndVerify(source);
P
Pilchie 已提交
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       41 (0x29)
  .maxstack  1
  .locals init (Program.CoInter<System.Exception> V_0, //f1
  Program.CoInter<object> V_1, //f2
  Program.CoInter<object> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  call       ""bool Program.C()""
  IL_0009:  brtrue.s   IL_000e
  IL_000b:  ldloc.1
  IL_000c:  br.s       IL_0011
  IL_000e:  ldloc.0
  IL_000f:  stloc.2
  IL_0010:  ldloc.2
  IL_0011:  call       ""void System.Console.WriteLine(object)""
  IL_0016:  call       ""bool Program.C()""
  IL_001b:  brtrue.s   IL_0022
  IL_001d:  ldloc.0
  IL_001e:  stloc.2
  IL_001f:  ldloc.2
  IL_0020:  br.s       IL_0023
  IL_0022:  ldloc.1
  IL_0023:  call       ""void System.Console.WriteLine(object)""
  IL_0028:  ret
}
");
        }

        [Fact]
        public void TestTernary_InterfaceVariance01()
        {
            var source = @"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security;

[assembly: SecurityTransparent()]

    class Program
    {
        private static bool C() { return true;}

        interface CoInter<out T>
        {
        }

        static void Main(string[] args)
        {
            CoInter<Exception[]> f1 = null;

            CoInter<IEnumerable<object>> f2 = null;

            var oo = C()? (CoInter<IEnumerable>)f1 : (CoInter<IEnumerable>)f2;
            Console.WriteLine(oo);

            oo = C()? (CoInter<IEnumerable>)f2 : (CoInter<IEnumerable>)f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       45 (0x2d)
  .maxstack  1
  .locals init (Program.CoInter<System.Exception[]> V_0, //f1
  Program.CoInter<System.Collections.Generic.IEnumerable<object>> V_1, //f2
  Program.CoInter<System.Collections.IEnumerable> V_2)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  call       ""bool Program.C()""
  IL_0009:  brtrue.s   IL_0010
  IL_000b:  ldloc.1
  IL_000c:  stloc.2
  IL_000d:  ldloc.2
  IL_000e:  br.s       IL_0013
  IL_0010:  ldloc.0
  IL_0011:  stloc.2
  IL_0012:  ldloc.2
  IL_0013:  call       ""void System.Console.WriteLine(object)""
  IL_0018:  call       ""bool Program.C()""
  IL_001d:  brtrue.s   IL_0024
  IL_001f:  ldloc.0
  IL_0020:  stloc.2
  IL_0021:  ldloc.2
  IL_0022:  br.s       IL_0027
  IL_0024:  ldloc.1
  IL_0025:  stloc.2
  IL_0026:  ldloc.2
  IL_0027:  call       ""void System.Console.WriteLine(object)""
  IL_002c:  ret
}
");
        }

        [Fact]
        public void TestTernary_ToBase()
        {
            var source = @"
using System;
using System.Collections.Generic;
using System.Security;

[assembly: SecurityTransparent()]

    class Program
    {
        private static bool C() { return true;}

        static void Main(string[] args)
        {
            Exception[] f1 = null;

            IEnumerable<object> f2 = null;

            var oo = C()? (object)f1 : (object)f2;
            Console.WriteLine(oo);

            oo = C()? (object)f2 : (object)f1;
            Console.WriteLine(oo);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       37 (0x25)
  .maxstack  1
  .locals init (System.Exception[] V_0, //f1
  System.Collections.Generic.IEnumerable<object> V_1) //f2
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldnull
  IL_0003:  stloc.1
  IL_0004:  call       ""bool Program.C()""
  IL_0009:  brtrue.s   IL_000e
  IL_000b:  ldloc.1
  IL_000c:  br.s       IL_000f
  IL_000e:  ldloc.0
  IL_000f:  call       ""void System.Console.WriteLine(object)""
  IL_0014:  call       ""bool Program.C()""
  IL_0019:  brtrue.s   IL_001e
  IL_001b:  ldloc.0
  IL_001c:  br.s       IL_001f
  IL_001e:  ldloc.1
  IL_001f:  call       ""void System.Console.WriteLine(object)""
  IL_0024:  ret
}
");
        }

4183
        [WorkItem(634407, "DevDiv")]
P
Pilchie 已提交
4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240
        [Fact]
        public void TestTernary_Null()
        {
            var source = @"
using System;
using System.Collections.Generic;
using System.Security;

[assembly: SecurityTransparent()]

    class Program
    {
        private static bool C() { return true;}

        static void Main(string[] args)
        {
            Exception[] f1 = null;

            var oo = C()? f1 : null as IEnumerable<object>;
            Console.WriteLine(oo);

            var oo1 = C()? null as IEnumerable<object> : f1 as IEnumerable<Exception>;
            Console.WriteLine(oo1);
        }
    }
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       39 (0x27)
  .maxstack  1
  .locals init (System.Exception[] V_0, //f1
  System.Collections.Generic.IEnumerable<object> V_1)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  call       ""bool Program.C()""
  IL_0007:  brtrue.s   IL_000c
  IL_0009:  ldnull
  IL_000a:  br.s       IL_000f
  IL_000c:  ldloc.0
  IL_000d:  stloc.1
  IL_000e:  ldloc.1
  IL_000f:  call       ""void System.Console.WriteLine(object)""
  IL_0014:  call       ""bool Program.C()""
  IL_0019:  brtrue.s   IL_0020
  IL_001b:  ldloc.0
  IL_001c:  stloc.1
  IL_001d:  ldloc.1
  IL_001e:  br.s       IL_0021
  IL_0020:  ldnull
  IL_0021:  call       ""void System.Console.WriteLine(object)""
  IL_0026:  ret
}");
        }

4241
        [WorkItem(634406, "DevDiv")]
P
Pilchie 已提交
4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293
        [Fact]
        public void TestBinary_Implicit()
        {
            var source = @"
using System;
using System.Security;

[assembly: SecurityTransparent()]

class Program
{
    private static bool C() { return true; }

    class cls1
    {
        public static implicit operator int(cls1 from)
        {
            return 42;
        }
    }

    static void Main(string[] args)
    {
        cls1 f1 = null;

        var oo = f1 ?? 33;
        Console.WriteLine(oo);
    }
}
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       21 (0x15)
  .maxstack  1
  .locals init (Program.cls1 V_0)
  IL_0000:  ldnull
  IL_0001:  stloc.0
  IL_0002:  ldloc.0
  IL_0003:  brtrue.s   IL_0009
  IL_0005:  ldc.i4.s   33
  IL_0007:  br.s       IL_000f
  IL_0009:  ldloc.0
  IL_000a:  call       ""int Program.cls1.op_Implicit(Program.cls1)""
  IL_000f:  call       ""void System.Console.WriteLine(int)""
  IL_0014:  ret
}
");
        }

4294
        [WorkItem(656807, "DevDiv")]
P
Pilchie 已提交
4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340
        [Fact]
        public void DelegateEqualsNull()
        {
            var source = @"
public delegate int D(int x);
public class Program
{
    public static D d1 = null;
    public static int r1;
    public static bool r2;
    public static void Main(string[] args)
    {
        if (d1 == null) { r1 = 1; }
        if (d1 != null) { r1 = 2; }
        r2 = (d1 == null);
        r2 = (d1 != null);
    }
}
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       53 (0x35)
  .maxstack  2
  IL_0000:  ldsfld     ""D Program.d1""
  IL_0005:  brtrue.s   IL_000d
  IL_0007:  ldc.i4.1
  IL_0008:  stsfld     ""int Program.r1""
  IL_000d:  ldsfld     ""D Program.d1""
  IL_0012:  brfalse.s  IL_001a
  IL_0014:  ldc.i4.2
  IL_0015:  stsfld     ""int Program.r1""
  IL_001a:  ldsfld     ""D Program.d1""
  IL_001f:  ldnull
  IL_0020:  ceq
  IL_0022:  stsfld     ""bool Program.r2""
  IL_0027:  ldsfld     ""D Program.d1""
  IL_002c:  ldnull
  IL_002d:  cgt.un
  IL_002f:  stsfld     ""bool Program.r2""
  IL_0034:  ret
}");
        }

4341
        [WorkItem(717072, "DevDiv")]
P
Pilchie 已提交
4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433
        [Fact]
        public void DecimalOperators()
        {
            var source = @"
class Program
{
    static void Main()
    {
        decimal d1 = 1.0201m;
        if (d1 == 10201M)
        {
        }

        if (d1 != 10201M)
        {
        }

        decimal d2 = d1 + d1;
        decimal d3 = d1 - d1;
        decimal d4 = d1 * d1;
        decimal d5 = d1 / d1;
        decimal d6 = d1 % d1;
        decimal d7 = d1 ++;
        decimal d8 = d1--;
        decimal d9 = -d1;
        decimal d10 = +d1;
    }
}
";

            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size      116 (0x74)
  .maxstack  6
  .locals init (decimal V_0) //d1
  IL_0000:  ldloca.s   V_0
  IL_0002:  ldc.i4     0x27d9
  IL_0007:  ldc.i4.0
  IL_0008:  ldc.i4.0
  IL_0009:  ldc.i4.0
  IL_000a:  ldc.i4.4
  IL_000b:  call       ""decimal..ctor(int, int, int, bool, byte)""
  IL_0010:  ldloc.0
  IL_0011:  ldc.i4     0x27d9
  IL_0016:  newobj     ""decimal..ctor(int)""
  IL_001b:  call       ""bool decimal.op_Equality(decimal, decimal)""
  IL_0020:  pop
  IL_0021:  ldloc.0
  IL_0022:  ldc.i4     0x27d9
  IL_0027:  newobj     ""decimal..ctor(int)""
  IL_002c:  call       ""bool decimal.op_Inequality(decimal, decimal)""
  IL_0031:  pop
  IL_0032:  ldloc.0
  IL_0033:  dup
  IL_0034:  call       ""decimal decimal.op_Addition(decimal, decimal)""
  IL_0039:  pop
  IL_003a:  ldloc.0
  IL_003b:  dup
  IL_003c:  call       ""decimal decimal.op_Subtraction(decimal, decimal)""
  IL_0041:  pop
  IL_0042:  ldloc.0
  IL_0043:  dup
  IL_0044:  call       ""decimal decimal.op_Multiply(decimal, decimal)""
  IL_0049:  pop
  IL_004a:  ldloc.0
  IL_004b:  dup
  IL_004c:  call       ""decimal decimal.op_Division(decimal, decimal)""
  IL_0051:  pop
  IL_0052:  ldloc.0
  IL_0053:  dup
  IL_0054:  call       ""decimal decimal.op_Modulus(decimal, decimal)""
  IL_0059:  pop
  IL_005a:  ldloc.0
  IL_005b:  dup
  IL_005c:  call       ""decimal decimal.op_Increment(decimal)""
  IL_0061:  stloc.0
  IL_0062:  pop
  IL_0063:  ldloc.0
  IL_0064:  dup
  IL_0065:  call       ""decimal decimal.op_Decrement(decimal)""
  IL_006a:  stloc.0
  IL_006b:  pop
  IL_006c:  ldloc.0
  IL_006d:  call       ""decimal decimal.op_UnaryNegation(decimal)""
  IL_0072:  pop
  IL_0073:  ret
}
");
        }

4434
        [WorkItem(732269, "DevDiv")]
P
Pilchie 已提交
4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528
        [Fact]
        public void NullCoalesce()
        {
            var source = @"
class Program
{
    static int Main(int? x, int y) { return x ?? y; }
}
";
            var comp = CompileAndVerify(source);
            comp.VerifyDiagnostics();
            comp.VerifyIL("Program.Main", @"
{
  // Code size       21 (0x15)
  .maxstack  1
  .locals init (int? V_0)
  IL_0000:  ldarg.0
  IL_0001:  stloc.0
  IL_0002:  ldloca.s   V_0
  IL_0004:  call       ""bool int?.HasValue.get""
  IL_0009:  brtrue.s   IL_000d
  IL_000b:  ldarg.1
  IL_000c:  ret
  IL_000d:  ldloca.s   V_0
  IL_000f:  call       ""int int?.GetValueOrDefault()""
  IL_0014:  ret
}
");
        }

        [Fact]
        public void TestCompoundOnAfieldOfGeneric()
        {
            var source = @"
class Program
{
    static void Main(string[] args)
    {
        var x = new c0();
        test<c0>.Repro1(x);
        System.Console.WriteLine(x.x);

        test<c0>.Repro2(x);
        System.Console.WriteLine(x.x);
    }
}

class c0
{
    public int x;

    public int P1
    {
        get { return x; }
        set { x = value; }
    }

    public int this[int i]
    {
        get { return x; }
        set { x = value; }
    }

    public static int Foo(c0 arg)
    {
        return 1;
    }

    public int Foo()
    {
        return 1;
    }
}

class test<T> where T : c0
{
    public static void Repro1(T arg)
    {
        arg.x += 1;
        arg.P1 += 1;
        arg[1] += 1;
    }

    public static void Repro2(T arg)
    {
        arg.x = c0.Foo(arg);
        arg.x = arg.Foo();
    }
}
";
            var compilation = CompileAndVerify(source, expectedOutput: @"3
1");
            compilation.VerifyIL("test<T>.Repro1(T)", @"
{
4529
  // Code size       80 (0x50)
P
Pilchie 已提交
4530
  .maxstack  4
4531
  .locals init (T& V_0)
P
Pilchie 已提交
4532 4533 4534 4535 4536 4537 4538
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  dup
  IL_0007:  ldfld      ""int c0.x""
  IL_000c:  ldc.i4.1
  IL_000d:  add
  IL_000e:  stfld      ""int c0.x""
4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553
  IL_0013:  ldarga.s   V_0
  IL_0015:  stloc.0
  IL_0016:  ldloc.0
  IL_0017:  ldloc.0
  IL_0018:  constrained. ""T""
  IL_001e:  callvirt   ""int c0.P1.get""
  IL_0023:  ldc.i4.1
  IL_0024:  add
  IL_0025:  constrained. ""T""
  IL_002b:  callvirt   ""void c0.P1.set""
  IL_0030:  ldarga.s   V_0
  IL_0032:  stloc.0
  IL_0033:  ldloc.0
  IL_0034:  ldc.i4.1
  IL_0035:  ldloc.0
P
Pilchie 已提交
4554
  IL_0036:  ldc.i4.1
4555 4556 4557 4558 4559 4560 4561
  IL_0037:  constrained. ""T""
  IL_003d:  callvirt   ""int c0.this[int].get""
  IL_0042:  ldc.i4.1
  IL_0043:  add
  IL_0044:  constrained. ""T""
  IL_004a:  callvirt   ""void c0.this[int].set""
  IL_004f:  ret
P
Pilchie 已提交
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584
}
").VerifyIL("test<T>.Repro2(T)", @"
{
  // Code size       45 (0x2d)
  .maxstack  2
  IL_0000:  ldarg.0
  IL_0001:  box        ""T""
  IL_0006:  ldarg.0
  IL_0007:  box        ""T""
  IL_000c:  call       ""int c0.Foo(c0)""
  IL_0011:  stfld      ""int c0.x""
  IL_0016:  ldarg.0
  IL_0017:  box        ""T""
  IL_001c:  ldarg.0
  IL_001d:  box        ""T""
  IL_0022:  callvirt   ""int c0.Foo()""
  IL_0027:  stfld      ""int c0.x""
  IL_002c:  ret
}
");
        }
    }
}