Skip to content

Commit b799465

Browse files
authored
Rollup merge of rust-lang#65678 - JohnTitor:add-e0728-explanation, r=GuilliaumeGomez
Add long error explanation for E0728 Part of rust-lang#61137 r? @GuillaumeGomez
2 parents 88e3ae2 + a1f6589 commit b799465

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

src/librustc/error_codes.rs

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,8 +2045,8 @@ so that a generator can then be constructed:
20452045
async fn bar<T>() -> () {}
20462046
20472047
async fn foo() {
2048-
bar::<String>().await;
2049-
// ^^^^^^^^ specify type explicitly
2048+
bar::<String>().await;
2049+
// ^^^^^^^^ specify type explicitly
20502050
}
20512051
```
20522052
"##,
@@ -2126,6 +2126,84 @@ static X: u32 = 42;
21262126
```
21272127
"##,
21282128

2129+
E0728: r##"
2130+
[`await`] has been used outside [`async`] function or block.
2131+
2132+
Erroneous code examples:
2133+
2134+
```edition2018,compile_fail,E0728
2135+
# use std::pin::Pin;
2136+
# use std::future::Future;
2137+
# use std::task::{Context, Poll};
2138+
#
2139+
# struct WakeOnceThenComplete(bool);
2140+
#
2141+
# fn wake_and_yield_once() -> WakeOnceThenComplete {
2142+
# WakeOnceThenComplete(false)
2143+
# }
2144+
#
2145+
# impl Future for WakeOnceThenComplete {
2146+
# type Output = ();
2147+
# fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
2148+
# if self.0 {
2149+
# Poll::Ready(())
2150+
# } else {
2151+
# cx.waker().wake_by_ref();
2152+
# self.0 = true;
2153+
# Poll::Pending
2154+
# }
2155+
# }
2156+
# }
2157+
#
2158+
fn foo() {
2159+
wake_and_yield_once().await // `await` is used outside `async` context
2160+
}
2161+
```
2162+
2163+
[`await`] is used to suspend the current computation until the given
2164+
future is ready to produce a value. So it is legal only within
2165+
an [`async`] context, like an `async fn` or an `async` block.
2166+
2167+
```edition2018
2168+
# use std::pin::Pin;
2169+
# use std::future::Future;
2170+
# use std::task::{Context, Poll};
2171+
#
2172+
# struct WakeOnceThenComplete(bool);
2173+
#
2174+
# fn wake_and_yield_once() -> WakeOnceThenComplete {
2175+
# WakeOnceThenComplete(false)
2176+
# }
2177+
#
2178+
# impl Future for WakeOnceThenComplete {
2179+
# type Output = ();
2180+
# fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
2181+
# if self.0 {
2182+
# Poll::Ready(())
2183+
# } else {
2184+
# cx.waker().wake_by_ref();
2185+
# self.0 = true;
2186+
# Poll::Pending
2187+
# }
2188+
# }
2189+
# }
2190+
#
2191+
async fn foo() {
2192+
wake_and_yield_once().await // `await` is used within `async` function
2193+
}
2194+
2195+
fn bar(x: u8) -> impl Future<Output = u8> {
2196+
async move {
2197+
wake_and_yield_once().await; // `await` is used within `async` block
2198+
x
2199+
}
2200+
}
2201+
```
2202+
2203+
[`async`]: https://doc.rust-lang.org/std/keyword.async.html
2204+
[`await`]: https://doc.rust-lang.org/std/keyword.await.html
2205+
"##,
2206+
21292207
E0734: r##"
21302208
A stability attribute has been used outside of the standard library.
21312209
@@ -2218,6 +2296,5 @@ See [RFC 2091] for details on this and other limitations.
22182296
// E0702, // replaced with a generic attribute input check
22192297
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
22202298
E0727, // `async` generators are not yet supported
2221-
E0728, // `await` must be in an `async` function or block
22222299
E0739, // invalid track_caller application/syntax
22232300
}

src/test/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,5 @@ LL | let _ = await bar()?;
244244

245245
error: aborting due to 35 previous errors
246246

247-
For more information about this error, try `rustc --explain E0277`.
247+
Some errors have detailed explanations: E0277, E0728.
248+
For more information about an error, try `rustc --explain E0277`.

src/test/ui/async-await/issues/issue-51719.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ LL | let _gen = || foo().await;
88

99
error: aborting due to previous error
1010

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

src/test/ui/async-await/issues/issue-51751.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ LL | let finished = result.await;
99

1010
error: aborting due to previous error
1111

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

src/test/ui/async-await/issues/issue-62009-1.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ LL | F: Future
4040

4141
error: aborting due to 4 previous errors
4242

43-
For more information about this error, try `rustc --explain E0277`.
43+
Some errors have detailed explanations: E0277, E0728.
44+
For more information about an error, try `rustc --explain E0277`.

src/test/ui/async-await/issues/issue-62009-2.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ LL | (async || 2333)().await;
88

99
error: aborting due to previous error
1010

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

src/test/ui/async-await/issues/non-async-enclosing-span.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ LL | let y = do_the_thing().await;
99

1010
error: aborting due to previous error
1111

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

0 commit comments

Comments
 (0)