-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Avoid GenFuture
shim when compiling async constructs
#104321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,8 @@ | |
//! [`await`]: ../../std/keyword.await.html | ||
//! [async book]: https://rust-lang.github.io/async-book/ | ||
use crate::{ | ||
ops::{Generator, GeneratorState}, | ||
pin::Pin, | ||
ptr::NonNull, | ||
task::{Context, Poll}, | ||
}; | ||
use crate::ptr::NonNull; | ||
use crate::task::Context; | ||
|
||
mod future; | ||
mod into_future; | ||
|
@@ -48,6 +44,7 @@ pub use poll_fn::{poll_fn, PollFn}; | |
/// non-Send/Sync as well, and we don't want that. | ||
/// | ||
/// It also simplifies the HIR lowering of `.await`. | ||
#[cfg_attr(not(bootstrap), lang = "ResumeTy")] | ||
#[doc(hidden)] | ||
#[unstable(feature = "gen_future", issue = "50547")] | ||
#[derive(Debug, Copy, Clone)] | ||
|
@@ -64,15 +61,21 @@ unsafe impl Sync for ResumeTy {} | |
/// 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` | ||
#[lang = "from_generator"] | ||
#[cfg_attr(bootstrap, lang = "from_generator")] | ||
#[doc(hidden)] | ||
#[unstable(feature = "gen_future", issue = "50547")] | ||
#[rustc_const_unstable(feature = "gen_future", issue = "50547")] | ||
#[inline] | ||
pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return> | ||
where | ||
T: Generator<ResumeTy, Yield = ()>, | ||
T: crate::ops::Generator<ResumeTy, Yield = ()>, | ||
{ | ||
use crate::{ | ||
ops::{Generator, GeneratorState}, | ||
pin::Pin, | ||
task::Poll, | ||
}; | ||
|
||
#[rustc_diagnostic_item = "gen_future"] | ||
struct GenFuture<T: Generator<ResumeTy, Yield = ()>>(T); | ||
|
||
|
@@ -109,3 +112,11 @@ pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> { | |
// that fulfills all the requirements for a mutable reference. | ||
unsafe { &mut *cx.0.as_ptr().cast() } | ||
} | ||
|
||
#[cfg_attr(not(bootstrap), lang = "identity_future")] | ||
#[doc(hidden)] | ||
#[unstable(feature = "gen_future", issue = "50547")] | ||
#[inline] | ||
pub const fn identity_future<O, Fut: Future<Output = O>>(f: Fut) -> Fut { | ||
Comment on lines
+116
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please open an issue reminding us that we should get rid of this function and look into what borrowck needs to handle this correctly without the identity function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you go: #104826 |
||
f | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,54 @@ | ||
error[E0277]: the trait bound `[static generator@$DIR/async.rs:7:29: 9:2]: Generator<ResumeTy>` is not satisfied | ||
error[E0277]: `impl Future<Output = u32>` is not a future | ||
--> $DIR/async.rs:7:29 | ||
| | ||
LL | async fn foo(x: u32) -> u32 { | ||
| _____________________________^ | ||
| _____________________________- | ||
LL | | x | ||
LL | | } | ||
| |_^ the trait `Generator<ResumeTy>` is not implemented for `[static generator@$DIR/async.rs:7:29: 9:2]` | ||
| | ^ | ||
| | | | ||
| |_`impl Future<Output = u32>` is not a future | ||
| required by a bound introduced by this call | ||
| | ||
note: required by a bound in `std::future::from_generator` | ||
= help: the trait `Future` is not implemented for `impl Future<Output = u32>` | ||
= note: impl Future<Output = u32> must be a future or must implement `IntoFuture` to be awaited | ||
note: required by a bound in `identity_future` | ||
--> $SRC_DIR/core/src/future/mod.rs:LL:COL | ||
| | ||
LL | T: Generator<ResumeTy, Yield = ()>, | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `std::future::from_generator` | ||
LL | pub const fn identity_future<O, Fut: Future<Output = O>>(f: Fut) -> Fut { | ||
| ^^^^^^^^^^^^^^^^^^ required by this bound in `identity_future` | ||
|
||
error[E0280]: the requirement `<[static generator@$DIR/async.rs:7:29: 9:2] as Generator<ResumeTy>>::Yield == ()` is not satisfied | ||
error[E0277]: the size for values of type `<impl Future<Output = u32> as Future>::Output` cannot be known at compilation time | ||
--> $DIR/async.rs:7:29 | ||
| | ||
LL | async fn foo(x: u32) -> u32 { | ||
| _____________________________^ | ||
LL | | x | ||
LL | | } | ||
| |_^ | ||
| |_^ doesn't have a size known at compile-time | ||
| | ||
note: required by a bound in `std::future::from_generator` | ||
= help: the trait `Sized` is not implemented for `<impl Future<Output = u32> as Future>::Output` | ||
note: required by a bound in `identity_future` | ||
--> $SRC_DIR/core/src/future/mod.rs:LL:COL | ||
| | ||
LL | T: Generator<ResumeTy, Yield = ()>, | ||
| ^^^^^^^^^^ required by this bound in `std::future::from_generator` | ||
LL | pub const fn identity_future<O, Fut: Future<Output = O>>(f: Fut) -> Fut { | ||
| ^ required by this bound in `identity_future` | ||
|
||
error[E0277]: `impl Future<Output = u32>` is not a future | ||
--> $DIR/async.rs:7:25 | ||
| | ||
LL | async fn foo(x: u32) -> u32 { | ||
| ^^^ `impl Future<Output = u32>` is not a future | ||
| | ||
= help: the trait `Future` is not implemented for `impl Future<Output = u32>` | ||
= note: impl Future<Output = u32> must be a future or must implement `IntoFuture` to be awaited | ||
|
||
error[E0280]: the requirement `<impl Future<Output = u32> as Future>::Output == u32` is not satisfied | ||
--> $DIR/async.rs:7:25 | ||
| | ||
LL | async fn foo(x: u32) -> u32 { | ||
| ^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can invoke https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/struct.Binder.html#method.no_bound_vars instead of
skip_binder
and.expect
the option to make sure if this condition is violated, the compiler ICEs instead of doing weird things.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An
expect
there actually does panic for thesrc/test/ui/generator/resume-arg-late-bound.rs
test.I only moved this code between the files, the same comment and code pattern exists as well for normal closures:
rust/compiler/rustc_trait_selection/src/traits/select/mod.rs
Line 2290 in e221616