Skip to content

Commit e172dd2

Browse files
Rewrite closure-of-async to async-closure
1 parent 899eb03 commit e172dd2

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
@@ -219,18 +219,53 @@ impl<'hir> LoweringContext<'_, 'hir> {
219219
*fn_decl_span,
220220
*fn_arg_span,
221221
),
222-
None => self.lower_expr_closure(
223-
binder,
224-
*capture_clause,
225-
e.id,
226-
hir_id,
227-
*constness,
228-
*movability,
229-
fn_decl,
230-
body,
231-
*fn_decl_span,
232-
*fn_arg_span,
233-
),
222+
None => {
223+
let peeled = body.peel_uwu();
224+
if let ast::ExprKind::Gen(
225+
gen_capture_clause,
226+
block,
227+
gen_kind @ ast::GenBlockKind::Async,
228+
span,
229+
) = &peeled.kind
230+
{
231+
let coroutine_kind = match gen_kind {
232+
GenBlockKind::Async => CoroutineKind::Async { span: *span, closure_id: peeled.node_id(), return_impl_trait_id: self.next_node_id() },
233+
GenBlockKind::Gen => CoroutineKind::Gen { span: *span, closure_id: peeled.node_id(), return_impl_trait_id: self.next_node_id() },
234+
GenBlockKind::AsyncGen => CoroutineKind::AsyncGen { span: *span, closure_id: peeled.node_id(), return_impl_trait_id: self.next_node_id() },
235+
};
236+
let id = self.next_node_id();
237+
self.lower_expr_coroutine_closure(
238+
binder,
239+
capture_clause.max(*gen_capture_clause),
240+
e.id,
241+
hir_id,
242+
coroutine_kind,
243+
fn_decl,
244+
&ast::Expr {
245+
id,
246+
span: *span,
247+
kind: ExprKind::Block(block.clone(), None),
248+
attrs: thin_vec![],
249+
tokens: None,
250+
},
251+
*fn_decl_span,
252+
*fn_arg_span,
253+
)
254+
} else {
255+
self.lower_expr_closure(
256+
binder,
257+
*capture_clause,
258+
e.id,
259+
hir_id,
260+
*constness,
261+
*movability,
262+
fn_decl,
263+
body,
264+
*fn_decl_span,
265+
*fn_arg_span,
266+
)
267+
}
268+
}
234269
},
235270
ExprKind::Gen(capture_clause, block, genblock_kind, decl_span) => {
236271
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)