From c376fc001772200e2de8d7a610a5b67dcf642432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 12 Feb 2020 11:51:07 -0800 Subject: [PATCH] Account for `Pin::new(_)` and `Pin::new(Box::new(_))` when `Box::pin(_)` would be applicable --- src/libcore/marker.rs | 7 +++++++ src/test/ui/generator/static-not-unpin.rs | 2 +- src/test/ui/generator/static-not-unpin.stderr | 2 +- .../expected-boxed-future-isnt-pinned.rs | 9 +++++---- .../expected-boxed-future-isnt-pinned.stderr | 14 ++++++++------ 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index b4b595f330e2..5c35041249f3 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -727,6 +727,13 @@ unsafe impl Freeze for &mut T {} /// [`Pin

`]: ../pin/struct.Pin.html /// [`pin module`]: ../../std/pin/index.html #[stable(feature = "pin", since = "1.33.0")] +#[rustc_on_unimplemented( + on( + _Self = "dyn std::future::Future + std::marker::Send", + note = "consider using `Box::pin`", + ), + message = "`{Self}` cannot be unpinned" +)] #[lang = "unpin"] pub auto trait Unpin {} diff --git a/src/test/ui/generator/static-not-unpin.rs b/src/test/ui/generator/static-not-unpin.rs index b271e982fb42..cfcb94737be6 100644 --- a/src/test/ui/generator/static-not-unpin.rs +++ b/src/test/ui/generator/static-not-unpin.rs @@ -11,5 +11,5 @@ fn main() { let mut generator = static || { yield; }; - assert_unpin(generator); //~ ERROR std::marker::Unpin` is not satisfied + assert_unpin(generator); //~ ERROR E0277 } diff --git a/src/test/ui/generator/static-not-unpin.stderr b/src/test/ui/generator/static-not-unpin.stderr index f2b1078e2b53..6512d67319b0 100644 --- a/src/test/ui/generator/static-not-unpin.stderr +++ b/src/test/ui/generator/static-not-unpin.stderr @@ -1,4 +1,4 @@ -error[E0277]: the trait bound `[static generator@$DIR/static-not-unpin.rs:11:25: 13:6 _]: std::marker::Unpin` is not satisfied +error[E0277]: `[static generator@$DIR/static-not-unpin.rs:11:25: 13:6 _]` cannot be unpinned --> $DIR/static-not-unpin.rs:14:18 | LL | fn assert_unpin(_: T) { diff --git a/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs b/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs index fd4077653903..eda579f7fb49 100644 --- a/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs +++ b/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs @@ -10,17 +10,18 @@ fn foo + Send + 'static>(x: F) -> BoxFuture<'static, i32> // We could instead use an `async` block, but this way we have no std spans. x //~ ERROR mismatched types } + fn bar + Send + 'static>(x: F) -> BoxFuture<'static, i32> { Box::new(x) //~ ERROR mismatched types - //~^ HELP use `Box::pin` } + fn baz + Send + 'static>(x: F) -> BoxFuture<'static, i32> { Pin::new(x) //~ ERROR mismatched types - //~^ ERROR the trait bound + //~^ ERROR E0277 } + fn qux + Send + 'static>(x: F) -> BoxFuture<'static, i32> { - Pin::new(Box::new(x)) //~ ERROR mismatched types - //~^ ERROR the trait bound + Pin::new(Box::new(x)) //~ ERROR E0277 } fn main() {} diff --git a/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr b/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr index cf5ef1362db3..783b118d2f9a 100644 --- a/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr +++ b/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr @@ -16,7 +16,7 @@ LL | x = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters error[E0308]: mismatched types - --> $DIR/expected-boxed-future-isnt-pinned.rs:14:5 + --> $DIR/expected-boxed-future-isnt-pinned.rs:15:5 | LL | fn bar + Send + 'static>(x: F) -> BoxFuture<'static, i32> { | ----------------------- expected `std::pin::Pin + std::marker::Send + 'static)>>` because of return type @@ -28,7 +28,7 @@ LL | Box::new(x) = help: use `Box::pin` error[E0308]: mismatched types - --> $DIR/expected-boxed-future-isnt-pinned.rs:18:14 + --> $DIR/expected-boxed-future-isnt-pinned.rs:19:14 | LL | fn baz + Send + 'static>(x: F) -> BoxFuture<'static, i32> { | - this type parameter @@ -44,20 +44,22 @@ LL | Pin::new(x) = 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]: the trait bound `dyn std::future::Future + std::marker::Send: std::marker::Unpin` is not satisfied - --> $DIR/expected-boxed-future-isnt-pinned.rs:18:5 +error[E0277]: `dyn std::future::Future + std::marker::Send` cannot be unpinned + --> $DIR/expected-boxed-future-isnt-pinned.rs:19:5 | LL | Pin::new(x) | ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future + std::marker::Send` | + = note: consider using `Box::pin` = note: required by `std::pin::Pin::

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

::new` error: aborting due to 5 previous errors