When expecting BoxFuture and using async {}, suggest Box::pin
This commit is contained in:
parent
2d2be57097
commit
c39b04ea85
6 changed files with 101 additions and 8 deletions
|
|
@ -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() {}
|
||||
16
src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs
Normal file
16
src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs
Normal 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() {}
|
||||
|
|
@ -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`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue