Skip to content

Commit cde60e8

Browse files
committed
Add long error explanation for E0728
1 parent 10f12fe commit cde60e8

File tree

7 files changed

+78
-5
lines changed

7 files changed

+78
-5
lines changed

src/librustc/error_codes.rs

+70-3
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,74 @@ 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` context
2193+
}
2194+
```
2195+
"##,
2196+
21292197
E0734: r##"
21302198
A stability attribute has been used outside of the standard library.
21312199
@@ -2218,6 +2286,5 @@ See [RFC 2091] for details on this and other limitations.
22182286
// E0702, // replaced with a generic attribute input check
22192287
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
22202288
E0727, // `async` generators are not yet supported
2221-
E0728, // `await` must be in an `async` function or block
22222289
E0739, // invalid track_caller application/syntax
22232290
}

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

+2-1
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

+1
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

+1
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

+2-1
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

+1
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

+1
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)