-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Improve async iterator diagnostic by removing redundant error #81282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
2355158
4ea5dc9
f79a4b6
386f33a
3de3769
1a57671
cac74c6
b7d326e
3b78eef
a5766a3
5b4c38e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,9 +185,21 @@ private bool ReportBadAwaitWithoutAsync(SyntaxNodeOrToken nodeOrToken, BindingDi | |
| } | ||
| else | ||
| { | ||
| info = method.ReturnsVoid ? | ||
| new CSDiagnosticInfo(ErrorCode.ERR_BadAwaitWithoutVoidAsyncMethod) : | ||
| new CSDiagnosticInfo(ErrorCode.ERR_BadAwaitWithoutAsyncMethod, method.ReturnType); | ||
| if (method.ReturnsVoid) | ||
| { | ||
| info = new CSDiagnosticInfo(ErrorCode.ERR_BadAwaitWithoutVoidAsyncMethod); | ||
| } | ||
| else if (method.IsIterator && | ||
| (method.ReturnType.IsIAsyncEnumerableType(Compilation) || | ||
| method.ReturnType.IsIAsyncEnumeratorType(Compilation))) | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot mention which method reports this ERR_IteratorMustBeAsync error. Also, if possible, can we share code between that location and this location to ensure that we're checking all the same conditions to report an error there and not report an error here? #Closed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 5b4c38e. Now using the shared |
||
| // For async iterators, don't suggest changing the return type | ||
| info = new CSDiagnosticInfo(ErrorCode.ERR_BadAwaitWithoutAsyncIterator); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot instead of a new error, can you use ERR_BadAwaitWithoutAsync? #Closed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 3de3769. Using |
||
| } | ||
| else | ||
| { | ||
| info = new CSDiagnosticInfo(ErrorCode.ERR_BadAwaitWithoutAsyncMethod, method.ReturnType); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1997,12 +1997,34 @@ static System.Collections.Generic.IAsyncEnumerator<int> M(int value) | |
| // (4,61): error CS8403: Method 'C.M(int)' with an iterator block must be 'async' to return 'IAsyncEnumerator<int>' | ||
| // static System.Collections.Generic.IAsyncEnumerator<int> M(int value) | ||
| Diagnostic(ErrorCode.ERR_IteratorMustBeAsync, "M").WithArguments("C.M(int)", "System.Collections.Generic.IAsyncEnumerator<int>").WithLocation(4, 61), | ||
| // (7,9): error CS4032: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<IAsyncEnumerator<int>>'. | ||
| // (7,9): error CS4037: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier. | ||
| // await System.Threading.Tasks.Task.CompletedTask; | ||
| Diagnostic(ErrorCode.ERR_BadAwaitWithoutAsyncMethod, "await System.Threading.Tasks.Task.CompletedTask").WithArguments("System.Collections.Generic.IAsyncEnumerator<int>").WithLocation(7, 9) | ||
| Diagnostic(ErrorCode.ERR_BadAwaitWithoutAsyncIterator, "await System.Threading.Tasks.Task.CompletedTask").WithLocation(7, 9) | ||
| ); | ||
| } | ||
|
|
||
| // This error message is rather poor. Tracked by https://github.com/dotnet/roslyn/issues/31113 | ||
| [Fact] | ||
| [WorkItem(31057, "https://github.com/dotnet/roslyn/issues/31057")] | ||
| [WorkItem(31113, "https://github.com/dotnet/roslyn/issues/31113")] | ||
|
CyrusNajmabadi marked this conversation as resolved.
Outdated
|
||
| public void AsyncIteratorReturningEnumerable_WithoutAsync() | ||
|
333fred marked this conversation as resolved.
|
||
| { | ||
| string source = @" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot use raw string literals.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in cac74c6. Converted both tests to use raw string literals and updated the line numbers in diagnostic expectations. |
||
| class C | ||
| { | ||
| static System.Collections.Generic.IAsyncEnumerable<int> M(int value) | ||
| { | ||
| yield return value; | ||
| await System.Threading.Tasks.Task.CompletedTask; | ||
| } | ||
| }"; | ||
| var comp = CreateCompilationWithAsyncIterator(source); | ||
| comp.VerifyDiagnostics( | ||
| // (4,61): error CS8403: Method 'C.M(int)' with an iterator block must be 'async' to return 'IAsyncEnumerable<int>' | ||
| // static System.Collections.Generic.IAsyncEnumerable<int> M(int value) | ||
| Diagnostic(ErrorCode.ERR_IteratorMustBeAsync, "M").WithArguments("C.M(int)", "System.Collections.Generic.IAsyncEnumerable<int>").WithLocation(4, 61), | ||
| // (7,9): error CS4037: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier. | ||
| // await System.Threading.Tasks.Task.CompletedTask; | ||
| Diagnostic(ErrorCode.ERR_BadAwaitWithoutAsyncIterator, "await System.Threading.Tasks.Task.CompletedTask").WithLocation(7, 9)); | ||
| } | ||
|
|
||
| [Fact] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot fix formatting errors (excess spaces on line). #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in b7d326e. Removed trailing whitespace from lines 192-194.