Skip to content

Commit 414dc53

Browse files
committed
Avoid GenFuture shim when compiling async constructs
Previously, async constructs would be lowered to "normal" generators, with an additional `from_generator` / `GenFuture` shim in between to convert from `Generator` to `Future`. The compiler will now special-case these generators internally so that async constructs will *directly* implement `Future` without the need to go through the `from_generator` / `GenFuture` shim. The primary motivation for this change was hiding this implementation detail in stack traces and debuginfo, but it can in theory also help the optimizer as there is less abstractions to see through.
1 parent 0c8e481 commit 414dc53

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

core/src/future/mod.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99
//! [`await`]: ../../std/keyword.await.html
1010
//! [async book]: https://rust-lang.github.io/async-book/
1111
12-
use crate::{
13-
ops::{Generator, GeneratorState},
14-
pin::Pin,
15-
ptr::NonNull,
16-
task::{Context, Poll},
17-
};
12+
use crate::ptr::NonNull;
13+
use crate::task::Context;
1814

1915
mod future;
2016
mod into_future;
@@ -48,6 +44,7 @@ pub use poll_fn::{poll_fn, PollFn};
4844
/// non-Send/Sync as well, and we don't want that.
4945
///
5046
/// It also simplifies the HIR lowering of `.await`.
47+
#[cfg_attr(not(bootstrap), lang = "ResumeTy")]
5148
#[doc(hidden)]
5249
#[unstable(feature = "gen_future", issue = "50547")]
5350
#[derive(Debug, Copy, Clone)]
@@ -64,15 +61,21 @@ unsafe impl Sync for ResumeTy {}
6461
/// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
6562
/// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
6663
// This is `const` to avoid extra errors after we recover from `const async fn`
67-
#[lang = "from_generator"]
64+
#[cfg_attr(bootstrap, lang = "from_generator")]
6865
#[doc(hidden)]
6966
#[unstable(feature = "gen_future", issue = "50547")]
7067
#[rustc_const_unstable(feature = "gen_future", issue = "50547")]
7168
#[inline]
7269
pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
7370
where
74-
T: Generator<ResumeTy, Yield = ()>,
71+
T: crate::ops::Generator<ResumeTy, Yield = ()>,
7572
{
73+
use crate::{
74+
ops::{Generator, GeneratorState},
75+
pin::Pin,
76+
task::Poll,
77+
};
78+
7679
#[rustc_diagnostic_item = "gen_future"]
7780
struct GenFuture<T: Generator<ResumeTy, Yield = ()>>(T);
7881

@@ -109,3 +112,11 @@ pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> {
109112
// that fulfills all the requirements for a mutable reference.
110113
unsafe { &mut *cx.0.as_ptr().cast() }
111114
}
115+
116+
#[cfg_attr(not(bootstrap), lang = "identity_future")]
117+
#[doc(hidden)]
118+
#[unstable(feature = "gen_future", issue = "50547")]
119+
#[inline]
120+
pub const fn identity_future<O, Fut: Future<Output = O>>(f: Fut) -> Fut {
121+
f
122+
}

core/src/task/poll.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::task::Ready;
99
/// scheduled to receive a wakeup instead.
1010
#[must_use = "this `Poll` may be a `Pending` variant, which should be handled"]
1111
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
12+
#[cfg_attr(not(bootstrap), lang = "Poll")]
1213
#[stable(feature = "futures_api", since = "1.36.0")]
1314
pub enum Poll<T> {
1415
/// Represents that a value is immediately ready.

0 commit comments

Comments
 (0)