PageRenderTime 77ms CodeModel.GetById 53ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenConditionalOperatorTests.cs

https://github.com/EkardNT/Roslyn
C# | 2266 lines | 2168 code | 54 blank | 44 comment | 2 complexity | 02e500b7f2fff032c9184e2774657abe MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1// Copyright (c) Microsoft Open Technologies, Inc.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
   2
   3using Microsoft.CodeAnalysis.CSharp.Symbols;
   4using Microsoft.CodeAnalysis.CSharp.Syntax;
   5using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
   6using Microsoft.CodeAnalysis.Text;
   7using Roslyn.Test.Utilities;
   8using Xunit;
   9
  10namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
  11{
  12    public class CodeGenConditionalOperatorTests : CSharpTestBase
  13    {
  14        [Fact, WorkItem(638289, "DevDiv")]
  15        public void ConditionalDelegateInterfaceUnification1()
  16        {
  17            var src =
  18@"using System;
  19using System.Security;
  20
  21[assembly: SecurityTransparent()]
  22
  23interface I {}
  24
  25class A : I {}
  26
  27class C : I
  28{
  29    static Func<I> Tester(bool a)
  30    {
  31        return a 
  32            ? (Func<I>)(() => new A())
  33            : () => new C();
  34    }
  35    static void Main()
  36    {
  37        System.Console.Write(Tester(false)().GetType());
  38    }
  39}";
  40            var verify = CompileAndVerify(src,
  41                options: TestOptions.UnoptimizedExe,
  42                expectedOutput: "C");
  43            verify.VerifyIL("C.Tester", @"
  44{
  45  // Code size       64 (0x40)
  46  .maxstack  2
  47  .locals init (System.Func<I> V_0)
  48  IL_0000:  ldarg.0
  49  IL_0001:  brtrue.s   IL_0020
  50  IL_0003:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
  51  IL_0008:  dup
  52  IL_0009:  brtrue.s   IL_001e
  53  IL_000b:  pop
  54  IL_000c:  ldnull
  55  IL_000d:  ldftn      ""I C.<Tester>b__2(object)""
  56  IL_0013:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
  57  IL_0018:  dup
  58  IL_0019:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
  59  IL_001e:  br.s       IL_003b
  60  IL_0020:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
  61  IL_0025:  dup
  62  IL_0026:  brtrue.s   IL_003b
  63  IL_0028:  pop
  64  IL_0029:  ldnull
  65  IL_002a:  ldftn      ""I C.<Tester>b__0(object)""
  66  IL_0030:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
  67  IL_0035:  dup
  68  IL_0036:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
  69  IL_003b:  stloc.0
  70  IL_003c:  br.s       IL_003e
  71  IL_003e:  ldloc.0
  72  IL_003f:  ret
  73}
  74");
  75
  76            verify = CompileAndVerify(src,
  77                expectedOutput: "C");
  78            verify.VerifyIL("C.Tester", @"
  79{
  80  // Code size       59 (0x3b)
  81  .maxstack  2
  82  IL_0000:  ldarg.0
  83  IL_0001:  brtrue.s   IL_001f
  84  IL_0003:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
  85  IL_0008:  dup
  86  IL_0009:  brtrue.s   IL_003a
  87  IL_000b:  pop
  88  IL_000c:  ldnull
  89  IL_000d:  ldftn      ""I C.<Tester>b__2(object)""
  90  IL_0013:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
  91  IL_0018:  dup
  92  IL_0019:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
  93  IL_001e:  ret
  94  IL_001f:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
  95  IL_0024:  dup
  96  IL_0025:  brtrue.s   IL_003a
  97  IL_0027:  pop
  98  IL_0028:  ldnull
  99  IL_0029:  ldftn      ""I C.<Tester>b__0(object)""
 100  IL_002f:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 101  IL_0034:  dup
 102  IL_0035:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 103  IL_003a:  ret
 104}
 105");
 106        }
 107
 108        [Fact, WorkItem(638289, "DevDiv")]
 109        public void ConditionalDelegateInterfaceUnification2()
 110        {
 111            var src =
 112@"using System;
 113using System.Security;
 114
 115[assembly: SecurityTransparent()]
 116
 117interface I {}
 118
 119class A : I {}
 120
 121class B : I {}
 122
 123class C : I
 124{
 125    static Func<I> Tester(int a)
 126    {
 127        return a > 0 
 128              ? a == 1 
 129                ? (Func<I>)(() => new A())
 130                : () => new B()
 131              : (() => new C());
 132    }
 133    static void Main()
 134    {
 135        System.Console.Write(Tester(1)().GetType());
 136    }
 137}";
 138            var verify = CompileAndVerify(src,
 139                options: TestOptions.UnoptimizedExe,
 140                expectedOutput: "A");
 141            verify.VerifyIL("C.Tester", @"
 142{
 143  // Code size       98 (0x62)
 144  .maxstack  2
 145  .locals init (System.Func<I> V_0)
 146  IL_0000:  ldarg.0
 147  IL_0001:  ldc.i4.0
 148  IL_0002:  bgt.s      IL_0021
 149  IL_0004:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 150  IL_0009:  dup
 151  IL_000a:  brtrue.s   IL_001f
 152  IL_000c:  pop
 153  IL_000d:  ldnull
 154  IL_000e:  ldftn      ""I C.<Tester>b__4(object)""
 155  IL_0014:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 156  IL_0019:  dup
 157  IL_001a:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 158  IL_001f:  br.s       IL_005d
 159  IL_0021:  ldarg.0
 160  IL_0022:  ldc.i4.1
 161  IL_0023:  beq.s      IL_0042
 162  IL_0025:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 163  IL_002a:  dup
 164  IL_002b:  brtrue.s   IL_0040
 165  IL_002d:  pop
 166  IL_002e:  ldnull
 167  IL_002f:  ldftn      ""I C.<Tester>b__2(object)""
 168  IL_0035:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 169  IL_003a:  dup
 170  IL_003b:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 171  IL_0040:  br.s       IL_005d
 172  IL_0042:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 173  IL_0047:  dup
 174  IL_0048:  brtrue.s   IL_005d
 175  IL_004a:  pop
 176  IL_004b:  ldnull
 177  IL_004c:  ldftn      ""I C.<Tester>b__0(object)""
 178  IL_0052:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 179  IL_0057:  dup
 180  IL_0058:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 181  IL_005d:  stloc.0
 182  IL_005e:  br.s       IL_0060
 183  IL_0060:  ldloc.0
 184  IL_0061:  ret
 185}
 186");
 187            verify = CompileAndVerify(src,
 188                expectedOutput: "A");
 189            verify.VerifyIL("C.Tester", @"
 190{
 191  // Code size       92 (0x5c)
 192  .maxstack  2
 193  IL_0000:  ldarg.0
 194  IL_0001:  ldc.i4.0
 195  IL_0002:  bgt.s      IL_0020
 196  IL_0004:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 197  IL_0009:  dup
 198  IL_000a:  brtrue.s   IL_005b
 199  IL_000c:  pop
 200  IL_000d:  ldnull
 201  IL_000e:  ldftn      ""I C.<Tester>b__4(object)""
 202  IL_0014:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 203  IL_0019:  dup
 204  IL_001a:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 205  IL_001f:  ret
 206  IL_0020:  ldarg.0
 207  IL_0021:  ldc.i4.1
 208  IL_0022:  beq.s      IL_0040
 209  IL_0024:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 210  IL_0029:  dup
 211  IL_002a:  brtrue.s   IL_005b
 212  IL_002c:  pop
 213  IL_002d:  ldnull
 214  IL_002e:  ldftn      ""I C.<Tester>b__2(object)""
 215  IL_0034:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 216  IL_0039:  dup
 217  IL_003a:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 218  IL_003f:  ret
 219  IL_0040:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 220  IL_0045:  dup
 221  IL_0046:  brtrue.s   IL_005b
 222  IL_0048:  pop
 223  IL_0049:  ldnull
 224  IL_004a:  ldftn      ""I C.<Tester>b__0(object)""
 225  IL_0050:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 226  IL_0055:  dup
 227  IL_0056:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 228  IL_005b:  ret
 229}
 230");
 231        }
 232
 233        [Fact, WorkItem(638289, "DevDiv")]
 234        public void ConditionalDelegateInterfaceUnification3()
 235        {
 236            var src =
 237                @"using System;
 238using System.Security;
 239
 240[assembly: SecurityTransparent()]
 241
 242interface I {}
 243
 244class A : I {}
 245
 246class B : I {}
 247
 248class C : I
 249{
 250    static Func<I> Tester(int a)
 251    {
 252        return a > 0 
 253                ? (() => new A())
 254                : a == -1 
 255                    ? (Func<I>)(() => new B())
 256                    : (() => new C());
 257    }
 258    static void Main()
 259    {
 260        System.Console.Write(Tester(-1)().GetType());
 261    }
 262}";
 263            var verify = CompileAndVerify(src,
 264                options: TestOptions.UnoptimizedExe,
 265                expectedOutput: "B");
 266            verify.VerifyIL("C.Tester", @"
 267{
 268  // Code size       98 (0x62)
 269  .maxstack  2
 270  .locals init (System.Func<I> V_0)
 271  IL_0000:  ldarg.0
 272  IL_0001:  ldc.i4.0
 273  IL_0002:  bgt.s      IL_0042
 274  IL_0004:  ldarg.0
 275  IL_0005:  ldc.i4.m1
 276  IL_0006:  beq.s      IL_0025
 277  IL_0008:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 278  IL_000d:  dup
 279  IL_000e:  brtrue.s   IL_0023
 280  IL_0010:  pop
 281  IL_0011:  ldnull
 282  IL_0012:  ldftn      ""I C.<Tester>b__4(object)""
 283  IL_0018:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 284  IL_001d:  dup
 285  IL_001e:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 286  IL_0023:  br.s       IL_0040
 287  IL_0025:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 288  IL_002a:  dup
 289  IL_002b:  brtrue.s   IL_0040
 290  IL_002d:  pop
 291  IL_002e:  ldnull
 292  IL_002f:  ldftn      ""I C.<Tester>b__2(object)""
 293  IL_0035:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 294  IL_003a:  dup
 295  IL_003b:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 296  IL_0040:  br.s       IL_005d
 297  IL_0042:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 298  IL_0047:  dup
 299  IL_0048:  brtrue.s   IL_005d
 300  IL_004a:  pop
 301  IL_004b:  ldnull
 302  IL_004c:  ldftn      ""I C.<Tester>b__0(object)""
 303  IL_0052:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 304  IL_0057:  dup
 305  IL_0058:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 306  IL_005d:  stloc.0
 307  IL_005e:  br.s       IL_0060
 308  IL_0060:  ldloc.0
 309  IL_0061:  ret
 310}
 311");
 312            verify = CompileAndVerify(src,
 313                expectedOutput: "B");
 314            verify.VerifyIL("C.Tester", @"
 315{
 316  // Code size       92 (0x5c)
 317  .maxstack  2
 318  IL_0000:  ldarg.0
 319  IL_0001:  ldc.i4.0
 320  IL_0002:  bgt.s      IL_0040
 321  IL_0004:  ldarg.0
 322  IL_0005:  ldc.i4.m1
 323  IL_0006:  beq.s      IL_0024
 324  IL_0008:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 325  IL_000d:  dup
 326  IL_000e:  brtrue.s   IL_005b
 327  IL_0010:  pop
 328  IL_0011:  ldnull
 329  IL_0012:  ldftn      ""I C.<Tester>b__4(object)""
 330  IL_0018:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 331  IL_001d:  dup
 332  IL_001e:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 333  IL_0023:  ret
 334  IL_0024:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 335  IL_0029:  dup
 336  IL_002a:  brtrue.s   IL_005b
 337  IL_002c:  pop
 338  IL_002d:  ldnull
 339  IL_002e:  ldftn      ""I C.<Tester>b__2(object)""
 340  IL_0034:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 341  IL_0039:  dup
 342  IL_003a:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 343  IL_003f:  ret
 344  IL_0040:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 345  IL_0045:  dup
 346  IL_0046:  brtrue.s   IL_005b
 347  IL_0048:  pop
 348  IL_0049:  ldnull
 349  IL_004a:  ldftn      ""I C.<Tester>b__0(object)""
 350  IL_0050:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 351  IL_0055:  dup
 352  IL_0056:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 353  IL_005b:  ret
 354}");
 355        }
 356
 357        [Fact, WorkItem(638289, "DevDiv")]
 358        public void ConditionalDelegateInterfaceUnification4()
 359        {
 360            var src =
 361                @"using System;
 362using System.Security;
 363
 364[assembly: SecurityTransparent()]
 365
 366interface I {}
 367
 368class A : I {}
 369
 370class B : I {}
 371
 372class D : I {}
 373
 374class C : I
 375{
 376    static Func<I> Tester(int a)
 377    {
 378        return a > 0 
 379                ? a == 1 
 380                    ? (Func<I>)(() => new A())
 381                    : () => new B()
 382                : a == -1 
 383                    ? () => new C()
 384                    : (Func<I>)(() => new D());
 385    }
 386    static void Main()
 387    {
 388        System.Console.Write(Tester(-2)().GetType());
 389    }
 390}";
 391            var verify = CompileAndVerify(src,
 392                options: TestOptions.UnoptimizedExe,
 393                expectedOutput: "D");
 394            verify.VerifyIL("C.Tester", @"
 395{
 396  // Code size      131 (0x83)
 397  .maxstack  2
 398  .locals init (System.Func<I> V_0)
 399  IL_0000:  ldarg.0
 400  IL_0001:  ldc.i4.0
 401  IL_0002:  bgt.s      IL_0042
 402  IL_0004:  ldarg.0
 403  IL_0005:  ldc.i4.m1
 404  IL_0006:  beq.s      IL_0025
 405  IL_0008:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate7""
 406  IL_000d:  dup
 407  IL_000e:  brtrue.s   IL_0023
 408  IL_0010:  pop
 409  IL_0011:  ldnull
 410  IL_0012:  ldftn      ""I C.<Tester>b__6(object)""
 411  IL_0018:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 412  IL_001d:  dup
 413  IL_001e:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate7""
 414  IL_0023:  br.s       IL_0040
 415  IL_0025:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 416  IL_002a:  dup
 417  IL_002b:  brtrue.s   IL_0040
 418  IL_002d:  pop
 419  IL_002e:  ldnull
 420  IL_002f:  ldftn      ""I C.<Tester>b__4(object)""
 421  IL_0035:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 422  IL_003a:  dup
 423  IL_003b:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 424  IL_0040:  br.s       IL_007e
 425  IL_0042:  ldarg.0
 426  IL_0043:  ldc.i4.1
 427  IL_0044:  beq.s      IL_0063
 428  IL_0046:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 429  IL_004b:  dup
 430  IL_004c:  brtrue.s   IL_0061
 431  IL_004e:  pop
 432  IL_004f:  ldnull
 433  IL_0050:  ldftn      ""I C.<Tester>b__2(object)""
 434  IL_0056:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 435  IL_005b:  dup
 436  IL_005c:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 437  IL_0061:  br.s       IL_007e
 438  IL_0063:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 439  IL_0068:  dup
 440  IL_0069:  brtrue.s   IL_007e
 441  IL_006b:  pop
 442  IL_006c:  ldnull
 443  IL_006d:  ldftn      ""I C.<Tester>b__0(object)""
 444  IL_0073:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 445  IL_0078:  dup
 446  IL_0079:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 447  IL_007e:  stloc.0
 448  IL_007f:  br.s       IL_0081
 449  IL_0081:  ldloc.0
 450  IL_0082:  ret
 451}
 452");
 453            verify = CompileAndVerify(src,
 454                expectedOutput: "D");
 455            verify.VerifyIL("C.Tester", @"
 456{
 457  // Code size      124 (0x7c)
 458  .maxstack  2
 459  IL_0000:  ldarg.0
 460  IL_0001:  ldc.i4.0
 461  IL_0002:  bgt.s      IL_0040
 462  IL_0004:  ldarg.0
 463  IL_0005:  ldc.i4.m1
 464  IL_0006:  beq.s      IL_0024
 465  IL_0008:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate7""
 466  IL_000d:  dup
 467  IL_000e:  brtrue.s   IL_007b
 468  IL_0010:  pop
 469  IL_0011:  ldnull
 470  IL_0012:  ldftn      ""I C.<Tester>b__6(object)""
 471  IL_0018:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 472  IL_001d:  dup
 473  IL_001e:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate7""
 474  IL_0023:  ret
 475  IL_0024:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 476  IL_0029:  dup
 477  IL_002a:  brtrue.s   IL_007b
 478  IL_002c:  pop
 479  IL_002d:  ldnull
 480  IL_002e:  ldftn      ""I C.<Tester>b__4(object)""
 481  IL_0034:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 482  IL_0039:  dup
 483  IL_003a:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate5""
 484  IL_003f:  ret
 485  IL_0040:  ldarg.0
 486  IL_0041:  ldc.i4.1
 487  IL_0042:  beq.s      IL_0060
 488  IL_0044:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 489  IL_0049:  dup
 490  IL_004a:  brtrue.s   IL_007b
 491  IL_004c:  pop
 492  IL_004d:  ldnull
 493  IL_004e:  ldftn      ""I C.<Tester>b__2(object)""
 494  IL_0054:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 495  IL_0059:  dup
 496  IL_005a:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate3""
 497  IL_005f:  ret
 498  IL_0060:  ldsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 499  IL_0065:  dup
 500  IL_0066:  brtrue.s   IL_007b
 501  IL_0068:  pop
 502  IL_0069:  ldnull
 503  IL_006a:  ldftn      ""I C.<Tester>b__0(object)""
 504  IL_0070:  newobj     ""System.Func<I>..ctor(object, System.IntPtr)""
 505  IL_0075:  dup
 506  IL_0076:  stsfld     ""System.Func<I> C.CS$<>9__CachedAnonymousMethodDelegate1""
 507  IL_007b:  ret
 508}
 509");
 510        }
 511
 512        [Fact(), WorkItem(638289, "DevDiv")]
 513        public void NestedConditional1()
 514        {
 515            var src =
 516@"using System.Security;
 517
 518[assembly: SecurityTransparent()]
 519
 520public interface I {}
 521
 522class A : I {}
 523
 524class B : I {}
 525
 526public class C : I
 527{
 528    static I Tester(int x, int y)
 529    {
 530        return x == 0 
 531            ? (y == 0 ? (I)new A() : new B())
 532            : new C();
 533    }
 534    static void Main()
 535    {
 536        System.Console.Write(Tester(1, 0).GetType());
 537    }
 538}";
 539            var verify = CompileAndVerify(src,
 540                options: TestOptions.UnoptimizedExe,
 541                expectedOutput: "C");
 542            verify.VerifyIL("C.Tester", @"
 543{
 544  // Code size       36 (0x24)
 545  .maxstack  1
 546  .locals init (I V_0)
 547  IL_0000:  ldarg.0
 548  IL_0001:  brfalse.s  IL_000c
 549  IL_0003:  newobj     ""C..ctor()""
 550  IL_0008:  stloc.0
 551  IL_0009:  ldloc.0
 552  IL_000a:  br.s       IL_001f
 553  IL_000c:  ldarg.1
 554  IL_000d:  brfalse.s  IL_0018
 555  IL_000f:  newobj     ""B..ctor()""
 556  IL_0014:  stloc.0
 557  IL_0015:  ldloc.0
 558  IL_0016:  br.s       IL_001f
 559  IL_0018:  newobj     ""A..ctor()""
 560  IL_001d:  stloc.0
 561  IL_001e:  ldloc.0
 562  IL_001f:  stloc.0
 563  IL_0020:  br.s       IL_0022
 564  IL_0022:  ldloc.0
 565  IL_0023:  ret
 566}");
 567            // Optimized
 568            verify = CompileAndVerify(src,
 569                expectedOutput: "C");
 570            verify.VerifyIL("C.Tester", @"
 571{
 572  // Code size       30 (0x1e)
 573  .maxstack  1
 574  .locals init (I V_0)
 575  IL_0000:  ldarg.0
 576  IL_0001:  brfalse.s  IL_000b
 577  IL_0003:  newobj     ""C..ctor()""
 578  IL_0008:  stloc.0
 579  IL_0009:  ldloc.0
 580  IL_000a:  ret
 581  IL_000b:  ldarg.1
 582  IL_000c:  brfalse.s  IL_0016
 583  IL_000e:  newobj     ""B..ctor()""
 584  IL_0013:  stloc.0
 585  IL_0014:  ldloc.0
 586  IL_0015:  ret
 587  IL_0016:  newobj     ""A..ctor()""
 588  IL_001b:  stloc.0
 589  IL_001c:  ldloc.0
 590  IL_001d:  ret
 591}");
 592        }
 593
 594        [Fact(), WorkItem(638289, "DevDiv")]
 595        public void NestedConditional2()
 596        {
 597            var src =
 598@"using System.Security;
 599
 600[assembly: SecurityTransparent()]
 601
 602public interface I {}
 603
 604class A : I {}
 605
 606class B : I {}
 607
 608public class C : I
 609{
 610    static I Tester(int x)
 611    {
 612        return x > 0 
 613            ? new C()
 614            : (x < 0 ? (I)new A() : new B());
 615    }
 616    static void Main()
 617    {
 618        System.Console.Write(Tester(0).GetType());
 619    }
 620}";
 621            var verify = CompileAndVerify(src,
 622                options: TestOptions.UnoptimizedExe,
 623                expectedOutput: "B");
 624            verify.VerifyIL("C.Tester", @"
 625{
 626  // Code size       38 (0x26)
 627  .maxstack  2
 628  .locals init (I V_0)
 629  IL_0000:  ldarg.0
 630  IL_0001:  ldc.i4.0
 631  IL_0002:  bgt.s      IL_001a
 632  IL_0004:  ldarg.0
 633  IL_0005:  ldc.i4.0
 634  IL_0006:  blt.s      IL_0011
 635  IL_0008:  newobj     ""B..ctor()""
 636  IL_000d:  stloc.0
 637  IL_000e:  ldloc.0
 638  IL_000f:  br.s       IL_0018
 639  IL_0011:  newobj     ""A..ctor()""
 640  IL_0016:  stloc.0
 641  IL_0017:  ldloc.0
 642  IL_0018:  br.s       IL_0021
 643  IL_001a:  newobj     ""C..ctor()""
 644  IL_001f:  stloc.0
 645  IL_0020:  ldloc.0
 646  IL_0021:  stloc.0
 647  IL_0022:  br.s       IL_0024
 648  IL_0024:  ldloc.0
 649  IL_0025:  ret
 650}");
 651            // Optimized
 652            verify = CompileAndVerify(src,
 653                expectedOutput: "B");
 654            verify.VerifyIL("C.Tester", @"
 655{
 656  // Code size       32 (0x20)
 657  .maxstack  2
 658  .locals init (I V_0)
 659  IL_0000:  ldarg.0
 660  IL_0001:  ldc.i4.0
 661  IL_0002:  bgt.s      IL_0018
 662  IL_0004:  ldarg.0
 663  IL_0005:  ldc.i4.0
 664  IL_0006:  blt.s      IL_0010
 665  IL_0008:  newobj     ""B..ctor()""
 666  IL_000d:  stloc.0
 667  IL_000e:  ldloc.0
 668  IL_000f:  ret
 669  IL_0010:  newobj     ""A..ctor()""
 670  IL_0015:  stloc.0
 671  IL_0016:  ldloc.0
 672  IL_0017:  ret
 673  IL_0018:  newobj     ""C..ctor()""
 674  IL_001d:  stloc.0
 675  IL_001e:  ldloc.0
 676  IL_001f:  ret
 677}");
 678        }
 679
 680        [Fact(), WorkItem(638289, "DevDiv")]
 681        public void NestedConditional3()
 682        {
 683            var src =
 684@"using System.Security;
 685
 686[assembly: SecurityTransparent()]
 687
 688public interface I {}
 689
 690class A : I {}
 691
 692class B : I {}
 693
 694class D : I {}
 695
 696public class C : I
 697{
 698    static I Tester(int x)
 699    {
 700        return x > 0 
 701            ? x == 1 ? (I)new A() : new B()
 702            : x == 0 ? (I)new C() : new D();
 703    }
 704    static void Main()
 705    {
 706        System.Console.Write(Tester(1).GetType());
 707    }
 708}";
 709            var verify = CompileAndVerify(src,
 710                options: TestOptions.UnoptimizedExe,
 711                expectedOutput: "A");
 712            verify.VerifyIL("C.Tester", @"
 713{
 714  // Code size       50 (0x32)
 715  .maxstack  2
 716  .locals init (I V_0)
 717  IL_0000:  ldarg.0
 718  IL_0001:  ldc.i4.0
 719  IL_0002:  bgt.s      IL_0019
 720  IL_0004:  ldarg.0
 721  IL_0005:  brfalse.s  IL_0010
 722  IL_0007:  newobj     ""D..ctor()""
 723  IL_000c:  stloc.0
 724  IL_000d:  ldloc.0
 725  IL_000e:  br.s       IL_0017
 726  IL_0010:  newobj     ""C..ctor()""
 727  IL_0015:  stloc.0
 728  IL_0016:  ldloc.0
 729  IL_0017:  br.s       IL_002d
 730  IL_0019:  ldarg.0
 731  IL_001a:  ldc.i4.1
 732  IL_001b:  beq.s      IL_0026
 733  IL_001d:  newobj     ""B..ctor()""
 734  IL_0022:  stloc.0
 735  IL_0023:  ldloc.0
 736  IL_0024:  br.s       IL_002d
 737  IL_0026:  newobj     ""A..ctor()""
 738  IL_002b:  stloc.0
 739  IL_002c:  ldloc.0
 740  IL_002d:  stloc.0
 741  IL_002e:  br.s       IL_0030
 742  IL_0030:  ldloc.0
 743  IL_0031:  ret
 744}");
 745            // Optimized
 746            verify = CompileAndVerify(src,
 747                expectedOutput: "A");
 748            verify.VerifyIL("C.Tester", @"
 749{
 750  // Code size       43 (0x2b)
 751  .maxstack  2
 752  .locals init (I V_0)
 753  IL_0000:  ldarg.0
 754  IL_0001:  ldc.i4.0
 755  IL_0002:  bgt.s      IL_0017
 756  IL_0004:  ldarg.0
 757  IL_0005:  brfalse.s  IL_000f
 758  IL_0007:  newobj     ""D..ctor()""
 759  IL_000c:  stloc.0
 760  IL_000d:  ldloc.0
 761  IL_000e:  ret
 762  IL_000f:  newobj     ""C..ctor()""
 763  IL_0014:  stloc.0
 764  IL_0015:  ldloc.0
 765  IL_0016:  ret
 766  IL_0017:  ldarg.0
 767  IL_0018:  ldc.i4.1
 768  IL_0019:  beq.s      IL_0023
 769  IL_001b:  newobj     ""B..ctor()""
 770  IL_0020:  stloc.0
 771  IL_0021:  ldloc.0
 772  IL_0022:  ret
 773  IL_0023:  newobj     ""A..ctor()""
 774  IL_0028:  stloc.0
 775  IL_0029:  ldloc.0
 776  IL_002a:  ret
 777}");
 778        }
 779
 780        [Fact(), WorkItem(638289, "DevDiv")]
 781        public void NestedConditional4()
 782        {
 783            var src =
 784@"using System.Security;
 785
 786[assembly: SecurityTransparent()]
 787
 788public interface I {}
 789
 790class A : I {}
 791
 792class B : I {}
 793
 794class D : I {}
 795
 796public class C : I
 797{
 798    static I Tester(int x)
 799    {
 800        return x > 0 
 801            ? x > 1
 802                ? x > 2 
 803                    ? (I)new A() 
 804                    : new B() 
 805                : new C()
 806            : new D();
 807    }
 808    static void Main()
 809    {
 810        System.Console.Write(Tester(0).GetType());
 811    }
 812}";
 813            var verify = CompileAndVerify(src,
 814                options: TestOptions.UnoptimizedExe,
 815                expectedOutput: "D");
 816            verify.VerifyIL("C.Tester", @"
 817{
 818  // Code size       51 (0x33)
 819  .maxstack  2
 820  .locals init (I V_0)
 821  IL_0000:  ldarg.0
 822  IL_0001:  ldc.i4.0
 823  IL_0002:  bgt.s      IL_000d
 824  IL_0004:  newobj     ""D..ctor()""
 825  IL_0009:  stloc.0
 826  IL_000a:  ldloc.0
 827  IL_000b:  br.s       IL_002e
 828  IL_000d:  ldarg.0
 829  IL_000e:  ldc.i4.1
 830  IL_000f:  bgt.s      IL_001a
 831  IL_0011:  newobj     ""C..ctor()""
 832  IL_0016:  stloc.0
 833  IL_0017:  ldloc.0
 834  IL_0018:  br.s       IL_002e
 835  IL_001a:  ldarg.0
 836  IL_001b:  ldc.i4.2
 837  IL_001c:  bgt.s      IL_0027
 838  IL_001e:  newobj     ""B..ctor()""
 839  IL_0023:  stloc.0
 840  IL_0024:  ldloc.0
 841  IL_0025:  br.s       IL_002e
 842  IL_0027:  newobj     ""A..ctor()""
 843  IL_002c:  stloc.0
 844  IL_002d:  ldloc.0
 845  IL_002e:  stloc.0
 846  IL_002f:  br.s       IL_0031
 847  IL_0031:  ldloc.0
 848  IL_0032:  ret
 849}");
 850            // Optimized
 851            verify = CompileAndVerify(src,
 852                expectedOutput: "D");
 853            verify.VerifyIL("C.Tester", @"
 854{
 855  // Code size       44 (0x2c)
 856  .maxstack  2
 857  .locals init (I V_0)
 858  IL_0000:  ldarg.0
 859  IL_0001:  ldc.i4.0
 860  IL_0002:  bgt.s      IL_000c
 861  IL_0004:  newobj     ""D..ctor()""
 862  IL_0009:  stloc.0
 863  IL_000a:  ldloc.0
 864  IL_000b:  ret
 865  IL_000c:  ldarg.0
 866  IL_000d:  ldc.i4.1
 867  IL_000e:  bgt.s      IL_0018
 868  IL_0010:  newobj     ""C..ctor()""
 869  IL_0015:  stloc.0
 870  IL_0016:  ldloc.0
 871  IL_0017:  ret
 872  IL_0018:  ldarg.0
 873  IL_0019:  ldc.i4.2
 874  IL_001a:  bgt.s      IL_0024
 875  IL_001c:  newobj     ""B..ctor()""
 876  IL_0021:  stloc.0
 877  IL_0022:  ldloc.0
 878  IL_0023:  ret
 879  IL_0024:  newobj     ""A..ctor()""
 880  IL_0029:  stloc.0
 881  IL_002a:  ldloc.0
 882  IL_002b:  ret
 883}");
 884        }
 885
 886        [Fact]
 887        public void TestConditionalOperatorSimple()
 888        {
 889            var source = @"
 890class C
 891{
 892    static void Main()
 893    {
 894        bool b = true;
 895        System.Console.WriteLine(b ? 1 : 2);
 896    }
 897}";
 898            var comp = CompileAndVerify(source, expectedOutput: "1");
 899            comp.VerifyDiagnostics();
 900            comp.VerifyIL("C.Main", @"{
 901  // Code size       13 (0xd)
 902  .maxstack  1
 903  IL_0000:  ldc.i4.1
 904  IL_0001:  brtrue.s   IL_0006
 905  IL_0003:  ldc.i4.2
 906  IL_0004:  br.s       IL_0007
 907  IL_0006:  ldc.i4.1
 908  IL_0007:  call       ""void System.Console.WriteLine(int)""
 909  IL_000c:  ret
 910}");
 911        }
 912
 913        [Fact]
 914        public void TestConditionalOperatorConstantCondition()
 915        {
 916            var source = @"
 917class C
 918{
 919    static void Main()
 920    {
 921        int x = 1;
 922        int y = 2;
 923        System.Console.WriteLine(true ? x : y);
 924        System.Console.WriteLine(false ? x : y);
 925    }
 926}";
 927            var comp = CompileAndVerify(source, expectedOutput: @"
 9281
 9292");
 930            comp.VerifyDiagnostics();
 931            comp.VerifyIL("C.Main", @"{
 932  // Code size       15 (0xf)
 933  .maxstack  2
 934  .locals init (int V_0) //y
 935  IL_0000:  ldc.i4.1
 936  IL_0001:  ldc.i4.2
 937  IL_0002:  stloc.0
 938  IL_0003:  call       ""void System.Console.WriteLine(int)""
 939  IL_0008:  ldloc.0
 940  IL_0009:  call       ""void System.Console.WriteLine(int)""
 941  IL_000e:  ret
 942}");
 943        }
 944
 945        [Fact]
 946        public void TestConditionalOperatorNullLiteral()
 947        {
 948            var source = @"
 949class C
 950{
 951    static void Main()
 952    {
 953        bool b = true;
 954        string s1 = b ? ""hello"" : null;
 955        System.Console.WriteLine(s1);
 956        string s2 = b ? null : ""goodbye"";
 957        System.Console.WriteLine(s2);
 958    }
 959}";
 960            var comp = CompileAndVerify(source);
 961            comp.VerifyDiagnostics();
 962            comp.VerifyIL("C.Main", @"
 963{
 964  // Code size       33 (0x21)
 965  .maxstack  2
 966  IL_0000:  ldc.i4.1
 967  IL_0001:  dup
 968  IL_0002:  brtrue.s   IL_0007
 969  IL_0004:  ldnull
 970  IL_0005:  br.s       IL_000c
 971  IL_0007:  ldstr      ""hello""
 972  IL_000c:  call       ""void System.Console.WriteLine(string)""
 973  IL_0011:  brtrue.s   IL_001a
 974  IL_0013:  ldstr      ""goodbye""
 975  IL_0018:  br.s       IL_001b
 976  IL_001a:  ldnull
 977  IL_001b:  call       ""void System.Console.WriteLine(string)""
 978  IL_0020:  ret
 979}");
 980        }
 981
 982        [Fact]
 983        public void TestConditionalOperatorLambda()
 984        {
 985            var source = @"
 986class C
 987{
 988    static void Main()
 989    {
 990        bool b = true;
 991        System.Func<int, int> f = null;
 992        System.Console.WriteLine(f);
 993        System.Func<int, int> g1 = b ? f : x => x;
 994        System.Console.WriteLine(g1);
 995        System.Func<int, int> g2 = b ? x => x : f;
 996        System.Console.WriteLine(g2);
 997    }
 998}";
 999            // NOTE: this is slightly different from the Dev10 IL, which caches the lambdas in static fields
1000            var comp = CompileAndVerify(source);
1001            comp.VerifyDiagnostics();
1002            comp.VerifyIL("C.Main", @"
1003{
1004  // Code size       85 (0x55)
1005  .maxstack  3
1006  .locals init (System.Func<int, int> V_0) //f
1007  IL_0000:  ldc.i4.1
1008  IL_0001:  ldnull
1009  IL_0002:  stloc.0
1010  IL_0003:  ldloc.0
1011  IL_0004:  call       ""void System.Console.WriteLine(object)""
1012  IL_0009:  dup
1013  IL_000a:  brtrue.s   IL_0029
1014  IL_000c:  ldsfld     ""System.Func<int, int> C.CS$<>9__CachedAnonymousMethodDelegate1""
1015  IL_0011:  dup
1016  IL_0012:  brtrue.s   IL_002a
1017  IL_0014:  pop
1018  IL_0015:  ldnull
1019  IL_0016:  ldftn      ""int C.<Main>b__0(object, int)""
1020  IL_001c:  newobj     ""System.Func<int, int>..ctor(object, System.IntPtr)""
1021  IL_0021:  dup
1022  IL_0022:  stsfld     ""System.Func<int, int> C.CS$<>9__CachedAnonymousMethodDelegate1""
1023  IL_0027:  br.s       IL_002a
1024  IL_0029:  ldloc.0
1025  IL_002a:  call       ""void System.Console.WriteLine(object)""
1026  IL_002f:  brtrue.s   IL_0034
1027  IL_0031:  ldloc.0
1028  IL_0032:  br.s       IL_004f
1029  IL_0034:  ldsfld     ""System.Func<int, int> C.CS$<>9__CachedAnonymousMethodDelegate3""
1030  IL_0039:  dup
1031  IL_003a:  brtrue.s   IL_004f
1032  IL_003c:  pop
1033  IL_003d:  ldnull
1034  IL_003e:  ldftn      ""int C.<Main>b__2(object, int)""
1035  IL_0044:  newobj     ""System.Func<int, int>..ctor(object, System.IntPtr)""
1036  IL_0049:  dup
1037  IL_004a:  stsfld     ""System.Func<int, int> C.CS$<>9__CachedAnonymousMethodDelegate3""
1038  IL_004f:  call       ""void System.Console.WriteLine(object)""
1039  IL_0054:  ret
1040}
1041");
1042        }
1043
1044        [Fact]
1045        public void TestConditionalOperatorMethodGroup()
1046        {
1047            var source = @"
1048class C
1049{
1050    static void Main()
1051    {
1052        bool b = true;
1053        System.Func<int> f = null;
1054        System.Console.WriteLine(f);
1055        System.Func<int> g1 = b ? f : M;
1056        System.Console.WriteLine(g1);
1057        System.Func<int> g2 = b ? M : f;
1058        System.Console.WriteLine(g2);
1059    }
1060
1061    static int M()
1062    {
1063        return 0;
1064    }
1065}";
1066            var comp = CompileAndVerify(source);
1067            comp.VerifyDiagnostics();
1068            comp.VerifyIL("C.Main", @"
1069{
1070  // Code size       55 (0x37)
1071  .maxstack  3
1072  .locals init (System.Func<int> V_0) //f
1073  IL_0000:  ldc.i4.1
1074  IL_0001:  ldnull
1075  IL_0002:  stloc.0
1076  IL_0003:  ldloc.0
1077  IL_0004:  call       ""void System.Console.WriteLine(object)""
1078  IL_0009:  dup
1079  IL_000a:  brtrue.s   IL_001a
1080  IL_000c:  ldnull
1081  IL_000d:  ldftn      ""int C.M()""
1082  IL_0013:  newobj     ""System.Func<int>..ctor(object, System.IntPtr)""
1083  IL_0018:  br.s       IL_001b
1084  IL_001a:  ldloc.0
1085  IL_001b:  call       ""void System.Console.WriteLine(object)""
1086  IL_0020:  brtrue.s   IL_0025
1087  IL_0022:  ldloc.0
1088  IL_0023:  br.s       IL_0031
1089  IL_0025:  ldnull
1090  IL_0026:  ldftn      ""int C.M()""
1091  IL_002c:  newobj     ""System.Func<int>..ctor(object, System.IntPtr)""
1092  IL_0031:  call       ""void System.Console.WriteLine(object)""
1093  IL_0036:  ret
1094}
1095");
1096        }
1097
1098        [Fact]
1099        public void TestConditionalOperatorPreferWider()
1100        {
1101            var source = @"
1102class C
1103{
1104    static void Main()
1105    {
1106        bool b = true;
1107        System.Console.Write(b ? 0 : (short)1);
1108        System.Console.Write(b ? 0 : 1u);
1109    }
1110}";
1111            // NOTE: second call is to Write(uint)
1112            var comp = CompileAndVerify(source, expectedOutput: "00");
1113            comp.VerifyDiagnostics();
1114            comp.VerifyIL("C.Main", @"
1115{
1116  // Code size       25 (0x19)
1117  .maxstack  2
1118  IL_0000:  ldc.i4.1
1119  IL_0001:  dup
1120  IL_0002:  brtrue.s   IL_0007
1121  IL_0004:  ldc.i4.1
1122  IL_0005:  br.s       IL_0008
1123  IL_0007:  ldc.i4.0
1124  IL_0008:  call       ""void System.Console.Write(int)""
1125  IL_000d:  brtrue.s   IL_0012
1126  IL_000f:  ldc.i4.1
1127  IL_0010:  br.s       IL_0013
1128  IL_0012:  ldc.i4.0
1129  IL_0013:  call       ""void System.Console.Write(uint)""
1130  IL_0018:  ret
1131}");
1132        }
1133
1134        /// <summary>
1135        /// This specific code has caused problems in the past.
1136        /// System.Security.VerificationException on the second attempt.
1137        /// </summary>
1138        /// <remarks>
1139        /// No special handling seems to have been required to make this work
1140        /// in Roslyn.
1141        /// </remarks>
1142        [Fact]
1143        public void TestConditionalOperatorInterfaceRegression1()
1144        {
1145            var source = @"
1146using System;
1147using System.Collections.Generic;
1148
1149public class Test
1150{
1151    static void Main()
1152    {
1153        IList<Type> knownTypeList = null;
1154
1155        Console.Write(""first attempt: "");
1156        object o1 = (IList<Type>)(knownTypeList == null ? new Type[0] : knownTypeList);
1157        Console.WriteLine(o1);
1158
1159        Console.Write(""second attempt: "");
1160        IList<Type> o2 = (IList<Type>)(knownTypeList == null ? new Type[0] : knownTypeList);
1161        Console.WriteLine(o2);
1162    }
1163}";
1164            // NOTE: second call is to Write(uint)
1165            var comp = CompileAndVerify(source, expectedOutput: @"
1166first attempt: System.Type[]
1167second attempt: System.Type[]");
1168            comp.VerifyDiagnostics();
1169            comp.VerifyIL("Test.Main", @"
1170{
1171  // Code size       61 (0x3d)
1172  .maxstack  1
1173  .locals init (System.Collections.Generic.IList<System.Type> V_0, //knownTypeList
1174  System.Collections.Generic.IList<System.Type> V_1)
1175  IL_0000:  ldnull
1176  IL_0001:  stloc.0
1177  IL_0002:  ldstr      ""first attempt: ""
1178  IL_0007:  call       ""void System.Console.Write(string)""
1179  IL_000c:  ldloc.0
1180  IL_000d:  brfalse.s  IL_0012
1181  IL_000f:  ldloc.0
1182  IL_0010:  br.s       IL_001a
1183  IL_0012:  ldc.i4.0
1184  IL_0013:  newarr     ""System.Type""
1185  IL_0018:  stloc.1
1186  IL_0019:  ldloc.1
1187  IL_001a:  call       ""void System.Console.WriteLine(object)""
1188  IL_001f:  ldstr      ""second attempt: ""
1189  IL_0024:  call       ""void System.Console.Write(string)""
1190  IL_0029:  ldloc.0
1191  IL_002a:  brfalse.s  IL_002f
1192  IL_002c:  ldloc.0
1193  IL_002d:  br.s       IL_0037
1194  IL_002f:  ldc.i4.0
1195  IL_0030:  newarr     ""System.Type""
1196  IL_0035:  stloc.1
1197  IL_0036:  ldloc.1
1198  IL_0037:  call       ""void System.Console.WriteLine(object)""
1199  IL_003c:  ret
1200}");
1201        }
1202
1203        /// <summary>
1204        /// From orcas bug #42645.  PEVerify fails.
1205        /// </summary>
1206        [Fact]
1207        public void TestConditionalOperatorInterfaceRegression2()
1208        {
1209            var source = @"
1210using System.Collections.Generic;
1211
1212public class Test
1213{
1214    static void Main()
1215    {
1216        int[] a = new int[] { };
1217        List<int> b = new List<int>();
1218
1219        IEnumerable<int> c = a != null ? a : (IEnumerable<int>)b;
1220        System.Console.WriteLine(c);
1221    }
1222}";
1223            // Note the explicit casts, even though the conversions are
1224            // implicit reference conversions.
1225            var comp = CompileAndVerify(source);
1226            comp.VerifyDiagnostics();
1227            comp.VerifyIL("Test.Main", @"
1228{
1229  // Code size       30 (0x1e)
1230  .maxstack  1
1231  .locals init (int[] V_0, //a
1232  System.Collections.Generic.List<int> V_1, //b
1233  System.Collections.Generic.IEnumerable<int> V_2)
1234  IL_0000:  ldc.i4.0
1235  IL_0001:  newarr     ""int""
1236  IL_0006:  stloc.0
1237  IL_0007:  newobj     ""System.Collections.Generic.List<int>..ctor()""
1238  IL_000c:  stloc.1
1239  IL_000d:  ldloc.0
1240  IL_000e:  brtrue.s   IL_0015
1241  IL_0010:  ldloc.1
1242  IL_0011:  stloc.2
1243  IL_0012:  ldloc.2
1244  IL_0013:  br.s       IL_0018
1245  IL_0015:  ldloc.0
1246  IL_0016:  stloc.2
1247  IL_0017:  ldloc.2
1248  IL_0018:  call       ""void System.Console.WriteLine(object)""
1249  IL_001d:  ret
1250}");
1251        }
1252
1253        [Fact]
1254        public void TestConditionalOperatorInterfaceRegression2a()
1255        {
1256            var source = @"
1257using System.Collections.Generic;
1258
1259public class Test
1260{
1261    static void Main()
1262    {
1263        int[] a = new int[] { };
1264        List<int> b = new List<int>();
1265
1266        IEnumerable<int> c = a != null ? a : (IEnumerable<int>)b;
1267    }
1268}";
1269            // Note the explicit casts, even though the conversions are
1270            // implicit reference conversions.
1271            var comp = CompileAndVerify(source);
1272            comp.VerifyDiagnostics();
1273            comp.VerifyIL("Test.Main", @"
1274{
1275  // Code size       16 (0x10)
1276  .maxstack  1
1277  .locals init (int[] V_0, //a
1278  System.Collections.Generic.List<int> V_1) //b
1279  IL_0000:  ldc.i4.0
1280  IL_0001:  newarr     ""int""
1281  IL_0006:  stloc.0
1282  IL_0007:  newobj     ""System.Collections.Generic.List<int>..ctor()""
1283  IL_000c:  stloc.1
1284  IL_000d:  ldloc.0
1285  IL_000e:  pop
1286  IL_000f:  ret
1287}
1288");
1289        }
1290        /// <summary>
1291        /// From whidbey bug #108643.  Extraneous class casts in Test1.m2 (vs Test1.m1).
1292        /// </summary>
1293        [Fact]
1294        public void TestConditionalOperatorInterfaceRegression3()
1295        {
1296            var source = @"
1297interface Base { Base m1();}
1298class Driver { public static int mask = 1;}
1299class Test1 : Base
1300{
1301    Base next;
1302    Base same;
1303    int cnt = 0;
1304    public Test1(Base link)
1305    {
1306        same = this;
1307        next = link;
1308        if (next == null) next = this;
1309    }
1310    public Base m1() { return ((++cnt) & Driver.mask) != 0 ? same : next; } //version1 (explicit impl in original repro)
1311    public Base m2() { return ((++cnt) & Driver.mask) != 0 ? this : next; } //version2 
1312}";
1313            var comp = CompileAndVerify(source);
1314            comp.VerifyDiagnostics();
1315            comp.VerifyIL("Test1.m1", @"
1316{
1317  // Code size       39 (0x27)
1318  .maxstack  3
1319  .locals init (int V_0)
1320  IL_0000:  ldarg.0
1321  IL_0001:  ldarg.0
1322  IL_0002:  ldfld      ""int Test1.cnt""
1323  IL_0007:  ldc.i4.1
1324  IL_0008:  add
1325  IL_0009:  stloc.0
1326  IL_000a:  ldloc.0
1327  IL_000b:  stfld      ""int Test1.cnt""
1328  IL_0010:  ldloc.0
1329  IL_0011:  ldsfld     ""int Driver.mask""
1330  IL_0016:  and
1331  IL_0017:  brtrue.s   IL_0020
1332  IL_0019:  ldarg.0
1333  IL_001a:  ldfld      ""Base Test1.next""
1334  IL_001f:  ret
1335  IL_0020:  ldarg.0
1336  IL_0021:  ldfld      ""Base Test1.same""
1337  IL_0026:  ret
1338}");
1339            // NOTE: no castclass
1340            comp.VerifyIL("Test1.m2", @"
1341{
1342  // Code size       36 (0x24)
1343  .maxstack  3
1344  .locals init (int V_0,
1345  Base V_1)
1346  IL_0000:  ldarg.0
1347  IL_0001:  ldarg.0
1348  IL_0002:  ldfld      ""int Test1.cnt""
1349  IL_0007:  ldc.i4.1
1350  IL_0008:  add
1351  IL_0009:  stloc.0
1352  IL_000a:  ldloc.0
1353  IL_000b:  stfld      ""int Test1.cnt""
1354  IL_0010:  ldloc.0
1355  IL_0011:  ldsfld     ""int Driver.mask""
1356  IL_0016:  and
1357  IL_0017:  brtrue.s   IL_0020
1358  IL_0019:  ldarg.0
1359  IL_001a:  ldfld      ""Base Test1.next""
1360  IL_001f:  ret
1361  IL_0020:  ldarg.0
1362  IL_0021:  stloc.1
1363  IL_0022:  ldloc.1
1364  IL_0023:  ret
1365}");
1366        }
1367
1368        /// <summary>
1369        /// From whidbey bug #49619.  PEVerify fails.
1370        /// </summary>
1371        [Fact]
1372        public void TestConditionalOperatorInterfaceRegression4()
1373        {
1374            var source = @"
1375public interface IA { }
1376public interface IB { int f(); }
1377public class AB1 : IA, IB { public int f() { return 42; } }
1378public class AB2 : IA, IB { public int f() { return 1; } }
1379
1380class MainClass
1381{
1382    public static void g(bool p)
1383    {
1384        (p ? (IB)new AB1() : (IB)new AB2()).f();
1385    }
1386}";
1387            // Note the explicit casts, even though the conversions are
1388            // implicit reference conversions.
1389            // CONSIDER: dev10 writes to/reads from a temp to simulate
1390            // a static cast (instead of using castclass).
1391            var comp = CompileAndVerify(source);
1392            comp.VerifyDiagnostics();
1393            comp.VerifyIL("MainClass.g", @"
1394{
1395  // Code size       26 (0x1a)
1396  .maxstack  1
1397  .locals init (IB V_0)
1398  IL_0000:  ldarg.0
1399  IL_0001:  brtrue.s   IL_000c
1400  IL_0003:  newobj     ""AB2..ctor()""
1401  IL_0008:  stloc.0
1402  IL_0009:  ldloc.0
1403  IL_000a:  br.s       IL_0013
1404  IL_000c:  newobj     ""AB1..ctor()""
1405  IL_0011:  stloc.0
1406  IL_0012:  ldloc.0
1407  IL_0013:  callvirt   ""int IB.f()""
1408  IL_0018:  pop
1409  IL_0019:  ret
1410}");
1411        }
1412
1413        [WorkItem(9229, "DevDiv_Projects/Roslyn")]
1414        [Fact]
1415        public void TestConditionalOperatorForExpression()
1416        {
1417            var source = @"
1418using System.Linq;
1419using System.Collections.Generic;
1420class Program
1421{
1422    static void Main(string[] args)
1423    {
1424        string[] arr = new string[] { ""aaa"", ""bbb"", ""ccc"" };
1425        int[] arr_int = new int[] { 111, 222, 333 };
1426        IEnumerable<string> s = true ? from x in arr select x : from x in arr_int select x.ToString();
1427        foreach (var item in s)
1428        {
1429            System.Console.WriteLine(item);
1430        }
1431    }
1432}";
1433            string expectedOutput = @"aaa
1434bbb
1435ccc";
1436            CompileAndVerify(source, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: expectedOutput);
1437        }
1438
1439        [Fact]
1440        public void TestConditionalOperatorForObjInit()
1441        {
1442            var source = @"
1443using System;
1444class Program
1445{
1446    static void Main(string[] args)
1447    {
1448        Foo f1 = new Foo(), f2 = new Foo(), f3 = new Foo();
1449        bool b = true;
1450        f3 = b ? f1 = new Foo { i = 1 } : f2 = new Foo { i = 2 };
1451        Console.WriteLine(f1.i);
1452        Console.WriteLine(f2.i);
1453        Console.WriteLine(f3.i);
1454        b = false;
1455        f3 = b ? f1 = new Foo { i = 3 } : f2 = new Foo { i = 4 };
1456        Console.WriteLine(f1.i);
1457        Console.WriteLine(f2.i);
1458        Console.WriteLine(f3.i);
1459    }
1460}
1461class Foo
1462{
1463    public int i;
1464}
1465";
1466            string expectedOutput = @"1
14670
14681
14691
14704
14714";
1472            CompileAndVerify(source, expectedOutput: expectedOutput);
1473        }
1474
1475        [Fact]
1476        public void TestConditionalOperatorForObjInit_2()
1477        {
1478            var source = @"
1479using System;
1480class Program
1481{
1482    static void Main(string[] args)
1483    {
1484        bool b1 = true;
1485        bool b2 = false;
1486        Foo f = new Foo
1487        {
1488            i = b1 ? 10 : -10
1489        };
1490        Console.WriteLine(f.i);
1491        f = new Foo
1492        {
1493            i = b2 ? 10 : -10
1494        };
1495        Console.WriteLine(f.i);
1496    }
1497}
1498class Foo
1499{
1500    public int i;
1501}
1502";
1503            string expectedOutput = @"10
1504-10";
1505            CompileAndVerify(source, additionalRefs: new[] { SystemCoreRef }, expectedOutput: expectedOutput);
1506        }
1507
1508        [Fact]
1509        public void TestConditionalOperatorForExpressionTree()
1510        {
1511            var source = @"
1512using System;
1513using System.Linq.Expressions;
1514class Program
1515{
1516    static void Main(string[] args)
1517    {
1518        Expression<Func<bool, long, int, long>> testExpr = (x, y, z) => x ? y : z;
1519        var testFunc = testExpr.Compile();
1520        Console.WriteLine(testFunc(false, (long)3, 100)); //100
1521    }
1522}
1523";
1524            string expectedOutput = @"100";
1525            CompileAndVerify(source, additionalRefs: new [] { SystemCoreRef }, expectedOutput: expectedOutput);
1526        }
1527
1528        [Fact]
1529        public void TestConditionalOperatorForCustomOperator()
1530        {
1531            var source = @"
1532using System;
1533using System.Linq.Expressions;
1534class Program
1535{
1536    static void Main(string[] args)
1537    {
1538        Expression<Func<TestStruct, int?, int, int?>> testExpr = (x, y, z) => x ? y : z;
1539        var testFunc = testExpr.Compile();
1540        Console.WriteLine(testFunc(new TestStruct(), (int?)null, 10)); //10
1541    }
1542}
1543public struct TestStruct
1544{
1545    public static bool operator true(TestStruct ts)
1546    {
1547        return false;
1548    }
1549    public static bool operator false(TestStruct ts)
1550    {
1551        return true;
1552    }
1553}
1554";
1555            string expectedOutput = @"10";
1556            CompileAndVerify(source, additionalRefs: new [] { SystemCoreRef }, expectedOutput: expectedOutput);
1557        }
1558
1559        [Fact]
1560        public void TestConditionalOperatorForMultiCondition()
1561        {
1562            var source = @"
1563class Program
1564{
1565    static void Main(string[] args)
1566    {
1567        System.Console.WriteLine(false ? 1 : true ? 2 : 3);
1568    }
1569}
1570";
1571            string expectedOutput = @"2";
1572            string expectedIL = @"{
1573  // Code size        7 (0x7)
1574  .maxstack  1
1575  IL_0000:  ldc.i4.2  
1576  IL_0001:  call       ""void System.Console.WriteLine(int)""
1577  IL_0006:  ret       
1578}";
1579            CompileAndVerify(source, expectedOutput: expectedOutput).VerifyIL("Program.Main", expectedIL);
1580        }
1581
1582        [WorkItem(528275, "DevDiv")]
1583        [Fact]
1584        public void TestConditionalOperatorForImplicitConv()
1585        {
1586            var source = @"
1587using System.Linq;
1588class Program
1589{
1590    static void Main(string[] args)
1591    {
1592        System.Console.WriteLine(false ? Enumerable.Empty<int>() : new int[] { 1 });
1593    }
1594}
1595";
1596            string expectedOutput = @"System.Int32[]";
1597            string expectedIL = @"{
1598  // Code size       16 (0x10)
1599  .maxstack  4
1600  IL_0000:  ldc.i4.1  
1601  IL_0001:  newarr     ""int""
1602  IL_0006:  dup       
1603  IL_0007:  ldc.i4.0  
1604  IL_0008:  ldc.i4.1  
1605  IL_0009:  stelem.i4 
1606  IL_000a:  call       ""void System.Console.WriteLine(object)""
1607  IL_000f:  ret       
1608}";
1609            MetadataReference[] metadataRef = new[] { LinqAssemblyRef };
1610
1611            // Dev11 compiler reports WRN_UnreachableExpr, but reachability is defined for statements not for expressions.
1612            // We don't report the warning.
1613            CompileAndVerify(source, additionalRefs: metadataRef, expectedOutput: expectedOutput).VerifyIL("Program.Main", expectedIL).VerifyDiagnostics();
1614        }
1615
1616        [Fact]
1617        public void TestConditionalOperatorForConversion()
1618        {
1619            var source = @"
1620using System;
1621class Program
1622{
1623    static void Main(string[] args)
1624    {
1625        object valueFromDatabase;
1626        decimal result;
1627        valueFromDatabase = DBNull.Value;
1628        result = (valueFromDatabase != DBNull.Value ? (decimal)valueFromDatabase : (decimal)0);
1629        System.Console.WriteLine(result);
1630        result = (decimal)(valueFromDatabase != DBNull.Value ? valueFromDatabase : 0); //Runtime exception
1631        System.Console.WriteLine(result);
1632    }
1633}
1634";
1635            string expectedIL = @"
1636{
1637  // Code size       60 (0x3c)
1638  .maxstack  2
1639  .locals init (object V_0) //valueFromDatabase
1640  IL_0000:  ldsfld     ""System.DBNull System.DBNull.Value""
1641  IL_0005:  stloc.0
1642  IL_0006:  ldloc.0
1643  IL_0007:  ldsfld     ""System.DBNull System.DBNull.Value""
1644  IL_000c:  bne.un.s   IL_0015
1645  IL_000e:  ldsfld     ""decimal decimal.Zero""
1646  IL_0013:  br.s       IL_001b
1647  IL_0015:  ldloc.0
1648  IL_0016:  unbox.any  ""decimal""
1649  IL_001b:  call       ""void System.Console.WriteLine(decimal)""
1650  IL_0020:  ldloc.0
1651  IL_0021:  ldsfld     ""System.DBNull System.DBNull.Value""
1652  IL_0026:  bne.un.s   IL_0030
1653  IL_0028:  ldc.i4.0
1654  IL_0029:  box        ""int""
1655  IL_002e:  br.s       IL_0031
1656  IL_0030:  ldloc.0
1657  IL_0031:  unbox.any  ""decimal""
1658  IL_0036:  call       ""void System.Console.WriteLine(decimal)""
1659  IL_003b:  ret
1660}";
1661            CompileAndVerify(source, additionalRefs: new[] { SystemCoreRef }).VerifyIL("Program.Main", expectedIL);
1662        }
1663
1664        [Fact, WorkItem(530071, "DevDiv")]
1665        public void TestConditionalOperatorForImplicitlyTypedArrays()
1666        {
1667            var source = @"
1668using System;
1669class Program
1670{
1671    static void Main(string[] args)
1672    {
1673        int[] a, a1, a2;
1674        a = a1 = a2 = null;
1675        a = true ? a1 = new[] { 1, 2, 3 } : a2 = new[] { 4, 5, 6 };
1676        foreach (int item in a1)
1677        { Console.WriteLine(item); }
1678    }
1679}
1680";
1681            string expectedOutput = @"1
16822
16833";
1684            // Dev11 compiler reports WRN_UnreachableExpr, but reachability is defined for statements not for expressions.
1685            // We don't report the warning.
1686            CompileAndVerify(source, additionalRefs: new[] { SystemCoreRef }, expectedOutput: expectedOutput).
1687                VerifyDiagnostics();
1688        }
1689
1690        [Fact]
1691        public void TestConditionalOperatorForConst()
1692        {
1693            var source = @"
1694class Program
1695{
1696    const bool con = true;
1697    static int Main(string[] args)
1698    {
1699        int s1 = con != true ? 1 : 2;
1700        return s1;
1701    }
1702}
1703";
1704            string expectedIL = @"{
1705  // Code size        2 (0x2)
1706  .maxstack  1
1707  IL_0000:  ldc.i4.2
1708  IL_0001:  ret
1709}";
1710            CompileAndVerify(source).VerifyIL("Program.Main", expectedIL);
1711        }
1712
1713        [Fact]
1714        public void TestConditionalOperatorForStatic()
1715        {
1716            var source = @"
1717class Program
1718{
1719    static bool con = true;
1720    static int Main(string[] args)
1721    {
1722        int s1 = con != true ? 1 : 2;
1723        return s1;
1724    }
1725}
1726";
1727            string expectedIL = @"{
1728  // Code size       11 (0xb)
1729  .maxstack  1
1730  IL_0000:  ldsfld     ""bool Program.con""
1731  IL_0005:  brfalse.s  IL_0009
1732  IL_0007:  ldc.i4.2
1733  IL_0008:  ret
1734  IL_0009:  ldc.i4.1
1735  IL_000a:  ret
1736}";
1737            CompileAndVerify(source).VerifyIL("Program.Main", expectedIL);
1738        }
1739
1740        [Fact]
1741        public void TestConditionalOperatorForGenericMethod()
1742        {
1743            var source = @"
1744class Program
1745{
1746    static void Main(string[] args)
1747    {
1748        int x = 1;
1749        object y = 0;
1750        System.Console.WriteLine(true ? fun(x) : fun(y));
1751    }
1752    private static T fun<T>(T t)
1753    {
1754        return t;
1755    }
1756}
1757";
1758            string expectedOutput = @"1";
1759            string expectedIL = @"{
1760  // Code size       17 (0x11)
1761  .maxstack  1
1762  IL_0000:  ldc.i4.1
1763  IL_0001:  call       ""int Program.fun<int>(int)""
1764  IL_0006:  box        ""int""
1765  IL_000b:  call       ""void System.Console.WriteLine(object)""
1766  IL_0010:  ret
1767}";
1768            CompileAndVerify(source, expectedOutput: expectedOutput).VerifyIL("Program.Main", expectedIL);
1769        }
1770
1771        [Fact]
1772        public void TestConditionalOperatorForGenericMethod2()
1773        {
1774            var source = @"
1775class Program
1776{
1777    static void Main(string[] args)
1778    {
1779        int x = 1;
1780        object y = 0;
1781        System.Console.WriteLine(y != null ? fun(x) : fun(y));
1782    }
1783    private static T fun<T>(T t)
1784    {
1785        return t;
1786    }
1787}
1788";
1789            string expectedOutput = @"1";
1790            string expectedIL = @"
1791{
1792  // Code size       37 (0x25)
1793  .maxstack  1
1794  .locals init (int V_0, //x
1795  object V_1) //y
1796  IL_0000:  ldc.i4.1
1797  IL_0001:  stloc.0
1798  IL_0002:  ldc.i4.0
1799  IL_0003:  box        ""int""
1800  IL_0008:  stloc.1
1801  IL_0009:  ldloc.1
1802  IL_000a:  brtrue.s   IL_0014
1803  IL_000c:  ldloc.1
1804  IL_000d:  call       ""object Program.fun<object>(object)""
1805  IL_0012:  br.s       IL_001f
1806  IL_0014:  ldloc.0
1807  IL_0015:  call       ""int Program.fun<int>(int)""
1808  IL_001a:  box        ""int""
1809  IL_001f:  call       ""void System.Console.WriteLine(object)""
1810  IL_0024:  ret
1811}
1812";
1813            CompileAndVerify(source, expectedOutput: expectedOutput).VerifyIL("Program.Main", expectedIL);
1814        }
1815
1816        [Fact]
1817        public void TestConditionalOperatorForParenthesizedExpression()
1818        {
1819            var source = @"
1820class Program
1821{
1822    static void Main(string[] args)
1823    {
1824        int x = 1;
1825        int y = 1;
1826        System.Console.WriteLine(((x == y)) ? (++x) : ((((++y))))); 	// OK
1827    }
1828}
1829";
1830            string expectedOutput = @"2";
1831            CompileAndVerify(source, expectedOutput: expectedOutput);
1832        }
1833
1834        [Fact]
1835        public void TestConditionalOpera

Large files files are truncated, but you can click here to view the full file