Skip to content

Commit b603965

Browse files
Rewrite closure-of-async to async-closure
1 parent d3dd34a commit b603965

File tree

3 files changed

+76
-39
lines changed

3 files changed

+76
-39
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,30 @@ impl Expr {
12141214
}
12151215
}
12161216

1217+
pub fn peel_uwu(&self) -> &Expr {
1218+
let mut expr = self;
1219+
loop {
1220+
match &expr.kind {
1221+
ExprKind::Block(blk, None) => {
1222+
if blk.stmts.len() == 1
1223+
&& let StmtKind::Expr(blk) = &blk.stmts[0].kind
1224+
{
1225+
expr = blk;
1226+
} else {
1227+
break;
1228+
}
1229+
}
1230+
ExprKind::Paren(paren) => {
1231+
expr = paren;
1232+
}
1233+
_ => {
1234+
break;
1235+
}
1236+
}
1237+
}
1238+
expr
1239+
}
1240+
12171241
pub fn peel_parens(&self) -> &Expr {
12181242
let mut expr = self;
12191243
while let ExprKind::Paren(inner) = &expr.kind {
@@ -1614,15 +1638,15 @@ pub struct QSelf {
16141638
}
16151639

16161640
/// A capture clause used in closures and `async` blocks.
1617-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
1641+
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic, Ord, PartialOrd)]
16181642
pub enum CaptureBy {
1643+
/// `move` keyword was not specified.
1644+
Ref,
16191645
/// `move |x| y + x`.
16201646
Value {
16211647
/// The span of the `move` keyword.
16221648
move_kw: Span,
16231649
},
1624-
/// `move` keyword was not specified.
1625-
Ref,
16261650
}
16271651

16281652
/// Closure lifetime binder, `for<'a, 'b>` in `for<'a, 'b> |_: &'a (), _: &'b ()|`.

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,53 @@ impl<'hir> LoweringContext<'_, 'hir> {
213213
*fn_decl_span,
214214
*fn_arg_span,
215215
),
216-
None => self.lower_expr_closure(
217-
binder,
218-
*capture_clause,
219-
e.id,
220-
hir_id,
221-
*constness,
222-
*movability,
223-
fn_decl,
224-
body,
225-
*fn_decl_span,
226-
*fn_arg_span,
227-
),
216+
None => {
217+
let peeled = body.peel_uwu();
218+
if let ast::ExprKind::Gen(
219+
gen_capture_clause,
220+
block,
221+
gen_kind @ ast::GenBlockKind::Async,
222+
span,
223+
) = &peeled.kind
224+
{
225+
let coroutine_kind = match gen_kind {
226+
GenBlockKind::Async => CoroutineKind::Async { span: *span, closure_id: peeled.node_id(), return_impl_trait_id: self.next_node_id() },
227+
GenBlockKind::Gen => CoroutineKind::Gen { span: *span, closure_id: peeled.node_id(), return_impl_trait_id: self.next_node_id() },
228+
GenBlockKind::AsyncGen => CoroutineKind::AsyncGen { span: *span, closure_id: peeled.node_id(), return_impl_trait_id: self.next_node_id() },
229+
};
230+
let id = self.next_node_id();
231+
self.lower_expr_coroutine_closure(
232+
binder,
233+
capture_clause.max(*gen_capture_clause),
234+
e.id,
235+
hir_id,
236+
coroutine_kind,
237+
fn_decl,
238+
&ast::Expr {
239+
id,
240+
span: *span,
241+
kind: ExprKind::Block(block.clone(), None),
242+
attrs: thin_vec![],
243+
tokens: None,
244+
},
245+
*fn_decl_span,
246+
*fn_arg_span,
247+
)
248+
} else {
249+
self.lower_expr_closure(
250+
binder,
251+
*capture_clause,
252+
e.id,
253+
hir_id,
254+
*constness,
255+
*movability,
256+
fn_decl,
257+
body,
258+
*fn_decl_span,
259+
*fn_arg_span,
260+
)
261+
}
262+
}
228263
},
229264
ExprKind::Gen(capture_clause, block, genblock_kind, decl_span) => {
230265
let desugaring_kind = match genblock_kind {
Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
error[E0525]: expected a closure that implements the `async Fn` trait, but this closure only implements `async FnOnce`
2-
--> $DIR/wrong-fn-kind.rs:17:20
3-
|
4-
LL | needs_async_fn(move || async move {
5-
| -------------- -^^^^^^
6-
| | |
7-
| _____|______________this closure implements `async FnOnce`, not `async Fn`
8-
| | |
9-
| | required by a bound introduced by this call
10-
LL | |
11-
LL | | println!("{x}");
12-
| | - closure is `async FnOnce` because it moves the variable `x` out of its environment
13-
LL | | });
14-
| |_____- the requirement to implement `async Fn` derives from here
15-
|
16-
note: required by a bound in `needs_async_fn`
17-
--> $DIR/wrong-fn-kind.rs:5:27
18-
|
19-
LL | fn needs_async_fn(_: impl async Fn()) {}
20-
| ^^^^^^^^^^ required by this bound in `needs_async_fn`
21-
221
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
232
--> $DIR/wrong-fn-kind.rs:9:20
243
|
@@ -35,7 +14,6 @@ LL |
3514
LL | x += 1;
3615
| - mutable borrow occurs due to use of `x` in closure
3716

38-
error: aborting due to 2 previous errors
17+
error: aborting due to 1 previous error
3918

40-
Some errors have detailed explanations: E0525, E0596.
41-
For more information about an error, try `rustc --explain E0525`.
19+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)