Skip to content

Suggest pin::pin! over Box::pin in diagnostics #109964

Closed
@yoshuawuyts

Description

@yoshuawuyts

Code

Consider the following code, taken from the async book (playground):

pub fn main() {
    let mut test1 = Test::new("test1");
    let mut test1 = unsafe { Pin::new_unchecked(&mut test1) };
    Test::init(test1.as_mut());

    let mut test2 = Test::new("test2");
    let mut test2 = unsafe { Pin::new_unchecked(&mut test2) };
    Test::init(test2.as_mut());

    println!("a: {}, b: {}", Test::a(test1.as_ref()), Test::b(test1.as_ref()));
    std::mem::swap(test1.get_mut(), test2.get_mut());
    println!("a: {}, b: {}", Test::a(test2.as_ref()), Test::b(test2.as_ref()));
}

Current output

error[E0277]: `PhantomPinned` cannot be unpinned
  --> src/main.rs:47:26
   |
47 |     std::mem::swap(test1.get_mut(), test2.get_mut());
   |                          ^^^^^^^ within `Test`, the trait `Unpin` is not implemented for `PhantomPinned`
   |
   = note: consider using `Box::pin`
note: required because it appears within the type `Test`

Desired output

error[E0277]: `PhantomPinned` cannot be unpinned
  --> src/main.rs:47:26
   |
47 |     std::mem::swap(test1.get_mut(), test2.get_mut());
   |                          ^^^^^^^ within `Test`, the trait `Unpin` is not implemented for `PhantomPinned`
   |
   = note: consider using `core::pin::pin!`
note: required because it appears within the type `Test`

Rationale and extra context

Local pin construction was stabilized in Rust 1.68. We should update the diagnostics to suggest this instead of the more expensive Box::pin call.

Other cases

No response

Anything else?

cc/ @wg-async

This is probably a "good first issue" as well. If someone can tag it as such?

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions