@@ -2045,8 +2045,8 @@ so that a generator can then be constructed:
2045
2045
async fn bar<T>() -> () {}
2046
2046
2047
2047
async fn foo() {
2048
- bar::<String>().await;
2049
- // ^^^^^^^^ specify type explicitly
2048
+ bar::<String>().await;
2049
+ // ^^^^^^^^ specify type explicitly
2050
2050
}
2051
2051
```
2052
2052
"## ,
@@ -2126,6 +2126,84 @@ static X: u32 = 42;
2126
2126
```
2127
2127
"## ,
2128
2128
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
+
2129
2207
E0734 : r##"
2130
2208
A stability attribute has been used outside of the standard library.
2131
2209
@@ -2218,6 +2296,5 @@ See [RFC 2091] for details on this and other limitations.
2218
2296
// E0702, // replaced with a generic attribute input check
2219
2297
E0726 , // non-explicit (not `'_`) elided lifetime in unsupported position
2220
2298
E0727 , // `async` generators are not yet supported
2221
- E0728 , // `await` must be in an `async` function or block
2222
2299
E0739 , // invalid track_caller application/syntax
2223
2300
}
0 commit comments