Skip to content

Commit 4b35388

Browse files
committed
Document auto trait inference for async blocks
We are in the process of changing this (rust-lang/#69663), but it would be good to document the existing rules before changing them. This should also help explain the compilation errors people are getting in the meantime.
1 parent 1374727 commit 4b35388

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/expressions/block-expr.md

+20
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ loop {
117117
}
118118
```
119119
120+
### Auto traits and `async` blocks
121+
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+
124+
```rust
125+
#fn bar() -> i32 { 42 }
126+
#async fn foo() {}
127+
async {
128+
match bar() {
129+
_ => foo().await,
130+
}
131+
}
132+
#;
133+
```
134+
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+
120138
## `unsafe` blocks
121139

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

0 commit comments

Comments
 (0)