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

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

Closed
estebank opened this issue Feb 12, 2020 · 1 comment · Fixed by #71781
Closed

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

estebank opened this issue Feb 12, 2020 · 1 comment · Fixed by #71781
Labels
A-async-await A-diagnostics A-suggestion-diagnostics AsyncAwait-Triaged C-enhancement T-compiler

Comments

@estebank
Copy link
Contributor

@estebank estebank commented Feb 12, 2020

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.

@estebank estebank added C-enhancement A-diagnostics T-compiler A-async-await A-suggestion-diagnostics labels Feb 12, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 13, 2020
When expecting `BoxFuture` and using `async {}`, suggest `Box::pin`

Fix rust-lang#68197, cc rust-lang#69083.
@estebank
Copy link
Contributor Author

@estebank estebank commented Feb 13, 2020

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

@tmandry tmandry added the AsyncAwait-Triaged label Feb 18, 2020
RalfJung added a commit to RalfJung/rust that referenced this issue May 2, 2020
Uncomment test code for failure to use `Box::pin`

Close rust-lang#69083.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await A-diagnostics A-suggestion-diagnostics AsyncAwait-Triaged C-enhancement T-compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants