Added diagnostic for pin! macro in addition to Box::pin if Unpin isn't implemented

This commit is contained in:
Andrew Xie 2023-04-12 18:03:11 -04:00
parent 59a05ad118
commit 9e0e4c31aa
8 changed files with 55 additions and 7 deletions

View file

@ -823,7 +823,7 @@ unsafe impl<T: ?Sized> 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"]

View file

@ -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
|

View file

@ -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
|

View file

@ -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::<P>::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::<P>::new`
--> $SRC_DIR/core/src/pin.rs:LL:COL

View file

@ -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
|

View file

@ -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
}

View file

@ -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`.

View file

@ -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
|