@@ -2045,8 +2045,8 @@ so that a generator can then be constructed:
20452045async fn bar<T>() -> () {}
20462046
20472047async 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+
21292197E0734 : r##"
21302198A 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}
0 commit comments