Skip to content

Commit a9c818e

Browse files
authored
Rollup merge of #71781 - estebank:box-pin-test, r=tmandry
Uncomment test code for failure to use `Box::pin` Close #69083.
2 parents 509896f + b3a8f21 commit a9c818e

File tree

2 files changed

+95
-16
lines changed

2 files changed

+95
-16
lines changed

src/test/ui/suggestions/expected-boxed-future-isnt-pinned.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@ fn foo<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32>
1111
x //~ ERROR mismatched types
1212
}
1313

14-
// FIXME: uncomment these once this commit is in Beta and we can rely on `rustc_on_unimplemented`
15-
// having filtering for `Self` being a trait.
16-
//
17-
// fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
18-
// Box::new(x)
19-
// }
20-
//
21-
// fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
22-
// Pin::new(x)
23-
// }
24-
//
25-
// fn qux<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
26-
// Pin::new(Box::new(x))
27-
// }
14+
// This case is still subpar:
15+
// `Pin::new(x)`: store this in the heap by calling `Box::new`: `Box::new(x)`
16+
// Should suggest changing the code from `Pin::new` to `Box::pin`.
17+
fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
18+
Box::new(x) //~ ERROR mismatched types
19+
}
20+
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+
}
2835

2936
fn main() {}

src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr

+74-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,78 @@ LL | x
1515
= help: type parameters must be constrained to match other types
1616
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
1717

18-
error: aborting due to previous error
18+
error[E0308]: mismatched types
19+
--> $DIR/expected-boxed-future-isnt-pinned.rs:18:5
20+
|
21+
LL | fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
22+
| ----------------------- expected `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>` because of return type
23+
LL | Box::new(x)
24+
| ^^^^^^^^^^^ expected struct `std::pin::Pin`, found struct `std::boxed::Box`
25+
|
26+
= note: expected struct `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>`
27+
found struct `std::boxed::Box<F>`
28+
= help: use `Box::pin`
29+
30+
error[E0308]: mismatched types
31+
--> $DIR/expected-boxed-future-isnt-pinned.rs:22:14
32+
|
33+
LL | fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
34+
| - this type parameter
35+
LL | Pin::new(x)
36+
| ^
37+
| |
38+
| expected struct `std::boxed::Box`, found type parameter `F`
39+
| help: store this in the heap by calling `Box::new`: `Box::new(x)`
40+
|
41+
= note: expected struct `std::boxed::Box<dyn std::future::Future<Output = i32> + std::marker::Send>`
42+
found type parameter `F`
43+
= help: type parameters must be constrained to match other types
44+
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
45+
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
46+
47+
error[E0277]: `dyn std::future::Future<Output = i32> + std::marker::Send` cannot be unpinned
48+
--> $DIR/expected-boxed-future-isnt-pinned.rs:22:5
49+
|
50+
LL | Pin::new(x)
51+
| ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future<Output = i32> + std::marker::Send`
52+
|
53+
= note: consider using `Box::pin`
54+
= note: required by `std::pin::Pin::<P>::new`
55+
56+
error[E0277]: `dyn std::future::Future<Output = i32> + std::marker::Send` cannot be unpinned
57+
--> $DIR/expected-boxed-future-isnt-pinned.rs:27:5
58+
|
59+
LL | Pin::new(Box::new(x))
60+
| ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future<Output = i32> + std::marker::Send`
61+
|
62+
= note: consider using `Box::pin`
63+
= note: required by `std::pin::Pin::<P>::new`
64+
65+
error[E0308]: mismatched types
66+
--> $DIR/expected-boxed-future-isnt-pinned.rs:31:5
67+
|
68+
LL | fn zap() -> BoxFuture<'static, i32> {
69+
| ----------------------- expected `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>` because of return type
70+
LL | / async {
71+
LL | | 42
72+
LL | | }
73+
| |_____^ expected struct `std::pin::Pin`, found opaque type
74+
|
75+
::: $SRC_DIR/libcore/future/mod.rs:LL:COL
76+
|
77+
LL | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
78+
| ------------------------------- the found opaque type
79+
|
80+
= note: expected struct `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>`
81+
found opaque type `impl std::future::Future`
82+
help: you need to pin and box this expression
83+
|
84+
LL | Box::pin(async {
85+
LL | 42
86+
LL | })
87+
|
88+
89+
error: aborting due to 6 previous errors
1990

20-
For more information about this error, try `rustc --explain E0308`.
91+
Some errors have detailed explanations: E0277, E0308.
92+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)