]> git.lizzy.rs Git - rust.git/blob - src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs
Move misplaced comment
[rust.git] / src / test / ui / suggestions / expected-boxed-future-isnt-pinned.rs
1 // edition:2018
2 #![allow(dead_code)]
3 use std::future::Future;
4 use std::pin::Pin;
5
6 type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
7 //   ^^^^^^^^^ This would come from the `futures` crate in real code.
8
9 fn foo<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
10     // We could instead use an `async` block, but this way we have no std spans.
11     x //~ ERROR mismatched types
12 }
13
14 fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
15     Box::new(x) //~ ERROR mismatched types
16 }
17
18 // This case is still subpar:
19 // `Pin::new(x)`: store this in the heap by calling `Box::new`: `Box::new(x)`
20 // Should suggest changing the code from `Pin::new` to `Box::pin`.
21 fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
22     Pin::new(x) //~ ERROR mismatched types
23     //~^ ERROR E0277
24 }
25
26 fn qux<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
27     Pin::new(Box::new(x)) //~ ERROR E0277
28 }
29
30 fn zap() -> BoxFuture<'static, i32> {
31     async { //~ ERROR mismatched types
32         42
33     }
34 }
35
36 fn main() {}