Skip to content

Commit 71e0d7c

Browse files
committed
Auto merge of #127827 - compiler-errors:async-closure-closure-async, r=<try>
[EXPERIMENT] Rewrite closure-of-async to async-closure to see what the fallout is, to gauge how forwards-compatible `async || {}` is w/ `|| async {}`. uwuwuwu
2 parents 899eb03 + ce86e83 commit 71e0d7c

File tree

8 files changed

+129
-66
lines changed

8 files changed

+129
-66
lines changed

compiler/rustc_ast/src/ast.rs

+28-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,16 @@ 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, Ord, Eq, PartialOrd, Debug)]
1642+
#[derive(Encodable, Decodable, HashStable_Generic)]
16181643
pub enum CaptureBy {
1644+
/// `move` keyword was not specified.
1645+
Ref,
16191646
/// `move |x| y + x`.
16201647
Value {
16211648
/// The span of the `move` keyword.
16221649
move_kw: Span,
16231650
},
1624-
/// `move` keyword was not specified.
1625-
Ref,
16261651
}
16271652

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

compiler/rustc_ast_lowering/src/expr.rs

+59-12
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,65 @@ 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+
_,
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 {
233+
span: *span,
234+
closure_id: peeled.node_id(),
235+
return_impl_trait_id: self.next_node_id(),
236+
},
237+
GenBlockKind::Gen => CoroutineKind::Gen {
238+
span: *span,
239+
closure_id: peeled.node_id(),
240+
return_impl_trait_id: self.next_node_id(),
241+
},
242+
GenBlockKind::AsyncGen => CoroutineKind::AsyncGen {
243+
span: *span,
244+
closure_id: peeled.node_id(),
245+
return_impl_trait_id: self.next_node_id(),
246+
},
247+
};
248+
let id = self.next_node_id();
249+
self.lower_expr_coroutine_closure(
250+
binder,
251+
*capture_clause,
252+
e.id,
253+
hir_id,
254+
coroutine_kind,
255+
fn_decl,
256+
&ast::Expr {
257+
id,
258+
span: *span,
259+
kind: ExprKind::Block(block.clone(), None),
260+
attrs: thin_vec![],
261+
tokens: None,
262+
},
263+
*fn_decl_span,
264+
*fn_arg_span,
265+
)
266+
} else {
267+
self.lower_expr_closure(
268+
binder,
269+
*capture_clause,
270+
e.id,
271+
hir_id,
272+
*constness,
273+
*movability,
274+
fn_decl,
275+
body,
276+
*fn_decl_span,
277+
*fn_arg_span,
278+
)
279+
}
280+
}
234281
},
235282
ExprKind::Gen(capture_clause, block, genblock_kind, decl_span) => {
236283
let desugaring_kind = match genblock_kind {

compiler/rustc_hir_typeck/src/closure.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::span_bug;
1414
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
1515
use rustc_middle::ty::{self, GenericArgs, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
1616
use rustc_span::def_id::LocalDefId;
17-
use rustc_span::Span;
17+
use rustc_span::{Span, DUMMY_SP};
1818
use rustc_target::spec::abi::Abi;
1919
use rustc_trait_selection::error_reporting::traits::ArgKind;
2020
use rustc_trait_selection::traits;
@@ -563,25 +563,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
563563
return None;
564564
};
565565

566+
let mut return_ty = None;
567+
566568
// FIXME: We may want to elaborate here, though I assume this will be exceedingly rare.
567569
for bound in self.obligations_for_self_ty(return_vid) {
568570
if let Some(ret_projection) = bound.predicate.as_projection_clause()
569571
&& let Some(ret_projection) = ret_projection.no_bound_vars()
570572
&& self.tcx.is_lang_item(ret_projection.def_id(), LangItem::FutureOutput)
571573
{
572-
let sig = projection.rebind(self.tcx.mk_fn_sig(
573-
input_tys,
574-
ret_projection.term.expect_type(),
575-
false,
576-
hir::Safety::Safe,
577-
Abi::Rust,
578-
));
579-
580-
return Some(ExpectedSig { cause_span, sig });
574+
return_ty = Some(ret_projection.term.expect_type());
581575
}
582576
}
583577

584-
None
578+
let sig = projection.rebind(self.tcx.mk_fn_sig(
579+
input_tys,
580+
return_ty.unwrap_or_else(|| self.next_ty_var(cause_span.unwrap_or(DUMMY_SP))),
581+
false,
582+
hir::Safety::Safe,
583+
Abi::Rust,
584+
));
585+
586+
return Some(ExpectedSig { cause_span, sig });
585587
}
586588

587589
fn sig_of_closure(

tests/ui/async-await/async-closures/without-precise-captures-we-are-powerless.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ LL | outlives::<'a>(call_once(c));
5656
LL |
5757
LL | let c = async move || { println!("{}", *x.0); };
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move out of `x` occurs here
59+
|
60+
help: consider cloning the value if the performance cost is acceptable
61+
|
62+
LL | let c = async || { println!("{}", *x.0); }.clone();
63+
| ++++++++
5964

6065
error[E0597]: `c` does not live long enough
6166
--> $DIR/without-precise-captures-we-are-powerless.rs:33:20
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`.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
error: captured variable cannot escape `FnMut` closure body
2-
--> $DIR/issue-69446-fnmut-capture.rs:19:17
1+
error: async closure does not implement `FnMut` because it captures state from its environment
2+
--> $DIR/issue-69446-fnmut-capture.rs:19:9
33
|
4-
LL | let mut x = Foo;
5-
| ----- variable defined here
6-
LL | bar(move || async {
7-
| _______________-_^
8-
| | |
9-
| | inferred to be a `FnMut` closure
10-
LL | | x.foo();
11-
| | - variable captured here
12-
LL | | });
13-
| |_____^ returns an `async` block that contains a reference to a captured variable, which then escapes the closure body
4+
LL | bar(move || async {
5+
| --- ^^^^^^^
6+
| |
7+
| required by a bound introduced by this call
148
|
15-
= note: `FnMut` closures only have access to their captured variables while they are executing...
16-
= note: ...therefore, they cannot allow references to captured variables to escape
9+
note: required by a bound in `bar`
10+
--> $DIR/issue-69446-fnmut-capture.rs:12:25
11+
|
12+
LL | async fn bar<T>(_: impl FnMut() -> T)
13+
| ^^^^^^^^^^^^ required by this bound in `bar`
1714

1815
error: aborting due to 1 previous error
1916

tests/ui/async-await/issue-70935-complex-spans.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ note: required because it appears within the type `NotSync`
1313
LL | struct NotSync(PhantomData<*mut ()>);
1414
| ^^^^^^^
1515
= note: required for `&NotSync` to implement `Send`
16+
= note: required because it appears within the type `(&NotSync,)`
1617
note: required because it's used within this closure
1718
--> $DIR/issue-70935-complex-spans.rs:19:13
1819
|
@@ -46,6 +47,7 @@ note: required because it appears within the type `NotSync`
4647
LL | struct NotSync(PhantomData<*mut ()>);
4748
| ^^^^^^^
4849
= note: required for `&NotSync` to implement `Send`
50+
= note: required because it appears within the type `(&NotSync,)`
4951
note: required because it's used within this closure
5052
--> $DIR/issue-70935-complex-spans.rs:19:13
5153
|
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/opaque-cast-field-access-in-future.rs:7:14
3+
|
4+
LL | &mut foo.bar;
5+
| ^^^ cannot infer type
6+
17
error[E0283]: type annotations needed
28
--> $DIR/opaque-cast-field-access-in-future.rs:22:17
39
|
@@ -6,6 +12,7 @@ LL | fn run() -> Foo<impl Future<Output = ()>> {
612
|
713
= note: cannot satisfy `_: Future`
814

9-
error: aborting due to 1 previous error
15+
error: aborting due to 2 previous errors
1016

11-
For more information about this error, try `rustc --explain E0283`.
17+
Some errors have detailed explanations: E0282, E0283.
18+
For more information about an error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)