Skip to content

Commit b8405a9

Browse files
Rewrite closure-of-async to async-closure
1 parent eb10639 commit b8405a9

File tree

3 files changed

+76
-39
lines changed

3 files changed

+76
-39
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+27-3
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 ()|`.

Diff for: compiler/rustc_ast_lowering/src/expr.rs

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