Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
@@ -208,6 +208,10 @@ language_item_table! {
FnMut, sym::fn_mut, fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
FnOnce, sym::fn_once, fn_once_trait, Target::Trait, GenericRequirement::Exact(1);

AsyncFn, sym::async_fn, async_fn_trait, Target::Trait, GenericRequirement::Exact(1);
AsyncFnMut, sym::async_fn_mut, async_fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
AsyncFnOnce, sym::async_fn_once, async_fn_once_trait, Target::Trait, GenericRequirement::Exact(1);

FnOnceOutput, sym::fn_once_output, fn_once_output, Target::AssocTy, GenericRequirement::None;

Iterator, sym::iterator, iterator_trait, Target::Trait, GenericRequirement::Exact(0);
11 changes: 11 additions & 0 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
@@ -220,6 +220,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(self.tcx.lang_items().fn_trait(), Ident::with_dummy_span(sym::call), true),
(self.tcx.lang_items().fn_mut_trait(), Ident::with_dummy_span(sym::call_mut), true),
(self.tcx.lang_items().fn_once_trait(), Ident::with_dummy_span(sym::call_once), false),
(self.tcx.lang_items().async_fn_trait(), Ident::with_dummy_span(sym::async_call), true),
(
self.tcx.lang_items().async_fn_mut_trait(),
Ident::with_dummy_span(sym::async_call_mut),
true,
),
(
self.tcx.lang_items().async_fn_once_trait(),
Ident::with_dummy_span(sym::async_call_once),
false,
),
] {
let Some(trait_def_id) = opt_trait_def_id else { continue };

6 changes: 6 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
@@ -423,8 +423,14 @@ symbols! {
assume,
assume_init,
async_await,
async_call,
async_call_mut,
async_call_once,
async_closure,
async_fn,
async_fn_in_trait,
async_fn_mut,
async_fn_once,
async_fn_track_caller,
async_for_loop,
async_iterator,
108 changes: 108 additions & 0 deletions library/core/src/ops/async_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use crate::future::Future;
use crate::marker::Tuple;

/// An async-aware version of the [`Fn`](crate::ops::Fn) trait.
///
/// All `async fn` and functions returning futures implement this trait.
#[unstable(feature = "async_fn_traits", issue = "none")]
#[rustc_paren_sugar]
#[fundamental]
#[must_use = "async closures are lazy and do nothing unless called"]
#[cfg_attr(not(bootstrap), lang = "async_fn")]
pub trait AsyncFn<Args: Tuple>: AsyncFnMut<Args> {
/// Future returned by [`AsyncFn::async_call`].
#[unstable(feature = "async_fn_traits", issue = "none")]
type CallFuture<'a>: Future<Output = Self::Output>
where
Self: 'a;

/// Call the [`AsyncFn`], returning a future which may borrow from the called closure.
#[unstable(feature = "async_fn_traits", issue = "none")]
extern "rust-call" fn async_call(&self, args: Args) -> Self::CallFuture<'_>;
}

/// An async-aware version of the [`FnMut`](crate::ops::FnMut) trait.
///
/// All `async fn` and functions returning futures implement this trait.
#[unstable(feature = "async_fn_traits", issue = "none")]
#[rustc_paren_sugar]
#[fundamental]
#[must_use = "async closures are lazy and do nothing unless called"]
#[cfg_attr(not(bootstrap), lang = "async_fn_mut")]
pub trait AsyncFnMut<Args: Tuple>: AsyncFnOnce<Args> {
/// Future returned by [`AsyncFnMut::async_call_mut`].
#[unstable(feature = "async_fn_traits", issue = "none")]
type CallMutFuture<'a>: Future<Output = Self::Output>
where
Self: 'a;

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

/// An async-aware version of the [`FnOnce`](crate::ops::FnOnce) trait.
///
/// All `async fn` and functions returning futures implement this trait.
#[unstable(feature = "async_fn_traits", issue = "none")]
#[rustc_paren_sugar]
#[fundamental]
#[must_use = "async closures are lazy and do nothing unless called"]
#[cfg_attr(not(bootstrap), lang = "async_fn_once")]
pub trait AsyncFnOnce<Args: Tuple> {
/// Future returned by [`AsyncFnOnce::async_call_once`].
#[unstable(feature = "async_fn_traits", issue = "none")]
type CallOnceFuture: Future<Output = Self::Output>;

/// Output type of the called closure's future.
#[unstable(feature = "async_fn_traits", issue = "none")]
type Output;

/// Call the [`AsyncFnOnce`], returning a future which may move out of the called closure.
#[unstable(feature = "async_fn_traits", issue = "none")]
extern "rust-call" fn async_call_once(self, args: Args) -> Self::CallOnceFuture;
}

mod impls {
use super::{AsyncFn, AsyncFnMut, AsyncFnOnce};
use crate::future::Future;
use crate::marker::Tuple;

#[unstable(feature = "async_fn_traits", issue = "none")]
impl<F: Fn<A>, A: Tuple> AsyncFn<A> for F
where
<F as FnOnce<A>>::Output: Future,
{
type CallFuture<'a> = <F as FnOnce<A>>::Output where Self: 'a;

extern "rust-call" fn async_call(&self, args: A) -> Self::CallFuture<'_> {
self.call(args)
}
}

#[unstable(feature = "async_fn_traits", issue = "none")]
impl<F: FnMut<A>, A: Tuple> AsyncFnMut<A> for F
where
<F as FnOnce<A>>::Output: Future,
{
type CallMutFuture<'a> = <F as FnOnce<A>>::Output where Self: 'a;

extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallMutFuture<'_> {
self.call_mut(args)
}
}

#[unstable(feature = "async_fn_traits", issue = "none")]
impl<F: FnOnce<A>, A: Tuple> AsyncFnOnce<A> for F
where
<F as FnOnce<A>>::Output: Future,
{
type CallOnceFuture = <F as FnOnce<A>>::Output;

type Output = <<F as FnOnce<A>>::Output as Future>::Output;

extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture {
self.call_once(args)
}
}
}
4 changes: 4 additions & 0 deletions library/core/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -139,6 +139,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

mod arith;
mod async_function;
mod bit;
mod control_flow;
mod coroutine;
@@ -173,6 +174,9 @@ pub use self::drop::Drop;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::function::{Fn, FnMut, FnOnce};

#[unstable(feature = "async_fn_traits", issue = "none")]
pub use self::async_function::{AsyncFn, AsyncFnMut, AsyncFnOnce};

#[stable(feature = "rust1", since = "1.0.0")]
pub use self::index::{Index, IndexMut};

16 changes: 16 additions & 0 deletions tests/ui/async-await/async-fn/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// edition: 2021
// check-pass

#![feature(async_fn_traits)]

use std::ops::AsyncFn;

async fn foo() {}

async fn call_asyncly(f: impl AsyncFn(i32) -> i32) -> i32 {
f(1).await
}

fn main() {
let fut = call_asyncly(|x| async move { x + 1 });
}
9 changes: 8 additions & 1 deletion tests/ui/did_you_mean/bad-assoc-ty.stderr
Original file line number Diff line number Diff line change
@@ -191,7 +191,14 @@ error[E0223]: ambiguous associated type
--> $DIR/bad-assoc-ty.rs:33:10
|
LL | type H = Fn(u8) -> (u8)::Output;
| ^^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<(dyn Fn(u8) -> u8 + 'static) as IntoFuture>::Output`
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: use fully-qualified syntax
|
LL | type H = <(dyn Fn(u8) -> u8 + 'static) as AsyncFnOnce>::Output;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL | type H = <(dyn Fn(u8) -> u8 + 'static) as IntoFuture>::Output;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0223]: ambiguous associated type
--> $DIR/bad-assoc-ty.rs:39:19