about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2020-03-21 05:33:15 +0100
committerGitHub <noreply@github.com>2020-03-21 05:33:15 +0100
commitef7c8a158f5bb36e3f5b96eacc196061e8ab9d10 (patch)
treef480ee62934374e6458d5da35bf480d2e60336a2 /src/libstd
parent4b91729df22015bd412f6fc0fa397785d1e2159c (diff)
parentdb0126a7c251f4cca39ad4c50527e88c97190992 (diff)
downloadrust-ef7c8a158f5bb36e3f5b96eacc196061e8ab9d10.tar.gz
rust-ef7c8a158f5bb36e3f5b96eacc196061e8ab9d10.zip
Rollup merge of #69033 - jonas-schievink:resume-with-context, r=tmandry
Use generator resume arguments in the async/await lowering

This removes the TLS requirement from async/await and enables it in `#![no_std]` crates.

Closes https://github.com/rust-lang/rust/issues/56974

I'm not confident the HIR lowering is completely correct, there seem to be quite a few undocumented invariants in there. The `async-std` and tokio test suites are passing with these changes though.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/future.rs25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/libstd/future.rs b/src/libstd/future.rs
index c1ca6771326..c0675eeba98 100644
--- a/src/libstd/future.rs
+++ b/src/libstd/future.rs
@@ -1,12 +1,14 @@
 //! Asynchronous values.
 
-use core::cell::Cell;
-use core::marker::Unpin;
-use core::ops::{Drop, Generator, GeneratorState};
-use core::option::Option;
-use core::pin::Pin;
-use core::ptr::NonNull;
-use core::task::{Context, Poll};
+#[cfg(bootstrap)]
+use core::{
+    cell::Cell,
+    marker::Unpin,
+    ops::{Drop, Generator, GeneratorState},
+    pin::Pin,
+    ptr::NonNull,
+    task::{Context, Poll},
+};
 
 #[doc(inline)]
 #[stable(feature = "futures_api", since = "1.36.0")]
@@ -17,6 +19,7 @@ pub use core::future::*;
 /// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
 /// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
 // This is `const` to avoid extra errors after we recover from `const async fn`
+#[cfg(bootstrap)]
 #[doc(hidden)]
 #[unstable(feature = "gen_future", issue = "50547")]
 pub const fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
@@ -24,6 +27,7 @@ pub const fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Outpu
 }
 
 /// A wrapper around generators used to implement `Future` for `async`/`await` code.
+#[cfg(bootstrap)]
 #[doc(hidden)]
 #[unstable(feature = "gen_future", issue = "50547")]
 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
@@ -31,8 +35,10 @@ struct GenFuture<T: Generator<Yield = ()>>(T);
 
 // We rely on the fact that async/await futures are immovable in order to create
 // self-referential borrows in the underlying generator.
+#[cfg(bootstrap)]
 impl<T: Generator<Yield = ()>> !Unpin for GenFuture<T> {}
 
+#[cfg(bootstrap)]
 #[doc(hidden)]
 #[unstable(feature = "gen_future", issue = "50547")]
 impl<T: Generator<Yield = ()>> Future for GenFuture<T> {
@@ -48,12 +54,15 @@ impl<T: Generator<Yield = ()>> Future for GenFuture<T> {
     }
 }
 
+#[cfg(bootstrap)]
 thread_local! {
     static TLS_CX: Cell<Option<NonNull<Context<'static>>>> = Cell::new(None);
 }
 
+#[cfg(bootstrap)]
 struct SetOnDrop(Option<NonNull<Context<'static>>>);
 
+#[cfg(bootstrap)]
 impl Drop for SetOnDrop {
     fn drop(&mut self) {
         TLS_CX.with(|tls_cx| {
@@ -64,6 +73,7 @@ impl Drop for SetOnDrop {
 
 // Safety: the returned guard must drop before `cx` is dropped and before
 // any previous guard is dropped.
+#[cfg(bootstrap)]
 unsafe fn set_task_context(cx: &mut Context<'_>) -> SetOnDrop {
     // transmute the context's lifetime to 'static so we can store it.
     let cx = core::mem::transmute::<&mut Context<'_>, &mut Context<'static>>(cx);
@@ -71,6 +81,7 @@ unsafe fn set_task_context(cx: &mut Context<'_>) -> SetOnDrop {
     SetOnDrop(old_cx)
 }
 
+#[cfg(bootstrap)]
 #[doc(hidden)]
 #[unstable(feature = "gen_future", issue = "50547")]
 /// Polls a future in the current thread-local task waker.