forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatternTests.cs
6913 lines (6612 loc) · 210 KB
/
PatternTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
{
public class PatternTests : EmitMetadataTestBase
{
#region Miscellaneous
[Fact, WorkItem(48493, "https://github.com/dotnet/roslyn/issues/48493")]
public void Repro_48493()
{
var source = @"
using System;
using System.Linq;
namespace Sample
{
internal class Row
{
public string Message { get; set; } = """";
}
internal class Program
{
private static void Main()
{
Console.WriteLine(ProcessRow(new Row()));
}
private static string ProcessRow(Row row)
{
if (row == null) throw new ArgumentNullException(nameof(row));
return row switch
{
{ Message: ""stringA"" } => ""stringB"",
var r when new[] { ""stringC"", ""stringD"" }.Any(x => r.Message.Contains(x)) => ""stringE"",
{ Message: ""stringF"" } => ""stringG"",
_ => ""stringH"",
};
}
}
}";
CompileAndVerify(source, expectedOutput: "stringH");
}
[Fact, WorkItem(48493, "https://github.com/dotnet/roslyn/issues/48493")]
public void Repro_48493_Simple()
{
var source = @"
using System;
internal class Widget
{
public bool IsGood { get; set; }
}
internal class Program
{
private static bool M0(Func<bool> fn) => fn();
private static void Main()
{
Console.Write(new Widget() switch
{
{ IsGood: true } => 1,
_ when M0(() => true) => 2,
{ } => 3,
});
}
}";
CompileAndVerify(source, expectedOutput: @"2");
}
[Fact, WorkItem(18811, "https://github.com/dotnet/roslyn/issues/18811")]
public void MissingNullable_01()
{
var source = @"namespace System {
public class Object { }
public abstract class ValueType { }
public struct Void { }
public struct Boolean { }
public struct Int32 { }
}
static class C {
public static bool M() => ((object)123) is int i;
}
";
var compilation = CreateEmptyCompilation(source, parseOptions: TestOptions.Regular.WithNoRefSafetyRulesAttribute(), options: TestOptions.ReleaseDll);
compilation.GetDiagnostics().Verify();
compilation.GetEmitDiagnostics().Verify(
// warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1)
);
}
[Fact, WorkItem(18811, "https://github.com/dotnet/roslyn/issues/18811")]
public void MissingNullable_02()
{
var source = @"namespace System {
public class Object { }
public abstract class ValueType { }
public struct Void { }
public struct Boolean { }
public struct Int32 { }
public struct Nullable<T> where T : struct { }
}
static class C {
public static bool M() => ((object)123) is int i;
}
";
var compilation = CreateEmptyCompilation(source, parseOptions: TestOptions.Regular.WithNoRefSafetyRulesAttribute(), options: TestOptions.UnsafeReleaseDll);
compilation.GetDiagnostics().Verify();
compilation.GetEmitDiagnostics().Verify(
// warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion)
);
}
[Fact]
public void MissingNullable_03()
{
var source = @"namespace System {
public class Object { }
public abstract class ValueType { }
public struct Void { }
public struct Boolean { }
public struct Int32 { }
public struct Nullable<T> where T : struct { }
}
static class C {
static void M1(int? x)
{
switch (x)
{
case int i: break;
}
}
static bool M2(int? x) => x is int i;
}
";
var compilation = CreateEmptyCompilation(source, parseOptions: TestOptions.Regular.WithNoRefSafetyRulesAttribute(), options: TestOptions.UnsafeReleaseDll);
compilation.GetDiagnostics().Verify();
compilation.GetEmitDiagnostics().Verify(
// warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1),
// (14,18): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
// case int i: break;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(14, 18),
// (14,18): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
// case int i: break;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(14, 18),
// (14,18): error CS0656: Missing compiler required member 'System.Nullable`1.get_Value'
// case int i: break;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_Value").WithLocation(14, 18),
// (17,36): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
// static bool M2(int? x) => x is int i;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(17, 36),
// (17,36): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
// static bool M2(int? x) => x is int i;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(17, 36),
// (17,36): error CS0656: Missing compiler required member 'System.Nullable`1.get_Value'
// static bool M2(int? x) => x is int i;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_Value").WithLocation(17, 36)
);
}
[Fact]
public void MissingNullable_04()
{
var source = @"namespace System {
public class Object { }
public abstract class ValueType { }
public struct Void { }
public struct Boolean { }
public struct Int32 { }
public struct Nullable<T> where T : struct { public T GetValueOrDefault() => default(T); }
}
static class C {
static void M1(int? x)
{
switch (x)
{
case int i: break;
}
}
static bool M2(int? x) => x is int i;
}
";
var compilation = CreateEmptyCompilation(source, parseOptions: TestOptions.Regular.WithNoRefSafetyRulesAttribute(), options: TestOptions.UnsafeReleaseDll);
compilation.GetDiagnostics().Verify();
compilation.GetEmitDiagnostics().Verify(
// warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1),
// (14,18): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
// case int i: break;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(14, 18),
// (17,36): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
// static bool M2(int? x) => x is int i;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int i").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(17, 36)
);
}
[Fact, WorkItem(17266, "https://github.com/dotnet/roslyn/issues/17266")]
public void DoubleEvaluation01()
{
var source =
@"using System;
public class C
{
public static void Main()
{
if (TryGet() is int index)
{
Console.WriteLine(index);
}
}
public static int? TryGet()
{
Console.WriteLine(""eval"");
return null;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
compilation.VerifyDiagnostics();
var expectedOutput = @"eval";
var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
compVerifier.VerifyIL("C.Main",
@"{
// Code size 42 (0x2a)
.maxstack 1
.locals init (int V_0, //index
bool V_1,
int? V_2)
IL_0000: nop
IL_0001: call ""int? C.TryGet()""
IL_0006: stloc.2
IL_0007: ldloca.s V_2
IL_0009: call ""bool int?.HasValue.get""
IL_000e: brfalse.s IL_001b
IL_0010: ldloca.s V_2
IL_0012: call ""int int?.GetValueOrDefault()""
IL_0017: stloc.0
IL_0018: ldc.i4.1
IL_0019: br.s IL_001c
IL_001b: ldc.i4.0
IL_001c: stloc.1
IL_001d: ldloc.1
IL_001e: brfalse.s IL_0029
IL_0020: nop
IL_0021: ldloc.0
IL_0022: call ""void System.Console.WriteLine(int)""
IL_0027: nop
IL_0028: nop
IL_0029: ret
}");
compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
compilation.VerifyDiagnostics();
compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
compVerifier.VerifyIL("C.Main",
@"{
// Code size 30 (0x1e)
.maxstack 1
.locals init (int V_0, //index
int? V_1)
IL_0000: call ""int? C.TryGet()""
IL_0005: stloc.1
IL_0006: ldloca.s V_1
IL_0008: call ""bool int?.HasValue.get""
IL_000d: brfalse.s IL_001d
IL_000f: ldloca.s V_1
IL_0011: call ""int int?.GetValueOrDefault()""
IL_0016: stloc.0
IL_0017: ldloc.0
IL_0018: call ""void System.Console.WriteLine(int)""
IL_001d: ret
}");
}
[Fact, WorkItem(19122, "https://github.com/dotnet/roslyn/issues/19122")]
public void PatternCrash_01()
{
var source = @"using System;
using System.Collections.Generic;
using System.Linq;
public class Class2 : IDisposable
{
public Class2(bool parameter = false)
{
}
public void Dispose()
{
}
}
class X<T>
{
IdentityAccessor<T> idAccessor = new IdentityAccessor<T>();
void Y<U>() where U : T
{
// BUG: The following line is the problem
if (GetT().FirstOrDefault(p => idAccessor.GetId(p) == Guid.Empty) is U u)
{
}
}
IEnumerable<T> GetT()
{
yield return default(T);
}
}
class IdentityAccessor<T>
{
public Guid GetId(T t)
{
return Guid.Empty;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.DebugDll);
compilation.VerifyDiagnostics();
var compVerifier = CompileAndVerify(compilation);
compVerifier.VerifyIL("X<T>.Y<U>",
@"{
// Code size 61 (0x3d)
.maxstack 3
.locals init (U V_0, //u
bool V_1,
T V_2)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call ""System.Collections.Generic.IEnumerable<T> X<T>.GetT()""
IL_0007: ldarg.0
IL_0008: ldftn ""bool X<T>.<Y>b__1_0<U>(T)""
IL_000e: newobj ""System.Func<T, bool>..ctor(object, System.IntPtr)""
IL_0013: call ""T System.Linq.Enumerable.FirstOrDefault<T>(System.Collections.Generic.IEnumerable<T>, System.Func<T, bool>)""
IL_0018: stloc.2
IL_0019: ldloc.2
IL_001a: box ""T""
IL_001f: isinst ""U""
IL_0024: brfalse.s IL_0035
IL_0026: ldloc.2
IL_0027: box ""T""
IL_002c: unbox.any ""U""
IL_0031: stloc.0
IL_0032: ldc.i4.1
IL_0033: br.s IL_0036
IL_0035: ldc.i4.0
IL_0036: stloc.1
IL_0037: ldloc.1
IL_0038: brfalse.s IL_003c
IL_003a: nop
IL_003b: nop
IL_003c: ret
}");
}
[Fact, WorkItem(24522, "https://github.com/dotnet/roslyn/issues/24522")]
public void IgnoreDeclaredConversion_01()
{
var source =
@"class Base<T>
{
public static implicit operator Derived(Base<T> obj)
{
return new Derived();
}
}
class Derived : Base<object>
{
}
class Program
{
static void Main(string[] args)
{
Base<object> x = new Derived();
System.Console.WriteLine(x is Derived);
System.Console.WriteLine(x is Derived y);
switch (x)
{
case Derived z: System.Console.WriteLine(true); break;
}
System.Console.WriteLine(null != (x as Derived));
}
}";
var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
compilation.VerifyDiagnostics();
var expectedOutput =
@"True
True
True
True";
var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
compVerifier.VerifyIL("Program.Main",
@"{
// Code size 84 (0x54)
.maxstack 2
.locals init (Base<object> V_0, //x
Derived V_1, //y
Derived V_2, //z
Base<object> V_3,
Base<object> V_4)
IL_0000: nop
IL_0001: newobj ""Derived..ctor()""
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: isinst ""Derived""
IL_000d: ldnull
IL_000e: cgt.un
IL_0010: call ""void System.Console.WriteLine(bool)""
IL_0015: nop
IL_0016: ldloc.0
IL_0017: isinst ""Derived""
IL_001c: stloc.1
IL_001d: ldloc.1
IL_001e: ldnull
IL_001f: cgt.un
IL_0021: call ""void System.Console.WriteLine(bool)""
IL_0026: nop
IL_0027: ldloc.0
IL_0028: stloc.s V_4
IL_002a: ldloc.s V_4
IL_002c: stloc.3
IL_002d: ldloc.3
IL_002e: isinst ""Derived""
IL_0033: stloc.2
IL_0034: ldloc.2
IL_0035: brtrue.s IL_0039
IL_0037: br.s IL_0044
IL_0039: br.s IL_003b
IL_003b: ldc.i4.1
IL_003c: call ""void System.Console.WriteLine(bool)""
IL_0041: nop
IL_0042: br.s IL_0044
IL_0044: ldloc.0
IL_0045: isinst ""Derived""
IL_004a: ldnull
IL_004b: cgt.un
IL_004d: call ""void System.Console.WriteLine(bool)""
IL_0052: nop
IL_0053: ret
}");
}
[Fact]
public void DoublePattern01()
{
var source =
@"using System;
class Program
{
static bool P1(double d) => d is double.NaN;
static bool P2(float f) => f is float.NaN;
static bool P3(double d) => d is 3.14d;
static bool P4(float f) => f is 3.14f;
static bool P5(object o)
{
switch (o)
{
case double.NaN: return true;
case float.NaN: return true;
case 3.14d: return true;
case 3.14f: return true;
default: return false;
}
}
public static void Main(string[] args)
{
Console.Write(P1(double.NaN));
Console.Write(P1(1.0));
Console.Write(P2(float.NaN));
Console.Write(P2(1.0f));
Console.Write(P3(3.14));
Console.Write(P3(double.NaN));
Console.Write(P4(3.14f));
Console.Write(P4(float.NaN));
Console.Write(P5(double.NaN));
Console.Write(P5(0.0d));
Console.Write(P5(float.NaN));
Console.Write(P5(0.0f));
Console.Write(P5(3.14d));
Console.Write(P5(125));
Console.Write(P5(3.14f));
Console.Write(P5(1.0f));
}
}";
var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
compilation.VerifyDiagnostics();
var expectedOutput = @"TrueFalseTrueFalseTrueFalseTrueFalseTrueFalseTrueFalseTrueFalseTrueFalse";
var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
compVerifier.VerifyIL("Program.P1",
@"{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call ""bool double.IsNaN(double)""
IL_0006: ret
}");
compVerifier.VerifyIL("Program.P2",
@"{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call ""bool float.IsNaN(float)""
IL_0006: ret
}");
compVerifier.VerifyIL("Program.P3",
@"{
// Code size 13 (0xd)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldc.r8 3.14
IL_000a: ceq
IL_000c: ret
}");
compVerifier.VerifyIL("Program.P4",
@"{
// Code size 9 (0x9)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldc.r4 3.14
IL_0006: ceq
IL_0008: ret
}");
compVerifier.VerifyIL("Program.P5",
@"{
// Code size 103 (0x67)
.maxstack 2
.locals init (object V_0,
double V_1,
float V_2,
object V_3,
bool V_4)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: stloc.3
IL_0003: ldloc.3
IL_0004: stloc.0
IL_0005: ldloc.0
IL_0006: isinst ""double""
IL_000b: brfalse.s IL_002a
IL_000d: ldloc.0
IL_000e: unbox.any ""double""
IL_0013: stloc.1
IL_0014: ldloc.1
IL_0015: call ""bool double.IsNaN(double)""
IL_001a: brtrue.s IL_004b
IL_001c: ldloc.1
IL_001d: ldc.r8 3.14
IL_0026: beq.s IL_0055
IL_0028: br.s IL_005f
IL_002a: ldloc.0
IL_002b: isinst ""float""
IL_0030: brfalse.s IL_005f
IL_0032: ldloc.0
IL_0033: unbox.any ""float""
IL_0038: stloc.2
IL_0039: ldloc.2
IL_003a: call ""bool float.IsNaN(float)""
IL_003f: brtrue.s IL_0050
IL_0041: ldloc.2
IL_0042: ldc.r4 3.14
IL_0047: beq.s IL_005a
IL_0049: br.s IL_005f
IL_004b: ldc.i4.1
IL_004c: stloc.s V_4
IL_004e: br.s IL_0064
IL_0050: ldc.i4.1
IL_0051: stloc.s V_4
IL_0053: br.s IL_0064
IL_0055: ldc.i4.1
IL_0056: stloc.s V_4
IL_0058: br.s IL_0064
IL_005a: ldc.i4.1
IL_005b: stloc.s V_4
IL_005d: br.s IL_0064
IL_005f: ldc.i4.0
IL_0060: stloc.s V_4
IL_0062: br.s IL_0064
IL_0064: ldloc.s V_4
IL_0066: ret
}");
}
[Fact]
public void DecimalEquality()
{
// demonstrate that pattern-matching against a decimal constant is
// at least as efficient as simply using ==
var source =
@"using System;
public class C
{
public static void Main()
{
Console.Write(M1(1.0m));
Console.Write(M2(1.0m));
Console.Write(M1(2.0m));
Console.Write(M2(2.0m));
}
static int M1(decimal d)
{
if (M(d) is 1.0m) return 1;
return 0;
}
static int M2(decimal d)
{
if (M(d) == 1.0m) return 1;
return 0;
}
public static decimal M(decimal d)
{
return d;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.DebugExe);
compilation.VerifyDiagnostics();
var expectedOutput = @"1100";
var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
compVerifier.VerifyIL("C.M1",
@"{
// Code size 37 (0x25)
.maxstack 6
.locals init (bool V_0,
int V_1)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call ""decimal C.M(decimal)""
IL_0007: ldc.i4.s 10
IL_0009: ldc.i4.0
IL_000a: ldc.i4.0
IL_000b: ldc.i4.0
IL_000c: ldc.i4.1
IL_000d: newobj ""decimal..ctor(int, int, int, bool, byte)""
IL_0012: call ""bool decimal.op_Equality(decimal, decimal)""
IL_0017: stloc.0
IL_0018: ldloc.0
IL_0019: brfalse.s IL_001f
IL_001b: ldc.i4.1
IL_001c: stloc.1
IL_001d: br.s IL_0023
IL_001f: ldc.i4.0
IL_0020: stloc.1
IL_0021: br.s IL_0023
IL_0023: ldloc.1
IL_0024: ret
}");
compVerifier.VerifyIL("C.M2",
@"{
// Code size 37 (0x25)
.maxstack 6
.locals init (bool V_0,
int V_1)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call ""decimal C.M(decimal)""
IL_0007: ldc.i4.s 10
IL_0009: ldc.i4.0
IL_000a: ldc.i4.0
IL_000b: ldc.i4.0
IL_000c: ldc.i4.1
IL_000d: newobj ""decimal..ctor(int, int, int, bool, byte)""
IL_0012: call ""bool decimal.op_Equality(decimal, decimal)""
IL_0017: stloc.0
IL_0018: ldloc.0
IL_0019: brfalse.s IL_001f
IL_001b: ldc.i4.1
IL_001c: stloc.1
IL_001d: br.s IL_0023
IL_001f: ldc.i4.0
IL_0020: stloc.1
IL_0021: br.s IL_0023
IL_0023: ldloc.1
IL_0024: ret
}");
compilation = CreateCompilation(source, options: TestOptions.ReleaseExe);
compilation.VerifyDiagnostics();
compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput);
compVerifier.VerifyIL("C.M1",
@"{
// Code size 28 (0x1c)
.maxstack 6
IL_0000: ldarg.0
IL_0001: call ""decimal C.M(decimal)""
IL_0006: ldc.i4.s 10
IL_0008: ldc.i4.0
IL_0009: ldc.i4.0
IL_000a: ldc.i4.0
IL_000b: ldc.i4.1
IL_000c: newobj ""decimal..ctor(int, int, int, bool, byte)""
IL_0011: call ""bool decimal.op_Equality(decimal, decimal)""
IL_0016: brfalse.s IL_001a
IL_0018: ldc.i4.1
IL_0019: ret
IL_001a: ldc.i4.0
IL_001b: ret
}");
compVerifier.VerifyIL("C.M2",
@"{
// Code size 28 (0x1c)
.maxstack 6
IL_0000: ldarg.0
IL_0001: call ""decimal C.M(decimal)""
IL_0006: ldc.i4.s 10
IL_0008: ldc.i4.0
IL_0009: ldc.i4.0
IL_000a: ldc.i4.0
IL_000b: ldc.i4.1
IL_000c: newobj ""decimal..ctor(int, int, int, bool, byte)""
IL_0011: call ""bool decimal.op_Equality(decimal, decimal)""
IL_0016: brfalse.s IL_001a
IL_0018: ldc.i4.1
IL_0019: ret
IL_001a: ldc.i4.0
IL_001b: ret
}");
}
[Fact, WorkItem(16878, "https://github.com/dotnet/roslyn/issues/16878")]
public void RedundantNullCheck()
{
var source =
@"public class C
{
static int M1(bool? b1, bool b2)
{
switch (b1)
{
case null:
return 1;
case var _ when b2:
return 2;
case true:
return 3;
case false:
return 4;
}
}
static int M2(object o, bool b)
{
switch (o)
{
case string a when b:
return 1;
case string a:
return 2;
}
return 3;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyDiagnostics();
var compVerifier = CompileAndVerify(compilation);
compVerifier.VerifyIL("C.M1",
@"{
// Code size 35 (0x23)
.maxstack 1
.locals init (bool? V_0)
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldloca.s V_0
IL_0004: call ""bool bool?.HasValue.get""
IL_0009: brfalse.s IL_0018
IL_000b: br.s IL_001a
IL_000d: ldloca.s V_0
IL_000f: call ""bool bool?.GetValueOrDefault()""
IL_0014: brtrue.s IL_001f
IL_0016: br.s IL_0021
IL_0018: ldc.i4.1
IL_0019: ret
IL_001a: ldarg.1
IL_001b: brfalse.s IL_000d
IL_001d: ldc.i4.2
IL_001e: ret
IL_001f: ldc.i4.3
IL_0020: ret
IL_0021: ldc.i4.4
IL_0022: ret
}");
compVerifier.VerifyIL("C.M2",
@"{
// Code size 19 (0x13)
.maxstack 1
.locals init (string V_0) //a
IL_0000: ldarg.0
IL_0001: isinst ""string""
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: brfalse.s IL_0011
IL_000a: ldarg.1
IL_000b: brfalse.s IL_000f
IL_000d: ldc.i4.1
IL_000e: ret
IL_000f: ldc.i4.2
IL_0010: ret
IL_0011: ldc.i4.3
IL_0012: ret
}");
}
[Fact, WorkItem(12813, "https://github.com/dotnet/roslyn/issues/12813")]
public void NoBoxingOnIntegerConstantPattern()
{
var source =
@"public class C
{
static bool M1(int x)
{
return x is 42;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyDiagnostics();
var compVerifier = CompileAndVerify(compilation);
compVerifier.VerifyIL("C.M1",
@"{
// Code size 6 (0x6)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldc.i4.s 42
IL_0003: ceq
IL_0005: ret
}");
}
[Fact, WorkItem(40403, "https://github.com/dotnet/roslyn/issues/40403")]
public void RefParameter_StoreToTemp()
{
var source =
@"public class C
{
static bool M1(ref object x)
{
return x is 42;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyDiagnostics();
var compVerifier = CompileAndVerify(compilation);
compVerifier.VerifyIL("C.M1",
@"{
// Code size 24 (0x18)
.maxstack 2
.locals init (object V_0)
IL_0000: ldarg.0
IL_0001: ldind.ref
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: isinst ""int""
IL_0009: brfalse.s IL_0016
IL_000b: ldloc.0
IL_000c: unbox.any ""int""
IL_0011: ldc.i4.s 42
IL_0013: ceq
IL_0015: ret
IL_0016: ldc.i4.0
IL_0017: ret
}");
}
[Fact, WorkItem(40403, "https://github.com/dotnet/roslyn/issues/40403")]
public void RefLocal_StoreToTemp()
{
var source =
@"public class C
{
static bool M1(bool b, ref object x, ref object y)
{
ref object z = ref b ? ref x : ref y;
return z is 42;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyDiagnostics();
var compVerifier = CompileAndVerify(compilation);
compVerifier.VerifyIL("C.M1",
@"{
// Code size 30 (0x1e)
.maxstack 2
.locals init (object V_0)
IL_0000: ldarg.0
IL_0001: brtrue.s IL_0006
IL_0003: ldarg.2
IL_0004: br.s IL_0007
IL_0006: ldarg.1
IL_0007: ldind.ref
IL_0008: stloc.0
IL_0009: ldloc.0
IL_000a: isinst ""int""
IL_000f: brfalse.s IL_001c
IL_0011: ldloc.0
IL_0012: unbox.any ""int""
IL_0017: ldc.i4.s 42
IL_0019: ceq
IL_001b: ret
IL_001c: ldc.i4.0
IL_001d: ret
}");
}
[Fact, WorkItem(40403, "https://github.com/dotnet/roslyn/issues/40403")]
public void InParameter_StoreToTemp()
{
var source =
@"public class C
{
static bool M1(in object x)
{
return x is 42;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyDiagnostics();
var compVerifier = CompileAndVerify(compilation);
compVerifier.VerifyIL("C.M1",
@"{
// Code size 24 (0x18)
.maxstack 2
.locals init (object V_0)
IL_0000: ldarg.0
IL_0001: ldind.ref
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: isinst ""int""
IL_0009: brfalse.s IL_0016
IL_000b: ldloc.0
IL_000c: unbox.any ""int""
IL_0011: ldc.i4.s 42
IL_0013: ceq
IL_0015: ret
IL_0016: ldc.i4.0
IL_0017: ret
}");
}
[Fact, WorkItem(40403, "https://github.com/dotnet/roslyn/issues/40403")]
public void RefReadonlyLocal_StoreToTemp()
{
var source =
@"public class C
{
static bool M1(bool b, in object x, in object y)
{
ref readonly object z = ref b ? ref x : ref y;
return z is 42;
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyDiagnostics();
var compVerifier = CompileAndVerify(compilation);
compVerifier.VerifyIL("C.M1",
@"{
// Code size 30 (0x1e)
.maxstack 2
.locals init (object V_0)
IL_0000: ldarg.0
IL_0001: brtrue.s IL_0006
IL_0003: ldarg.2
IL_0004: br.s IL_0007
IL_0006: ldarg.1
IL_0007: ldind.ref
IL_0008: stloc.0
IL_0009: ldloc.0
IL_000a: isinst ""int""
IL_000f: brfalse.s IL_001c
IL_0011: ldloc.0
IL_0012: unbox.any ""int""
IL_0017: ldc.i4.s 42
IL_0019: ceq
IL_001b: ret
IL_001c: ldc.i4.0
IL_001d: ret
}");
}
[Fact, WorkItem(40403, "https://github.com/dotnet/roslyn/issues/40403")]
public void OutParameter_StoreToTemp()
{
var source =
@"public class C