Skip to content

Commit 6c4360e

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 4b35388 commit 6c4360e

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/expressions/block-expr.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,22 @@ loop {
119119
120120
### Auto traits and `async` blocks
121121

122-
Auto trait inference for `async` blocks follow the same [rules as closures] except that [temporary values that are in scope][temporary-scopes] at an `await` expression are also considered. For example, consider the following block:
123-
122+
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.
123+
Live values are variables or temporaries that are defined before an `await` expression and potentially used afterwards.
124+
As an example, see below:
124125
```rust
125-
#fn bar() -> i32 { 42 }
126126
#async fn foo() {}
127127
async {
128-
match bar() {
129-
_ => foo().await,
130-
}
128+
let x = Bar;
129+
foo().await
130+
drop(x);
131131
}
132-
#;
133132
```
133+
Here the resulting future will be `Send` if `Bar` is send, since `x` is defined before the await and used afterwards.
134+
135+
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.
134136

135-
Here the result of `bar()` is in scope during the await of `foo()`, so the result of `bar()` will impact the inferred auto traits.
136-
If `bar()` is not `Send`, then the future for the whole match block will also not be `Send`.
137+
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][temporary-scopes].
137138

138139
## `unsafe` blocks
139140

0 commit comments

Comments
 (0)