Skip to content

Commit 3f831ea

Browse files
committed
Add description of auto traits in async blocks
This documents the proposed liveness-based rules for inferring auto traits for async block. Currently we use a scope-based approach, so this also serves as a description of we would like to change.
1 parent da6ea9b commit 3f831ea

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/expressions/block-expr.md

+21
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ Async blocks capture variables from their environment using the same [capture mo
9494
Like closures, when written `async { .. }` the capture mode for each variable will be inferred from the content of the block.
9595
`async move { .. }` blocks however will move all referenced variables into the resulting future.
9696
97+
### Auto traits and `async` blocks
98+
99+
Auto trait inference for `async` blocks follow the same [rules as closures] except that values that are live across an `await` expression are also considered.
100+
Live values are variables or temporaries that are defined before an `await` expression and potentially used afterwards.
101+
As an example, see below:
102+
```rust
103+
#async fn foo() {}
104+
async {
105+
let x = Bar;
106+
foo().await
107+
drop(x);
108+
}
109+
```
110+
Here the resulting future will be `Send` if `Bar` is send, since `x` is defined before the await and used afterwards.
111+
112+
Note that for values of types that implement `Drop`, there is an implicit use of the value at the end of its lifetime in order to run the destructor.
113+
114+
Besides named variables, temporary values also count. For example in `foo(&bar, baz.await)`, the value `&bar` is considered live across the `await` point. This is also true of the scrutinee of the match expression, since [the scrutinee is live for the entire match block].
115+
97116
### Async context
98117

99118
Because async blocks construct a future, they define an **async context** which can in turn contain [`await` expressions].
@@ -189,3 +208,5 @@ fn is_unix_platform() -> bool {
189208
[tuple expressions]: tuple-expr.md
190209
[unsafe operations]: ../unsafety.md
191210
[value expressions]: ../expressions.md#place-expressions-and-value-expressions
211+
[rules as closures]: ../special-types-and-traits.md#auto-traits
212+
[the scrutinee is live for the entire match block]: ../destructors.md#temporary-scopes

0 commit comments

Comments
 (0)