Skip to content

Commit f8ec29c

Browse files
committed
Auto merge of rust-lang#120717 - compiler-errors:cap-closure-kind, r=oli-obk
For async closures, cap closure kind, get rid of `by_mut_body` Right now we have three `AsyncFn*` traits, and three corresponding futures that are returned by the `call_*` functions for them. This is fine, but it is a bit excessive, since the future returned by `AsyncFn` and `AsyncFnMut` are identical. Really, the only distinction we need to make with these bodies is "by ref" and "by move". This PR removes `AsyncFn::CallFuture` and renames `AsyncFnMut::CallMutFuture` to `AsyncFnMut::CallRefFuture`. This simplifies MIR building for async closures, since we don't need to build an extra "by mut" body, but just a "by move" body which is materially different. We need to do a bit of delicate handling of the ClosureKind for async closures, since we need to "cap" it to `AsyncFnMut` in some cases when we only care about what body we're looking for. This also fixes a bug where `<{async closure} as Fn>::call` was returning a body that takes the async-closure receiver *by move*. This also helps align the `AsyncFn` traits to the `LendingFn` traits' eventual designs.
2 parents db8c896 + 96d7f0c commit f8ec29c

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

alloc/src/boxed.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2042,18 +2042,16 @@ impl<Args: Tuple, F: AsyncFnOnce<Args> + ?Sized, A: Allocator> AsyncFnOnce<Args>
20422042

20432043
#[unstable(feature = "async_fn_traits", issue = "none")]
20442044
impl<Args: Tuple, F: AsyncFnMut<Args> + ?Sized, A: Allocator> AsyncFnMut<Args> for Box<F, A> {
2045-
type CallMutFuture<'a> = F::CallMutFuture<'a> where Self: 'a;
2045+
type CallRefFuture<'a> = F::CallRefFuture<'a> where Self: 'a;
20462046

2047-
extern "rust-call" fn async_call_mut(&mut self, args: Args) -> Self::CallMutFuture<'_> {
2047+
extern "rust-call" fn async_call_mut(&mut self, args: Args) -> Self::CallRefFuture<'_> {
20482048
F::async_call_mut(self, args)
20492049
}
20502050
}
20512051

20522052
#[unstable(feature = "async_fn_traits", issue = "none")]
20532053
impl<Args: Tuple, F: AsyncFn<Args> + ?Sized, A: Allocator> AsyncFn<Args> for Box<F, A> {
2054-
type CallFuture<'a> = F::CallFuture<'a> where Self: 'a;
2055-
2056-
extern "rust-call" fn async_call(&self, args: Args) -> Self::CallFuture<'_> {
2054+
extern "rust-call" fn async_call(&self, args: Args) -> Self::CallRefFuture<'_> {
20572055
F::async_call(self, args)
20582056
}
20592057
}

core/src/ops/async_function.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,9 @@ use crate::marker::Tuple;
1010
#[must_use = "async closures are lazy and do nothing unless called"]
1111
#[lang = "async_fn"]
1212
pub trait AsyncFn<Args: Tuple>: AsyncFnMut<Args> {
13-
/// Future returned by [`AsyncFn::async_call`].
14-
#[unstable(feature = "async_fn_traits", issue = "none")]
15-
type CallFuture<'a>: Future<Output = Self::Output>
16-
where
17-
Self: 'a;
18-
1913
/// Call the [`AsyncFn`], returning a future which may borrow from the called closure.
2014
#[unstable(feature = "async_fn_traits", issue = "none")]
21-
extern "rust-call" fn async_call(&self, args: Args) -> Self::CallFuture<'_>;
15+
extern "rust-call" fn async_call(&self, args: Args) -> Self::CallRefFuture<'_>;
2216
}
2317

2418
/// An async-aware version of the [`FnMut`](crate::ops::FnMut) trait.
@@ -30,15 +24,15 @@ pub trait AsyncFn<Args: Tuple>: AsyncFnMut<Args> {
3024
#[must_use = "async closures are lazy and do nothing unless called"]
3125
#[lang = "async_fn_mut"]
3226
pub trait AsyncFnMut<Args: Tuple>: AsyncFnOnce<Args> {
33-
/// Future returned by [`AsyncFnMut::async_call_mut`].
27+
/// Future returned by [`AsyncFnMut::async_call_mut`] and [`AsyncFn::async_call`].
3428
#[unstable(feature = "async_fn_traits", issue = "none")]
35-
type CallMutFuture<'a>: Future<Output = Self::Output>
29+
type CallRefFuture<'a>: Future<Output = Self::Output>
3630
where
3731
Self: 'a;
3832

3933
/// Call the [`AsyncFnMut`], returning a future which may borrow from the called closure.
4034
#[unstable(feature = "async_fn_traits", issue = "none")]
41-
extern "rust-call" fn async_call_mut(&mut self, args: Args) -> Self::CallMutFuture<'_>;
35+
extern "rust-call" fn async_call_mut(&mut self, args: Args) -> Self::CallRefFuture<'_>;
4236
}
4337

4438
/// An async-aware version of the [`FnOnce`](crate::ops::FnOnce) trait.
@@ -72,9 +66,7 @@ mod impls {
7266
where
7367
F: AsyncFn<A>,
7468
{
75-
type CallFuture<'a> = F::CallFuture<'a> where Self: 'a;
76-
77-
extern "rust-call" fn async_call(&self, args: A) -> Self::CallFuture<'_> {
69+
extern "rust-call" fn async_call(&self, args: A) -> Self::CallRefFuture<'_> {
7870
F::async_call(*self, args)
7971
}
8072
}
@@ -84,9 +76,9 @@ mod impls {
8476
where
8577
F: AsyncFn<A>,
8678
{
87-
type CallMutFuture<'a> = F::CallFuture<'a> where Self: 'a;
79+
type CallRefFuture<'a> = F::CallRefFuture<'a> where Self: 'a;
8880

89-
extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallMutFuture<'_> {
81+
extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallRefFuture<'_> {
9082
F::async_call(*self, args)
9183
}
9284
}
@@ -97,7 +89,7 @@ mod impls {
9789
F: AsyncFn<A>,
9890
{
9991
type Output = F::Output;
100-
type CallOnceFuture = F::CallFuture<'a>;
92+
type CallOnceFuture = F::CallRefFuture<'a>;
10193

10294
extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture {
10395
F::async_call(self, args)
@@ -109,9 +101,9 @@ mod impls {
109101
where
110102
F: AsyncFnMut<A>,
111103
{
112-
type CallMutFuture<'a> = F::CallMutFuture<'a> where Self: 'a;
104+
type CallRefFuture<'a> = F::CallRefFuture<'a> where Self: 'a;
113105

114-
extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallMutFuture<'_> {
106+
extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallRefFuture<'_> {
115107
F::async_call_mut(*self, args)
116108
}
117109
}
@@ -122,7 +114,7 @@ mod impls {
122114
F: AsyncFnMut<A>,
123115
{
124116
type Output = F::Output;
125-
type CallOnceFuture = F::CallMutFuture<'a>;
117+
type CallOnceFuture = F::CallRefFuture<'a>;
126118

127119
extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture {
128120
F::async_call_mut(self, args)

0 commit comments

Comments
 (0)