Skip to content

Commit c376fc0

Browse files
committed
Account for Pin::new(_) and Pin::new(Box::new(_)) when Box::pin(_) would be applicable
1 parent 80cdb0a commit c376fc0

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

src/libcore/marker.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,13 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
727727
/// [`Pin<P>`]: ../pin/struct.Pin.html
728728
/// [`pin module`]: ../../std/pin/index.html
729729
#[stable(feature = "pin", since = "1.33.0")]
730+
#[rustc_on_unimplemented(
731+
on(
732+
_Self = "dyn std::future::Future<Output = i32> + std::marker::Send",
733+
note = "consider using `Box::pin`",
734+
),
735+
message = "`{Self}` cannot be unpinned"
736+
)]
730737
#[lang = "unpin"]
731738
pub auto trait Unpin {}
732739

src/test/ui/generator/static-not-unpin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ fn main() {
1111
let mut generator = static || {
1212
yield;
1313
};
14-
assert_unpin(generator); //~ ERROR std::marker::Unpin` is not satisfied
14+
assert_unpin(generator); //~ ERROR E0277
1515
}

src/test/ui/generator/static-not-unpin.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: the trait bound `[static generator@$DIR/static-not-unpin.rs:11:25: 13:6 _]: std::marker::Unpin` is not satisfied
1+
error[E0277]: `[static generator@$DIR/static-not-unpin.rs:11:25: 13:6 _]` cannot be unpinned
22
--> $DIR/static-not-unpin.rs:14:18
33
|
44
LL | fn assert_unpin<T: Unpin>(_: T) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ fn foo<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32>
1010
// We could instead use an `async` block, but this way we have no std spans.
1111
x //~ ERROR mismatched types
1212
}
13+
1314
fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
1415
Box::new(x) //~ ERROR mismatched types
15-
//~^ HELP use `Box::pin`
1616
}
17+
1718
fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
1819
Pin::new(x) //~ ERROR mismatched types
19-
//~^ ERROR the trait bound
20+
//~^ ERROR E0277
2021
}
22+
2123
fn qux<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
22-
Pin::new(Box::new(x)) //~ ERROR mismatched types
23-
//~^ ERROR the trait bound
24+
Pin::new(Box::new(x)) //~ ERROR E0277
2425
}
2526

2627
fn main() {}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | x
1616
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
1717

1818
error[E0308]: mismatched types
19-
--> $DIR/expected-boxed-future-isnt-pinned.rs:14:5
19+
--> $DIR/expected-boxed-future-isnt-pinned.rs:15:5
2020
|
2121
LL | fn bar<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
2222
| ----------------------- expected `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = i32> + std::marker::Send + 'static)>>` because of return type
@@ -28,7 +28,7 @@ LL | Box::new(x)
2828
= help: use `Box::pin`
2929

3030
error[E0308]: mismatched types
31-
--> $DIR/expected-boxed-future-isnt-pinned.rs:18:14
31+
--> $DIR/expected-boxed-future-isnt-pinned.rs:19:14
3232
|
3333
LL | fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
3434
| - this type parameter
@@ -44,20 +44,22 @@ LL | Pin::new(x)
4444
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
4545
= 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
4646

47-
error[E0277]: the trait bound `dyn std::future::Future<Output = i32> + std::marker::Send: std::marker::Unpin` is not satisfied
48-
--> $DIR/expected-boxed-future-isnt-pinned.rs:18:5
47+
error[E0277]: `dyn std::future::Future<Output = i32> + std::marker::Send` cannot be unpinned
48+
--> $DIR/expected-boxed-future-isnt-pinned.rs:19:5
4949
|
5050
LL | Pin::new(x)
5151
| ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future<Output = i32> + std::marker::Send`
5252
|
53+
= note: consider using `Box::pin`
5354
= note: required by `std::pin::Pin::<P>::new`
5455

55-
error[E0277]: the trait bound `dyn std::future::Future<Output = i32> + std::marker::Send: std::marker::Unpin` is not satisfied
56-
--> $DIR/expected-boxed-future-isnt-pinned.rs:22:5
56+
error[E0277]: `dyn std::future::Future<Output = i32> + std::marker::Send` cannot be unpinned
57+
--> $DIR/expected-boxed-future-isnt-pinned.rs:24:5
5758
|
5859
LL | Pin::new(Box::new(x))
5960
| ^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `dyn std::future::Future<Output = i32> + std::marker::Send`
6061
|
62+
= note: consider using `Box::pin`
6163
= note: required by `std::pin::Pin::<P>::new`
6264

6365
error: aborting due to 5 previous errors

0 commit comments

Comments
 (0)