Skip to content
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

Added diagnostic/suggestion for core::pin::pin! in addition to Box::pin if Unpin isn't implemented #109988

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `std::pin::pin!`\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
note = "consider using `std::pin::pin!`\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope",
note = "consider using the `pin!` macro\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope",

Works for both std and no-std 😄

r=me with commits squashed

message = "`{Self}` cannot be unpinned"
)]
#[lang = "unpin"]
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/async-await/pin-needed-to-poll-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 `std::pin::pin!`
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
|
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/generator/static-not-unpin.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ LL | assert_unpin(generator);
| |
| required by a bound introduced by this call
|
= note: consider using `Box::pin`
= note: consider using `std::pin::pin!`
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
|
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ LL | Pin::new(x)
| |
| required by a bound introduced by this call
|
= note: consider using `Box::pin`
= note: consider using `std::pin::pin!`
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

Expand All @@ -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 `std::pin::pin!`
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

Expand Down
3 changes: 2 additions & 1 deletion tests/ui/suggestions/issue-84973-blacklist.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 `std::pin::pin!`
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
|
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/suggestions/suggest-pin-macro.rs
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions tests/ui/suggestions/suggest-pin-macro.stderr
Original file line number Diff line number Diff line change
@@ -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 `std::pin::pin!`
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`.
3 changes: 2 additions & 1 deletion tests/ui/typeck/issue-90164.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ LL | copy(r, w);
| |
| required by a bound introduced by this call
|
= note: consider using `Box::pin`
= note: consider using `std::pin::pin!`
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
|
Expand Down