From 9e0e4c31aa88e5d510fdd0742808ec95329470e4 Mon Sep 17 00:00:00 2001 From: Andrew Xie Date: Wed, 12 Apr 2023 18:03:11 -0400 Subject: [PATCH] Added diagnostic for pin! macro in addition to Box::pin if Unpin isn't implemented --- library/core/src/marker.rs | 2 +- .../async-await/pin-needed-to-poll-2.stderr | 3 ++- tests/ui/generator/static-not-unpin.stderr | 3 ++- .../expected-boxed-future-isnt-pinned.stderr | 6 +++-- .../suggestions/issue-84973-blacklist.stderr | 3 ++- tests/ui/suggestions/suggest-pin-macro.rs | 23 +++++++++++++++++++ tests/ui/suggestions/suggest-pin-macro.stderr | 19 +++++++++++++++ tests/ui/typeck/issue-90164.stderr | 3 ++- 8 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 tests/ui/suggestions/suggest-pin-macro.rs create mode 100644 tests/ui/suggestions/suggest-pin-macro.stderr diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 62064f1aa6c..3cd4f5104ce 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -823,7 +823,7 @@ unsafe impl Freeze for &mut T {} /// [`pin` module]: crate::pin #[stable(feature = "pin", since = "1.33.0")] #[rustc_on_unimplemented( - note = "consider using `Box::pin`", + note = "consider using the `pin!` macro\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope", message = "`{Self}` cannot be unpinned" )] #[lang = "unpin"] diff --git a/tests/ui/async-await/pin-needed-to-poll-2.stderr b/tests/ui/async-await/pin-needed-to-poll-2.stderr index 0a6f705e255..61126faf899 100644 --- a/tests/ui/async-await/pin-needed-to-poll-2.stderr +++ b/tests/ui/async-await/pin-needed-to-poll-2.stderr @@ -6,7 +6,8 @@ LL | Pin::new(&mut self.sleep).poll(cx) | | | required by a bound introduced by this call | - = note: consider using `Box::pin` + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope note: required because it appears within the type `Sleep` --> $DIR/pin-needed-to-poll-2.rs:8:8 | diff --git a/tests/ui/generator/static-not-unpin.stderr b/tests/ui/generator/static-not-unpin.stderr index e3859595fd2..7376116b338 100644 --- a/tests/ui/generator/static-not-unpin.stderr +++ b/tests/ui/generator/static-not-unpin.stderr @@ -6,7 +6,8 @@ LL | assert_unpin(generator); | | | required by a bound introduced by this call | - = note: consider using `Box::pin` + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope note: required by a bound in `assert_unpin` --> $DIR/static-not-unpin.rs:7:20 | diff --git a/tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr b/tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr index b827beb504d..0232d4c8db6 100644 --- a/tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr +++ b/tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr @@ -50,7 +50,8 @@ LL | Pin::new(x) | | | required by a bound introduced by this call | - = note: consider using `Box::pin` + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope note: required by a bound in `Pin::

::new` --> $SRC_DIR/core/src/pin.rs:LL:COL @@ -62,7 +63,8 @@ LL | Pin::new(Box::new(x)) | | | required by a bound introduced by this call | - = note: consider using `Box::pin` + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope note: required by a bound in `Pin::

::new` --> $SRC_DIR/core/src/pin.rs:LL:COL diff --git a/tests/ui/suggestions/issue-84973-blacklist.stderr b/tests/ui/suggestions/issue-84973-blacklist.stderr index c20cc816484..b9473d43b01 100644 --- a/tests/ui/suggestions/issue-84973-blacklist.stderr +++ b/tests/ui/suggestions/issue-84973-blacklist.stderr @@ -38,7 +38,8 @@ LL | f_unpin(static || { yield; }); | | | required by a bound introduced by this call | - = note: consider using `Box::pin` + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope note: required by a bound in `f_unpin` --> $DIR/issue-84973-blacklist.rs:8:15 | diff --git a/tests/ui/suggestions/suggest-pin-macro.rs b/tests/ui/suggestions/suggest-pin-macro.rs new file mode 100644 index 00000000000..f5b96215925 --- /dev/null +++ b/tests/ui/suggestions/suggest-pin-macro.rs @@ -0,0 +1,23 @@ +use std::pin::Pin; +use std::marker::PhantomPinned; + +#[derive(Debug)] +struct Test { + _marker: PhantomPinned, +} +impl Test { + fn new() -> Self { + Test { + _marker: PhantomPinned, // This makes our type `!Unpin` + } + } +} + +fn dummy(_: &mut Test) {} + +pub fn main() { + let mut test1 = Test::new(); + let mut test1 = unsafe { Pin::new_unchecked(&mut test1) }; + + dummy(test1.get_mut()); //~ ERROR E0277 +} diff --git a/tests/ui/suggestions/suggest-pin-macro.stderr b/tests/ui/suggestions/suggest-pin-macro.stderr new file mode 100644 index 00000000000..1220cf650cc --- /dev/null +++ b/tests/ui/suggestions/suggest-pin-macro.stderr @@ -0,0 +1,19 @@ +error[E0277]: `PhantomPinned` cannot be unpinned + --> $DIR/suggest-pin-macro.rs:22:17 + | +LL | dummy(test1.get_mut()); + | ^^^^^^^ within `Test`, the trait `Unpin` is not implemented for `PhantomPinned` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope +note: required because it appears within the type `Test` + --> $DIR/suggest-pin-macro.rs:5:8 + | +LL | struct Test { + | ^^^^ +note: required by a bound in `Pin::<&'a mut T>::get_mut` + --> $SRC_DIR/core/src/pin.rs:LL:COL + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/typeck/issue-90164.stderr b/tests/ui/typeck/issue-90164.stderr index 1e2f1bae3bd..8586f522291 100644 --- a/tests/ui/typeck/issue-90164.stderr +++ b/tests/ui/typeck/issue-90164.stderr @@ -6,7 +6,8 @@ LL | copy(r, w); | | | required by a bound introduced by this call | - = note: consider using `Box::pin` + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope note: required by a bound in `copy` --> $DIR/issue-90164.rs:1:12 |