Skip to content

Detect cases where Box::pin was likely intended #69083

Closed
@estebank

Description

@estebank

When BoxFuture is expected and we supply Pin::new(async { /* .. */ }), Box::new(async { /* .. */ }) or Pin::new(Box::new(async { /* .. */ })) we should lead the user in the right direction instead of what we do now:

error[E0277]: the trait bound `dyn std::future::Future<Output = i32> + std::marker::Send: std::marker::Unpin` is not satisfied
  --> file5.rs:10:5
   |
10 |     Pin::new(async { 42 })
   |     ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future<Output = i32> + std::marker::Send`
   |
   = note: required by `std::pin::Pin::<P>::new`
error[E0308]: mismatched types
  --> file5.rs:10:5
   |
9  | fn foo() -> BoxFuture<'static, i32> {
   |             ----------------------- expected `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>` because of return type
10 |     Box::new(async { 42 })
   |     ^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     expected struct `std::pin::Pin`, found struct `std::boxed::Box`
   |     help: you need to pin and box this expression: `Box::pin(Box::new(async { 42 }))`
   |
   = note: expected struct `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>`
              found struct `std::boxed::Box<impl std::future::Future>`
error[E0277]: the trait bound `dyn std::future::Future<Output = i32> + std::marker::Send: std::marker::Unpin` is not satisfied
  --> file5.rs:10:5
   |
10 |     Pin::new(Box::new(async { 42 }))
   |     ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future<Output = i32> + std::marker::Send`
   |
   = note: required by `std::pin::Pin::<P>::new`

For the second case we could suggest Box::into_pin once #62370 stabilizes. The other two cases can be handled with a rustc_on_unimplemented note.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.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