When expecting BoxFuture and using async {}, suggest Box::pin

This commit is contained in:
Esteban Küber 2020-02-11 17:19:05 -08:00
parent 2d2be57097
commit c39b04ea85
6 changed files with 101 additions and 8 deletions

View file

@ -0,0 +1,16 @@
// edition:2018
// run-rustfix
#![allow(dead_code)]
use std::future::Future;
use std::pin::Pin;
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
// ^^^^^^^^^ This would come from the `futures` crate in real code.
fn foo() -> BoxFuture<'static, i32> {
Box::pin(async { //~ ERROR mismatched types
42
})
}
fn main() {}

View file

@ -0,0 +1,16 @@
// edition:2018
// run-rustfix
#![allow(dead_code)]
use std::future::Future;
use std::pin::Pin;
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
// ^^^^^^^^^ This would come from the `futures` crate in real code.
fn foo() -> BoxFuture<'static, i32> {
async { //~ ERROR mismatched types
42
}
}
fn main() {}

View file

@ -0,0 +1,27 @@
error[E0308]: mismatched types
--> $DIR/expected-boxed-future-isnt-pinned.rs:11:5
|
LL | 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
LL | / async {
LL | | 42
LL | | }
| |_____^ expected struct `std::pin::Pin`, found opaque type
|
::: $SRC_DIR/libstd/future.rs:LL:COL
|
LL | pub fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
| ------------------------------- the found opaque type
|
= note: expected struct `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>`
found opaque type `impl std::future::Future`
help: you need to pin and box this expression
|
LL | Box::pin(async {
LL | 42
LL | })
|
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.