-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathCodeGenAwaitForeachTests.cs
9487 lines (8996 loc) · 339 KB
/
CodeGenAwaitForeachTests.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
{
[CompilerTrait(CompilerFeature.AsyncStreams)]
public class CodeGenAwaitForeachTests : EmitMetadataTestBase
{
[Fact]
public void TestWithCSharp7_3()
{
string source = @"
using System.Collections.Generic;
class C : IAsyncEnumerable<int>
{
public static async System.Threading.Tasks.Task Main()
{
await foreach (int i in new C())
{
}
}
IAsyncEnumerator<int> IAsyncEnumerable<int>.GetAsyncEnumerator(System.Threading.CancellationToken token)
=> throw null;
}";
var expected = new[]
{
// (7,9): error CS8652: The feature 'async streams' is not available in C# 7.3. Please use language version 8.0 or greater.
// await foreach (int i in new C())
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_3, "await").WithArguments("async streams", "8.0").WithLocation(7, 9)
};
var comp = CreateCompilationWithTasksExtensions(new[] { source, s_IAsyncEnumerable }, parseOptions: TestOptions.Regular7_3);
comp.VerifyDiagnostics(expected);
comp = CreateCompilationWithTasksExtensions(new[] { source, s_IAsyncEnumerable }, parseOptions: TestOptions.Regular8);
comp.VerifyDiagnostics();
}
[Fact]
public void TestDeconstructionWithCSharp7_3()
{
string source = @"
using System.Collections.Generic;
class C : IAsyncEnumerable<(int, int)>
{
public static async System.Threading.Tasks.Task Main()
{
await foreach (var (i, j) in new C())
{
}
}
IAsyncEnumerator<(int, int)> IAsyncEnumerable<(int, int)>.GetAsyncEnumerator(System.Threading.CancellationToken token)
=> throw null;
}";
var expected = new[]
{
// (7,9): error CS8652: The feature 'async streams' is not available in C# 7.3. Please use language version 8.0 or greater.
// await foreach (int i in new C())
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_3, "await").WithArguments("async streams", "8.0").WithLocation(7, 9)
};
var comp = CreateCompilationWithTasksExtensions(new[] { source, s_IAsyncEnumerable }, parseOptions: TestOptions.Regular7_3);
comp.VerifyDiagnostics(expected);
comp = CreateCompilationWithTasksExtensions(new[] { source, s_IAsyncEnumerable }, parseOptions: TestOptions.Regular8);
comp.VerifyDiagnostics();
}
[Fact]
public void TestWithMissingValueTask()
{
string lib_cs = @"
using System.Collections.Generic;
public class C : IAsyncEnumerable<int>
{
IAsyncEnumerator<int> IAsyncEnumerable<int>.GetAsyncEnumerator(System.Threading.CancellationToken token)
=> throw null;
}";
var lib = CreateCompilationWithTasksExtensions(new[] { lib_cs, s_IAsyncEnumerable });
lib.VerifyDiagnostics();
string source = @"
class D
{
public static async System.Threading.Tasks.Task Main()
{
await foreach (int i in new C()) { }
}
}
";
var comp = CreateCompilationWithTasksExtensions(source, references: new[] { lib.EmitToImageReference() });
comp.MakeTypeMissing(WellKnownType.System_Threading_Tasks_ValueTask);
comp.VerifyDiagnostics(
// (6,9): error CS0518: Predefined type 'System.Threading.Tasks.ValueTask' is not defined or imported
// await foreach (int i in new C()) { }
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "await foreach (int i in new C()) { }").WithArguments("System.Threading.Tasks.ValueTask").WithLocation(6, 9)
);
}
[Fact]
public void TestWithIAsyncEnumerator()
{
string source = @"
using System.Collections.Generic;
using System.Threading.Tasks;
public class C
{
async Task M(IAsyncEnumerator<int> enumerator)
{
await foreach (int i in enumerator) { }
}
}
";
var comp_checked = CreateCompilationWithTasksExtensions(new[] { source, s_IAsyncEnumerable });
comp_checked.VerifyDiagnostics(
// (8,33): error CS8411: Async foreach statement cannot operate on variables of type 'IAsyncEnumerator<int>' because 'IAsyncEnumerator<int>' does not contain a suitable public instance or extension definition for 'GetAsyncEnumerator'
// await foreach (int i in enumerator) { }
Diagnostic(ErrorCode.ERR_AwaitForEachMissingMember, "enumerator").WithArguments("System.Collections.Generic.IAsyncEnumerator<int>", "GetAsyncEnumerator").WithLocation(8, 33)
);
}
[Fact]
public void TestWithUIntToIntConversion()
{
string source = @"
using System.Collections.Generic;
using System.Threading.Tasks;
public class C : IAsyncEnumerable<uint>
{
public static async Task Main()
{
try
{
REPLACE
{
await foreach (int i in new C()) { System.Console.Write($""0x{i:X8}""); }
}
}
catch (System.OverflowException)
{
System.Console.Write(""overflow"");
}
}
public IAsyncEnumerator<uint> GetAsyncEnumerator(System.Threading.CancellationToken token)
=> new AsyncEnumerator();
public sealed class AsyncEnumerator : IAsyncEnumerator<uint>
{
bool firstValue = true;
public uint Current => 0xFFFFFFFF;
public async ValueTask<bool> MoveNextAsync()
{
await Task.Yield();
bool result = firstValue;
firstValue = false;
return result;
}
public async ValueTask DisposeAsync()
{
await Task.Yield();
}
}
}
";
var comp_checked = CreateCompilationWithTasksExtensions(new[] { source.Replace("REPLACE", "checked"), s_IAsyncEnumerable }, options: TestOptions.DebugExe);
comp_checked.VerifyDiagnostics();
CompileAndVerify(comp_checked, expectedOutput: "overflow");
var comp_unchecked = CreateCompilationWithTasksExtensions(new[] { source.Replace("REPLACE", "unchecked"), s_IAsyncEnumerable }, options: TestOptions.DebugExe);
comp_unchecked.VerifyDiagnostics();
CompileAndVerify(comp_unchecked, expectedOutput: "0xFFFFFFFF");
}
[Fact]
public void TestWithTwoIAsyncEnumerableImplementations()
{
string source = @"
using System.Collections.Generic;
class C : IAsyncEnumerable<int>, IAsyncEnumerable<string>
{
public static async System.Threading.Tasks.Task Main()
{
await foreach (int i in new C())
{
}
}
IAsyncEnumerator<int> IAsyncEnumerable<int>.GetAsyncEnumerator(System.Threading.CancellationToken token)
=> throw null;
IAsyncEnumerator<string> IAsyncEnumerable<string>.GetAsyncEnumerator(System.Threading.CancellationToken token)
=> throw null;
}";
var comp = CreateCompilationWithTasksExtensions(new[] { source, s_IAsyncEnumerable });
comp.VerifyDiagnostics(
// (7,33): error CS8413: Async foreach statement cannot operate on variables of type 'C' because it implements multiple instantiations of 'IAsyncEnumerable<T>'; try casting to a specific interface instantiation
// await foreach (int i in new C())
Diagnostic(ErrorCode.ERR_MultipleIAsyncEnumOfT, "new C()").WithArguments("C", "System.Collections.Generic.IAsyncEnumerable<T>").WithLocation(7, 33)
);
}
[Fact]
public void TestWithMissingPattern()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C())
{
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8411: Async foreach statement cannot operate on variables of type 'C' because 'C' does not contain a suitable public instance or extension definition for 'GetAsyncEnumerator'
// await foreach (var i in new C())
Diagnostic(ErrorCode.ERR_AwaitForEachMissingMember, "new C()").WithArguments("C", "GetAsyncEnumerator").WithLocation(6, 33)
);
}
[Fact]
public void TestWithStaticGetEnumerator()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C())
{
}
}
public static Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token)
{
throw null;
}
public sealed class Enumerator
{
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8411: Async foreach statement cannot operate on variables of type 'C' because 'C' does not contain a suitable public instance or extension definition for 'GetAsyncEnumerator'
// await foreach (var i in new C())
Diagnostic(ErrorCode.ERR_AwaitForEachMissingMember, "new C()").WithArguments("C", "GetAsyncEnumerator").WithLocation(6, 33)
);
}
[Fact]
public void TestWithInaccessibleGetEnumerator()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C())
{
}
}
internal Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
{
throw null;
}
public sealed class Enumerator
{
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): warning CS0279: 'C' does not implement the 'async streams' pattern. 'C.GetAsyncEnumerator(System.Threading.CancellationToken)' is not a public instance or extension method.
// await foreach (var i in new C())
Diagnostic(ErrorCode.WRN_PatternNotPublicOrNotInstance, "new C()").WithArguments("C", "async streams", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(6, 33),
// (6,33): error CS8411: Async foreach statement cannot operate on variables of type 'C' because 'C' does not contain a public definition for 'GetAsyncEnumerator'
// await foreach (var i in new C())
Diagnostic(ErrorCode.ERR_AwaitForEachMissingMember, "new C()").WithArguments("C", "GetAsyncEnumerator").WithLocation(6, 33)
);
}
[Fact]
public void TestWithObsoletePatternMethods()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C())
{
}
}
[System.Obsolete]
public Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
{
throw null;
}
public sealed class Enumerator
{
[System.Obsolete]
public System.Threading.Tasks.Task<bool> MoveNextAsync()
{
throw null;
}
[System.Obsolete]
public int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,15): warning CS0612: 'C.GetAsyncEnumerator(CancellationToken)' is obsolete
// await foreach (var i in new C())
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "foreach").WithArguments("C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(6, 15),
// (6,15): warning CS0612: 'C.Enumerator.MoveNextAsync()' is obsolete
// await foreach (var i in new C())
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "foreach").WithArguments("C.Enumerator.MoveNextAsync()").WithLocation(6, 15),
// (6,15): warning CS0612: 'C.Enumerator.Current' is obsolete
// await foreach (var i in new C())
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "foreach").WithArguments("C.Enumerator.Current").WithLocation(6, 15)
);
}
[Fact]
public void TestWithMoveNextAsync_ReturnsValueTaskOfObject()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C()) { }
}
public Enumerator GetAsyncEnumerator() => throw null;
public sealed class Enumerator
{
public System.Threading.Tasks.Task<object> MoveNextAsync() => throw null;
public int Current => throw null;
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator()' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator()").WithLocation(6, 33)
);
}
[Theory, CombinatorialData, WorkItem(65363, "https://github.com/dotnet/roslyn/issues/65363")]
public void TestWithMoveNextAsync_ReturnsValueTaskOfObject_Extension(bool useCsharp8)
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C()) { }
}
}
static class Extension
{
public static Enumerator GetAsyncEnumerator(this C c) => throw null;
}
public sealed class Enumerator
{
public System.Threading.Tasks.Task<object> MoveNextAsync() => throw null;
public int Current => throw null;
}
";
var comp = CreateCompilationWithMscorlib46(source, parseOptions: useCsharp8 ? TestOptions.Regular8 : TestOptions.Regular9);
if (useCsharp8)
{
comp.VerifyDiagnostics(
// (6,33): error CS8400: Feature 'extension GetAsyncEnumerator' is not available in C# 8.0. Please use language version 9.0 or greater.
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion8, "new C()").WithArguments("extension GetAsyncEnumerator", "9.0").WithLocation(6, 33),
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'Enumerator' of 'Extension.GetAsyncEnumerator(C)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("Enumerator", "Extension.GetAsyncEnumerator(C)").WithLocation(6, 33)
);
}
else
{
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'Enumerator' of 'Extension.GetAsyncEnumerator(C)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("Enumerator", "Extension.GetAsyncEnumerator(C)").WithLocation(6, 33)
);
}
}
[Fact]
public void TestWithMoveNextAsync_ReturnsObject()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C())
{
}
}
public Enumerator GetAsyncEnumerator() => throw null;
public sealed class Enumerator
{
public System.Threading.Tasks.Task<object> MoveNextAsync() => throw null; // returns Task<object>
public int Current { get => throw null; }
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator()' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C())
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator()").WithLocation(6, 33)
);
}
[Fact]
public void TestWithMoveNextAsync_Static()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C())
{
}
}
public Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
{
throw null;
}
public sealed class Enumerator
{
public static System.Threading.Tasks.Task<bool> MoveNextAsync()
{
throw null;
}
public int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator(CancellationToken)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C())
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(6, 33)
);
}
[Fact]
public void TestWithCurrent_Static()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C()) { }
}
public Enumerator GetAsyncEnumerator()
=> throw null;
public sealed class Enumerator
{
public System.Threading.Tasks.Task<bool> MoveNextAsync()
=> throw null;
public static int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator()' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator()").WithLocation(6, 33)
);
}
[Fact]
public void TestWithMoveNextAsync_NonPublic()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C()) { }
}
public Enumerator GetAsyncEnumerator()
=> throw null;
public sealed class Enumerator
{
internal System.Threading.Tasks.Task<bool> MoveNextAsync()
=> throw null;
public int Current
=> throw null;
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator()' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator()").WithLocation(6, 33)
);
}
[Fact]
public void TestWithCurrent_NonPublicProperty()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C()) { }
}
public Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
=> throw null;
public sealed class Enumerator
{
public System.Threading.Tasks.Task<bool> MoveNextAsync()
=> throw null;
private int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS0122: 'C.Enumerator.Current' is inaccessible due to its protection level
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadAccess, "new C()").WithArguments("C.Enumerator.Current").WithLocation(6, 33),
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator(CancellationToken)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(6, 33)
);
}
[Fact]
public void TestWithCurrent_NonPublicGetter()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C()) { }
}
public Enumerator GetAsyncEnumerator()
=> throw null;
public sealed class Enumerator
{
public System.Threading.Tasks.Task<bool> MoveNextAsync()
=> throw null;
public int Current
{
private get => throw null;
set => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator()' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator()").WithLocation(6, 33)
);
}
[Fact]
public void TestWithCurrent_MissingGetter()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C()) { }
}
public Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
=> throw null;
public sealed class Enumerator
{
public System.Threading.Tasks.Task<bool> MoveNextAsync()
=> throw null;
public int Current
{
set => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator(CancellationToken)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C()) { }
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(6, 33)
);
}
[Fact]
public void TestWithCurrent_MissingGetterOnInterface()
{
string source = @"
using System.Collections.Generic;
using System.Threading.Tasks;
class C : IAsyncEnumerable<int>
{
public static async Task M(C c)
{
await foreach (var i in c)
{
}
}
public IAsyncEnumerator<int> GetAsyncEnumerator(System.Threading.CancellationToken token = default) => throw null;
}
namespace System.Collections.Generic
{
public interface IAsyncEnumerable<out T>
{
IAsyncEnumerator<T> GetAsyncEnumerator(System.Threading.CancellationToken token = default);
}
public interface IAsyncEnumerator<out T> : System.IAsyncDisposable
{
System.Threading.Tasks.Task<bool> MoveNextAsync();
T Current { set; }
}
}
namespace System
{
public interface IAsyncDisposable
{
System.Threading.Tasks.ValueTask DisposeAsync();
}
}
";
var comp = CreateCompilationWithTasksExtensions(source);
comp.VerifyEmitDiagnostics(
// (8,33): error CS8412: Asynchronous foreach requires that the return type 'IAsyncEnumerator<int>' of 'C.GetAsyncEnumerator(CancellationToken)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in c)
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "c").WithArguments("System.Collections.Generic.IAsyncEnumerator<int>", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(8, 33),
// (24,9): error CS1961: Invalid variance: The type parameter 'T' must be contravariantly valid on 'IAsyncEnumerator<T>.Current'. 'T' is covariant.
// T Current { set; }
Diagnostic(ErrorCode.ERR_UnexpectedVariance, "T").WithArguments("System.Collections.Generic.IAsyncEnumerator<T>.Current", "T", "covariant", "contravariantly").WithLocation(24, 9)
);
}
[Fact]
public void TestWithCurrent_MissingPropertyOnInterface()
{
string source = @"
using System.Collections.Generic;
using System.Threading.Tasks;
class C : IAsyncEnumerable<int>
{
public static async Task M(C c)
{
await foreach (var i in c)
{
}
}
public IAsyncEnumerator<int> GetAsyncEnumerator(System.Threading.CancellationToken token = default) => throw null;
}
namespace System.Collections.Generic
{
public interface IAsyncEnumerable<out T>
{
IAsyncEnumerator<T> GetAsyncEnumerator(System.Threading.CancellationToken token = default);
}
public interface IAsyncEnumerator<out T> : System.IAsyncDisposable
{
System.Threading.Tasks.Task<bool> MoveNextAsync();
}
}
namespace System
{
public interface IAsyncDisposable
{
System.Threading.Tasks.ValueTask DisposeAsync();
}
}
";
var comp = CreateCompilationWithTasksExtensions(source);
comp.VerifyEmitDiagnostics(
// (8,33): error CS0117: 'IAsyncEnumerator<int>' does not contain a definition for 'Current'
// await foreach (var i in c)
Diagnostic(ErrorCode.ERR_NoSuchMember, "c").WithArguments("System.Collections.Generic.IAsyncEnumerator<int>", "Current").WithLocation(8, 33),
// (8,33): error CS8412: Asynchronous foreach requires that the return type 'IAsyncEnumerator<int>' of 'C.GetAsyncEnumerator(CancellationToken)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in c)
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "c").WithArguments("System.Collections.Generic.IAsyncEnumerator<int>", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(8, 33)
);
}
[Fact]
public void TestMoveNextAsync_ReturnsTask()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C())
{
}
}
public Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
{
throw null;
}
public sealed class Enumerator
{
public System.Threading.Tasks.Task MoveNextAsync()
{
throw null;
}
public int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator(CancellationToken)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C())
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(6, 33)
);
}
[Fact]
public void TestMoveNextAsync_ReturnsTaskOfInt()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (var i in new C())
{
}
}
public Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
{
throw null;
}
public sealed class Enumerator
{
public System.Threading.Tasks.Task<int> MoveNextAsync()
{
throw null;
}
public int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (6,33): error CS8412: Asynchronous foreach requires that the return type 'C.Enumerator' of 'C.GetAsyncEnumerator(CancellationToken)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in new C())
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "new C()").WithArguments("C.Enumerator", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(6, 33)
);
}
[Fact]
public void TestMoveNextAsync_WithOptionalParameter()
{
string source = @"
public class C
{
public static async System.Threading.Tasks.Task Main()
{
await foreach (var i in new C())
{
}
}
public Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
{
return new Enumerator();
}
public sealed class Enumerator
{
public async System.Threading.Tasks.Task<bool> MoveNextAsync(int ok = 1)
{
System.Console.Write($""MoveNextAsync {ok}"");
await System.Threading.Tasks.Task.Yield();
return false;
}
public int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
CompileAndVerify(comp, expectedOutput: "MoveNextAsync 1");
}
[Fact]
public void TestMoveNextAsync_WithParamsParameter()
{
string source = @"
public class C
{
public static async System.Threading.Tasks.Task Main()
{
await foreach (var i in new C())
{
}
}
public Enumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
{
return new Enumerator();
}
public sealed class Enumerator
{
public async System.Threading.Tasks.Task<bool> MoveNextAsync(params int[] ok)
{
System.Console.Write($""MoveNextAsync {ok.Length}"");
await System.Threading.Tasks.Task.Yield();
return false;
}
public int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithMscorlib46(source, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
CompileAndVerify(comp, expectedOutput: "MoveNextAsync 0");
}
[Fact]
public void TestMoveNextAsync_Missing()
{
string source = @"
using System.Collections.Generic;
using System.Threading.Tasks;
class C : IAsyncEnumerable<int>
{
public static async Task M(C c)
{
await foreach (var i in c)
{
}
}
public IAsyncEnumerator<int> GetAsyncEnumerator(System.Threading.CancellationToken token = default) => throw null;
}
namespace System.Collections.Generic
{
public interface IAsyncEnumerable<out T>
{
IAsyncEnumerator<T> GetAsyncEnumerator(System.Threading.CancellationToken token = default);
}
public interface IAsyncEnumerator<out T> : System.IAsyncDisposable
{
T Current { get; }
}
}
namespace System
{
public interface IAsyncDisposable
{
System.Threading.Tasks.ValueTask DisposeAsync();
}
}
";
var comp = CreateCompilationWithTasksExtensions(source);
comp.VerifyEmitDiagnostics(
// (8,33): error CS0117: 'IAsyncEnumerator<int>' does not contain a definition for 'MoveNextAsync'
// await foreach (var i in c)
Diagnostic(ErrorCode.ERR_NoSuchMember, "c").WithArguments("System.Collections.Generic.IAsyncEnumerator<int>", "MoveNextAsync").WithLocation(8, 33),
// (8,33): error CS8412: Asynchronous foreach requires that the return type 'IAsyncEnumerator<int>' of 'C.GetAsyncEnumerator(CancellationToken)' must have a suitable public 'MoveNextAsync' method and public 'Current' property
// await foreach (var i in c)
Diagnostic(ErrorCode.ERR_BadGetAsyncEnumerator, "c").WithArguments("System.Collections.Generic.IAsyncEnumerator<int>", "C.GetAsyncEnumerator(System.Threading.CancellationToken)").WithLocation(8, 33)
);
}
[Fact]
public void TestWithNonConvertibleElementType()
{
string source = @"
class C
{
async System.Threading.Tasks.Task M()
{
await foreach (string i in new C())
{
}
}
public Enumerator GetAsyncEnumerator()
=> throw null;
public sealed class Enumerator
{
public System.Threading.Tasks.Task<bool> MoveNextAsync()
=> throw null;
public int Current
{
get => throw null;
}
}
}";
var comp = CreateCompilationWithTasksExtensions(source + s_IAsyncEnumerable);
comp.VerifyDiagnostics(
// (6,15): error CS0030: Cannot convert type 'int' to 'string'
// await foreach (string i in new C())
Diagnostic(ErrorCode.ERR_NoExplicitConv, "foreach").WithArguments("int", "string").WithLocation(6, 15)
);
var tree = comp.SyntaxTrees.Single();
var model = comp.GetSemanticModel(tree, ignoreAccessibility: false);
var foreachSyntax = tree.GetRoot().DescendantNodes().OfType<ForEachStatementSyntax>().Single();
var info = model.GetForEachStatementInfo(foreachSyntax);
Assert.True(info.IsAsynchronous);
Assert.Equal("C.Enumerator C.GetAsyncEnumerator()", info.GetEnumeratorMethod.ToTestDisplayString());
Assert.Equal("System.Threading.Tasks.Task<System.Boolean> C.Enumerator.MoveNextAsync()", info.MoveNextMethod.ToTestDisplayString());
Assert.Equal("System.Int32 C.Enumerator.Current { get; }", info.CurrentProperty.ToTestDisplayString());
Assert.Null(info.DisposeMethod);
Assert.Equal("System.Int32", info.ElementType.ToTestDisplayString());
Assert.Equal(ConversionKind.NoConversion, info.ElementConversion.Kind);
Assert.Equal(ConversionKind.Identity, info.CurrentConversion.Kind);
}
[Fact]
public void TestWithNonConvertibleElementType2()
{
string source = @"
using System.Threading.Tasks;
class C
{
async Task M()
{
await foreach (Element i in new C())
{
}
}
public AsyncEnumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
=> throw null;
public sealed class AsyncEnumerator
{
public int Current
{
get => throw null;
}
public Task<bool> MoveNextAsync()
=> throw null;
public ValueTask DisposeAsync()
=> throw null;
}
}
class Element
{
}";
var comp = CreateCompilationWithTasksExtensions(source);
comp.VerifyDiagnostics(
// (7,15): error CS0030: Cannot convert type 'int' to 'Element'
// await foreach (Element i in new C())
Diagnostic(ErrorCode.ERR_NoExplicitConv, "foreach").WithArguments("int", "Element").WithLocation(7, 15)
);
}
[Fact]
public void TestWithExplicitlyConvertibleElementType()
{
string source = @"
using static System.Console;
using System.Threading.Tasks;
class C
{
public static async System.Threading.Tasks.Task Main()
{
await foreach (Element i in new C())
{
Write($""Got({i}) "");
}