Skip to content

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

Closed
@estebank

Description

@estebank
Contributor

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.

Activity

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
on Feb 12, 2020
added a commit that references this issue on Feb 13, 2020

Rollup merge of rust-lang#69082 - estebank:boxfuture-box-pin, r=tmandry

ec5bf15
estebank

estebank commented on Feb 13, 2020

@estebank
ContributorAuthor

All that's left is to update the tests in the next release.

added
AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
on Feb 18, 2020
added a commit that references this issue on May 1, 2020
b3a8f21
added a commit that references this issue on May 2, 2020

Rollup merge of rust-lang#71781 - estebank:box-pin-test, r=tmandry

a9c818e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @estebank@tmandry

      Issue actions

        Detect cases where `Box::pin` was likely intended · Issue #69083 · rust-lang/rust