Skip to content

Uncomment test code for failure to use Box::pin #71781

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

Merged
merged 1 commit into from
May 2, 2020
Merged
Show file tree
Hide file tree
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
35 changes: 21 additions & 14 deletions src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ fn foo<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32>
x //~ ERROR mismatched types
}

// FIXME: uncomment these once this commit is in Beta and we can rely on `rustc_on_unimplemented`
// having filtering for `Self` being a trait.
//
// fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
// Box::new(x)
// }
//
// fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
// Pin::new(x)
// }
//
// fn qux<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
// Pin::new(Box::new(x))
// }
// This case is still subpar:
// `Pin::new(x)`: store this in the heap by calling `Box::new`: `Box::new(x)`
// Should suggest changing the code from `Pin::new` to `Box::pin`.
fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
Box::new(x) //~ ERROR mismatched types
}

fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
Pin::new(x) //~ ERROR mismatched types
//~^ ERROR E0277
}

fn qux<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
Pin::new(Box::new(x)) //~ ERROR E0277
}

fn zap() -> BoxFuture<'static, i32> {
async { //~ ERROR mismatched types
42
}
}

fn main() {}
76 changes: 74 additions & 2 deletions src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,78 @@ LL | x
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error: aborting due to previous error
error[E0308]: mismatched types
--> $DIR/expected-boxed-future-isnt-pinned.rs:18:5
|
LL | fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
| ----------------------- expected `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>` because of return type
LL | Box::new(x)
| ^^^^^^^^^^^ expected struct `std::pin::Pin`, found struct `std::boxed::Box`
|
= note: expected struct `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>`
found struct `std::boxed::Box<F>`
= help: use `Box::pin`

error[E0308]: mismatched types
--> $DIR/expected-boxed-future-isnt-pinned.rs:22:14
|
LL | fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
| - this type parameter
LL | Pin::new(x)
| ^
| |
| expected struct `std::boxed::Box`, found type parameter `F`
| help: store this in the heap by calling `Box::new`: `Box::new(x)`
|
= note: expected struct `std::boxed::Box<dyn std::future::Future<Output = i32> + std::marker::Send>`
found type parameter `F`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html

error[E0277]: `dyn std::future::Future<Output = i32> + std::marker::Send` cannot be unpinned
--> $DIR/expected-boxed-future-isnt-pinned.rs:22:5
|
LL | Pin::new(x)
| ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future<Output = i32> + std::marker::Send`
|
= note: consider using `Box::pin`
= note: required by `std::pin::Pin::<P>::new`

error[E0277]: `dyn std::future::Future<Output = i32> + std::marker::Send` cannot be unpinned
--> $DIR/expected-boxed-future-isnt-pinned.rs:27:5
|
LL | Pin::new(Box::new(x))
| ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future<Output = i32> + std::marker::Send`
|
= note: consider using `Box::pin`
= note: required by `std::pin::Pin::<P>::new`

error[E0308]: mismatched types
--> $DIR/expected-boxed-future-isnt-pinned.rs:31:5
|
LL | fn zap() -> BoxFuture<'static, i32> {
| ----------------------- expected `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>` because of return type
LL | / async {
LL | | 42
LL | | }
| |_____^ expected struct `std::pin::Pin`, found opaque type
|
::: $SRC_DIR/libcore/future/mod.rs:LL:COL
|
LL | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
| ------------------------------- the found opaque type
|
= note: expected struct `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>`
found opaque type `impl std::future::Future`
help: you need to pin and box this expression
|
LL | Box::pin(async {
LL | 42
LL | })
|

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0308`.
Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.