@@ -4821,8 +4821,8 @@ public static class Extensions
4821
4821
CompileAndVerify ( comp , expectedOutput : "NextAsync(0) Current(1) Got(1,-1) NextAsync(1) Current(2) Got(2,-2) NextAsync(2) Dispose(3) Done" ) ;
4822
4822
}
4823
4823
4824
- [ Fact ]
4825
- public void TestWithPatternAndObsolete ( )
4824
+ [ Fact , WorkItem ( "https://github.com/dotnet/roslyn/issues/30257" ) ]
4825
+ public void TestWithPatternAndObsolete_WithDisposableInterface ( )
4826
4826
{
4827
4827
string source = @"
4828
4828
using System.Threading.Tasks;
@@ -4852,6 +4852,9 @@ public sealed class AsyncEnumerator : System.IAsyncDisposable
4852
4852
}" ;
4853
4853
var comp = CreateCompilationWithTasksExtensions ( source + s_IAsyncEnumerable , options : TestOptions . DebugExe ) ;
4854
4854
comp . VerifyDiagnostics (
4855
+ // (7,15): warning CS0612: 'C.AsyncEnumerator.DisposeAsync()' is obsolete
4856
+ // await foreach (var i in new C())
4857
+ Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.AsyncEnumerator.DisposeAsync()" ) . WithLocation ( 7 , 15 ) ,
4855
4858
// (7,15): warning CS0612: 'C.GetAsyncEnumerator(CancellationToken)' is obsolete
4856
4859
// await foreach (var i in new C())
4857
4860
Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.GetAsyncEnumerator(System.Threading.CancellationToken)" ) . WithLocation ( 7 , 15 ) ,
@@ -4860,9 +4863,52 @@ public sealed class AsyncEnumerator : System.IAsyncDisposable
4860
4863
Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.AsyncEnumerator.MoveNextAsync()" ) . WithLocation ( 7 , 15 ) ,
4861
4864
// (7,15): warning CS0612: 'C.AsyncEnumerator.Current' is obsolete
4862
4865
// await foreach (var i in new C())
4863
- Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.AsyncEnumerator.Current" ) . WithLocation ( 7 , 15 )
4864
- ) ;
4865
- // Note: Obsolete on DisposeAsync is not reported since always called through IAsyncDisposable interface
4866
+ Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.AsyncEnumerator.Current" ) . WithLocation ( 7 , 15 ) ) ;
4867
+ }
4868
+
4869
+ [ Fact , WorkItem ( "https://github.com/dotnet/roslyn/issues/30257" ) ]
4870
+ public void TestWithPatternAndObsolete_WithoutDisposableInterface ( )
4871
+ {
4872
+ string source = @"
4873
+ using System.Threading.Tasks;
4874
+ class C
4875
+ {
4876
+ static async System.Threading.Tasks.Task Main()
4877
+ {
4878
+ await foreach (var i in new C())
4879
+ {
4880
+ }
4881
+ }
4882
+ [System.Obsolete]
4883
+ public AsyncEnumerator GetAsyncEnumerator(System.Threading.CancellationToken token = default)
4884
+ {
4885
+ throw null;
4886
+ }
4887
+ [System.Obsolete]
4888
+ public sealed class AsyncEnumerator
4889
+ {
4890
+ [System.Obsolete]
4891
+ public int Current { get => throw null; }
4892
+ [System.Obsolete]
4893
+ public Task<bool> MoveNextAsync() => throw null;
4894
+ [System.Obsolete]
4895
+ public ValueTask DisposeAsync() => throw null;
4896
+ }
4897
+ }" ;
4898
+ var comp = CreateCompilationWithTasksExtensions ( source + s_IAsyncEnumerable , options : TestOptions . DebugExe ) ;
4899
+ comp . VerifyDiagnostics (
4900
+ // (7,15): warning CS0612: 'C.AsyncEnumerator.DisposeAsync()' is obsolete
4901
+ // await foreach (var i in new C())
4902
+ Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.AsyncEnumerator.DisposeAsync()" ) . WithLocation ( 7 , 15 ) ,
4903
+ // (7,15): warning CS0612: 'C.GetAsyncEnumerator(CancellationToken)' is obsolete
4904
+ // await foreach (var i in new C())
4905
+ Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.GetAsyncEnumerator(System.Threading.CancellationToken)" ) . WithLocation ( 7 , 15 ) ,
4906
+ // (7,15): warning CS0612: 'C.AsyncEnumerator.MoveNextAsync()' is obsolete
4907
+ // await foreach (var i in new C())
4908
+ Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.AsyncEnumerator.MoveNextAsync()" ) . WithLocation ( 7 , 15 ) ,
4909
+ // (7,15): warning CS0612: 'C.AsyncEnumerator.Current' is obsolete
4910
+ // await foreach (var i in new C())
4911
+ Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.AsyncEnumerator.Current" ) . WithLocation ( 7 , 15 ) ) ;
4866
4912
}
4867
4913
4868
4914
[ Fact ]
@@ -8495,6 +8541,9 @@ public static class Extensions
8495
8541
}" ;
8496
8542
var comp = CreateCompilationWithMscorlib46 ( source , options : TestOptions . DebugExe , parseOptions : TestOptions . Regular9 ) ;
8497
8543
comp . VerifyDiagnostics (
8544
+ // (8,15): warning CS0612: 'C.Enumerator.DisposeAsync()' is obsolete
8545
+ // await foreach (var i in new C())
8546
+ Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.Enumerator.DisposeAsync()" ) . WithLocation ( 8 , 15 ) ,
8498
8547
// (8,15): warning CS0612: 'Extensions.GetAsyncEnumerator(C)' is obsolete
8499
8548
// await foreach (var i in new C())
8500
8549
Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "Extensions.GetAsyncEnumerator(C)" ) . WithLocation ( 8 , 15 ) ,
@@ -8503,8 +8552,7 @@ public static class Extensions
8503
8552
Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.Enumerator.MoveNextAsync()" ) . WithLocation ( 8 , 15 ) ,
8504
8553
// (8,15): warning CS0612: 'C.Enumerator.Current' is obsolete
8505
8554
// await foreach (var i in new C())
8506
- Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.Enumerator.Current" ) . WithLocation ( 8 , 15 )
8507
- ) ;
8555
+ Diagnostic ( ErrorCode . WRN_DeprecatedSymbol , "foreach" ) . WithArguments ( "C.Enumerator.Current" ) . WithLocation ( 8 , 15 ) ) ;
8508
8556
CompileAndVerify ( comp , expectedOutput : "123Disposed" ) ;
8509
8557
}
8510
8558
0 commit comments