Skip to content

Commit 4f167b4

Browse files
authored
Rollup merge of rust-lang#121617 - compiler-errors:async-closure-kind-check, r=oli-obk
Actually use the right closure kind when checking async Fn goals Dumb copy-paste mistake on my part from rust-lang#120712. Sorry! r? oli-obk Fixes rust-lang#121599
2 parents 9d5ab73 + ff07f55 commit 4f167b4

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
934934
(trait_ref, Ty::from_closure_kind(tcx, ty::ClosureKind::Fn))
935935
}
936936
ty::Closure(_, args) => {
937-
let sig = args.as_closure().sig();
937+
let args = args.as_closure();
938+
let sig = args.sig();
938939
let trait_ref = sig.map_bound(|sig| {
939940
ty::TraitRef::new(
940941
self.tcx(),
@@ -950,7 +951,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
950951
ty::TraitRef::new(tcx, future_trait_def_id, [sig.output()])
951952
}),
952953
));
953-
(trait_ref, Ty::from_closure_kind(tcx, ty::ClosureKind::Fn))
954+
(trait_ref, args.kind_ty())
954955
}
955956
_ => bug!("expected callable type for AsyncFn candidate"),
956957
};

tests/ui/async-await/async-closures/wrong-fn-kind.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ edition:2021
22

3-
// FIXME(async_closures): This needs a better error message!
4-
53
#![feature(async_closure)]
64

75
fn main() {
@@ -12,4 +10,10 @@ fn main() {
1210
//~^ ERROR expected a closure that implements the `async Fn` trait, but this closure only implements `async FnMut`
1311
x += 1;
1412
});
13+
14+
let x = String::new();
15+
needs_async_fn(move || async move {
16+
//~^ ERROR expected a closure that implements the `async Fn` trait, but this closure only implements `async FnOnce`
17+
println!("{x}");
18+
});
1519
}

tests/ui/async-await/async-closures/wrong-fn-kind.stderr

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0525]: expected a closure that implements the `async Fn` trait, but this closure only implements `async FnMut`
2-
--> $DIR/wrong-fn-kind.rs:11:20
2+
--> $DIR/wrong-fn-kind.rs:9:20
33
|
44
LL | needs_async_fn(async || {
55
| -------------- -^^^^^^^
@@ -14,11 +14,32 @@ LL | | });
1414
| |_____- the requirement to implement `async Fn` derives from here
1515
|
1616
note: required by a bound in `needs_async_fn`
17-
--> $DIR/wrong-fn-kind.rs:8:31
17+
--> $DIR/wrong-fn-kind.rs:6:31
1818
|
1919
LL | fn needs_async_fn(_: impl async Fn()) {}
2020
| ^^^^^^^^^^ required by this bound in `needs_async_fn`
2121

22-
error: aborting due to 1 previous error
22+
error[E0525]: expected a closure that implements the `async Fn` trait, but this closure only implements `async FnOnce`
23+
--> $DIR/wrong-fn-kind.rs:15:20
24+
|
25+
LL | needs_async_fn(move || async move {
26+
| -------------- -^^^^^^
27+
| | |
28+
| _____|______________this closure implements `async FnOnce`, not `async Fn`
29+
| | |
30+
| | required by a bound introduced by this call
31+
LL | |
32+
LL | | println!("{x}");
33+
| | - closure is `async FnOnce` because it moves the variable `x` out of its environment
34+
LL | | });
35+
| |_____- the requirement to implement `async Fn` derives from here
36+
|
37+
note: required by a bound in `needs_async_fn`
38+
--> $DIR/wrong-fn-kind.rs:6:31
39+
|
40+
LL | fn needs_async_fn(_: impl async Fn()) {}
41+
| ^^^^^^^^^^ required by this bound in `needs_async_fn`
42+
43+
error: aborting due to 2 previous errors
2344

2445
For more information about this error, try `rustc --explain E0525`.

0 commit comments

Comments
 (0)