Skip to content
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

Add long error explanation for E0728 #65678

Merged
merged 3 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 77 additions & 3 deletions src/librustc/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2045,8 +2045,8 @@ so that a generator can then be constructed:
async fn bar<T>() -> () {}

async fn foo() {
bar::<String>().await;
// ^^^^^^^^ specify type explicitly
bar::<String>().await;
// ^^^^^^^^ specify type explicitly
}
```
"##,
Expand Down Expand Up @@ -2126,6 +2126,81 @@ static X: u32 = 42;
```
"##,

E0728: r##"
`await` has been used outside `async` function or block.

Erroneous code examples:

```edition2018,compile_fail,E0728
# use std::pin::Pin;
# use std::future::Future;
# use std::task::{Context, Poll};
#
# struct WakeOnceThenComplete(bool);
#
# fn wake_and_yield_once() -> WakeOnceThenComplete {
# WakeOnceThenComplete(false)
# }
#
# impl Future for WakeOnceThenComplete {
# type Output = ();
# fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
# if self.0 {
# Poll::Ready(())
# } else {
# cx.waker().wake_by_ref();
# self.0 = true;
# Poll::Pending
# }
# }
# }
#
fn foo() {
wake_and_yield_once().await // `await` is used outside `async` context
}
```

`await` is used to suspend the current computation until the given
future is ready to produce a value. So it is legal only within
an async context, like an `async fn` or an `async` block.

```edition2018
# use std::pin::Pin;
# use std::future::Future;
# use std::task::{Context, Poll};
#
# struct WakeOnceThenComplete(bool);
#
# fn wake_and_yield_once() -> WakeOnceThenComplete {
# WakeOnceThenComplete(false)
# }
#
# impl Future for WakeOnceThenComplete {
# type Output = ();
# fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
# if self.0 {
# Poll::Ready(())
# } else {
# cx.waker().wake_by_ref();
# self.0 = true;
# Poll::Pending
# }
# }
# }
#
async fn foo() {
wake_and_yield_once().await // `await` is used within `async` function
}

fn bar(x: u8) -> impl Future<Output = u8> {
async move {
wake_and_yield_once().await; // `await` is used within `async` block
x
}
}
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an example inside of an async block, I'd say.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an async block example, what about?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great.

My last nitpick would be to have a link to the async/await documentation, but this is already a great improvement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, at glance, we don't have the links to keywords in error code's explanations. I found the links to RFCs or specific topics. We should have a link to the async/await?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would lean towards over linking over under linking, but I'll defer to @GuillaumeGomez

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to have the links indeed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"##,

E0734: r##"
A stability attribute has been used outside of the standard library.

Expand Down Expand Up @@ -2218,6 +2293,5 @@ See [RFC 2091] for details on this and other limitations.
// E0702, // replaced with a generic attribute input check
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
E0727, // `async` generators are not yet supported
E0728, // `await` must be in an `async` function or block
E0739, // invalid track_caller application/syntax
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,5 @@ LL | let _ = await bar()?;

error: aborting due to 35 previous errors

For more information about this error, try `rustc --explain E0277`.
Some errors have detailed explanations: E0277, E0728.
For more information about an error, try `rustc --explain E0277`.
1 change: 1 addition & 0 deletions src/test/ui/async-await/issues/issue-51719.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ LL | let _gen = || foo().await;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0728`.
1 change: 1 addition & 0 deletions src/test/ui/async-await/issues/issue-51751.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ LL | let finished = result.await;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0728`.
3 changes: 2 additions & 1 deletion src/test/ui/async-await/issues/issue-62009-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ LL | F: Future

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0277`.
Some errors have detailed explanations: E0277, E0728.
For more information about an error, try `rustc --explain E0277`.
1 change: 1 addition & 0 deletions src/test/ui/async-await/issues/issue-62009-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ LL | (async || 2333)().await;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0728`.
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ LL | let y = do_the_thing().await;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0728`.