Skip to content

Document using liveness to infer auto traits for async blocks #1088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/expressions/block-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ loop {
}
```

### Auto traits and `async` blocks

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.
Live values are variables or temporaries that are defined before an `await` expression and potentially used afterwards.
As an example, see below:
```rust
# struct Bar;
# async fn foo() {}
async {
let x = Bar;
foo().await;
drop(x);
}
# ;
```
Here the resulting future will be `Send` if `Bar` is send, since `x` is defined before the await and used afterwards.
If the `drop(x)` line were removed, then `x` would not be considered in auto trait inference because it is not live across the await point.

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.

Besides named variables, temporary values also affect auto trait inference.
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].

## `unsafe` blocks

> **<sup>Syntax</sup>**\
Expand Down Expand Up @@ -189,3 +213,5 @@ fn is_unix_platform() -> bool {
[tuple expressions]: tuple-expr.md
[unsafe operations]: ../unsafety.md
[value expressions]: ../expressions.md#place-expressions-and-value-expressions
[rules as closures]: ../special-types-and-traits.md#auto-traits
[temporary-scopes]: ../destructors.md#temporary-scopes