Skip to content

Commit 832be69

Browse files
committed
Add some error message tests
1 parent 8365fcb commit 832be69

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed

tests/ui/deref/pin-deref.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// The purpose of this file is to track the error messages from Pin and DerefMut interacting.
2+
3+
//@ check-fail
4+
5+
use std::ops::DerefMut;
6+
use std::pin::Pin;
7+
8+
struct MyUnpinType {}
9+
10+
impl MyUnpinType {
11+
fn at_self(&self) {}
12+
fn at_mut_self(&mut self) {}
13+
}
14+
15+
struct MyPinType(core::marker::PhantomPinned);
16+
17+
impl MyPinType {
18+
fn at_self(&self) {}
19+
fn at_mut_self(&mut self) {}
20+
}
21+
22+
fn call_mut_ref_unpin(mut r_unpin: Pin<&mut MyUnpinType>) {
23+
r_unpin.at_self();
24+
r_unpin.at_mut_self();
25+
}
26+
27+
fn call_ref_unpin(mut r_unpin: Pin<&MyUnpinType>) {
28+
r_unpin.at_self();
29+
r_unpin.at_mut_self(); //~ ERROR: cannot borrow data in dereference of `Pin<&MyUnpinType>` as mutable
30+
}
31+
32+
fn call_mut_ref_pin(mut r_pin: Pin<&mut MyPinType>) {
33+
r_pin.at_self();
34+
r_pin.at_mut_self(); //~ ERROR: cannot borrow data in dereference of `Pin<&mut MyPinType>` as mutable
35+
}
36+
37+
fn call_ref_pin(mut r_pin: Pin<&MyPinType>) {
38+
r_pin.at_self();
39+
r_pin.at_mut_self(); //~ ERROR: cannot borrow data in dereference of `Pin<&MyPinType>` as mutable
40+
}
41+
42+
fn coerce_unpin_rr<'a>(mut r_unpin: &'a mut Pin<&MyUnpinType>) -> &'a MyUnpinType {
43+
r_unpin
44+
}
45+
46+
fn coerce_unpin_rm<'a>(mut r_unpin: &'a mut Pin<&MyUnpinType>) -> &'a mut MyUnpinType {
47+
r_unpin //~ ERROR: cannot borrow data in dereference of `Pin<&MyUnpinType>` as mutable
48+
}
49+
50+
fn coerce_unpin_mr<'a>(mut r_unpin: &'a mut Pin<&mut MyUnpinType>) -> &'a MyUnpinType {
51+
r_unpin
52+
}
53+
54+
fn coerce_unpin_mm<'a>(mut r_unpin: &'a mut Pin<&mut MyUnpinType>) -> &'a mut MyUnpinType {
55+
r_unpin
56+
}
57+
58+
fn coerce_pin_rr<'a>(mut r_pin: &'a mut Pin<&MyPinType>) -> &'a MyPinType {
59+
r_pin
60+
}
61+
62+
fn coerce_pin_rm<'a>(mut r_pin: &'a mut Pin<&MyPinType>) -> &'a mut MyPinType {
63+
r_pin //~ ERROR: cannot borrow data in dereference of `Pin<&MyPinType>` as mutable
64+
}
65+
66+
fn coerce_pin_mr<'a>(mut r_pin: &'a mut Pin<&mut MyPinType>) -> &'a MyPinType {
67+
r_pin
68+
}
69+
70+
fn coerce_pin_mm<'a>(mut r_pin: &'a mut Pin<&mut MyPinType>) -> &'a mut MyPinType {
71+
r_pin //~ ERROR: cannot borrow data in dereference of `Pin<&mut MyPinType>` as mutable
72+
}
73+
74+
fn main() {}

tests/ui/deref/pin-deref.stderr

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error[E0596]: cannot borrow data in dereference of `Pin<&MyUnpinType>` as mutable
2+
--> $DIR/pin-deref.rs:29:5
3+
|
4+
LL | r_unpin.at_mut_self();
5+
| ^^^^^^^ cannot borrow as mutable
6+
|
7+
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Pin<&MyUnpinType>`
8+
9+
error[E0596]: cannot borrow data in dereference of `Pin<&mut MyPinType>` as mutable
10+
--> $DIR/pin-deref.rs:34:5
11+
|
12+
LL | r_pin.at_mut_self();
13+
| ^^^^^ cannot borrow as mutable
14+
|
15+
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Pin<&mut MyPinType>`
16+
17+
error[E0596]: cannot borrow data in dereference of `Pin<&MyPinType>` as mutable
18+
--> $DIR/pin-deref.rs:39:5
19+
|
20+
LL | r_pin.at_mut_self();
21+
| ^^^^^ cannot borrow as mutable
22+
|
23+
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Pin<&MyPinType>`
24+
25+
error[E0596]: cannot borrow data in dereference of `Pin<&MyUnpinType>` as mutable
26+
--> $DIR/pin-deref.rs:47:5
27+
|
28+
LL | r_unpin
29+
| ^^^^^^^ cannot borrow as mutable
30+
|
31+
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Pin<&MyUnpinType>`
32+
33+
error[E0596]: cannot borrow data in dereference of `Pin<&MyPinType>` as mutable
34+
--> $DIR/pin-deref.rs:63:5
35+
|
36+
LL | r_pin
37+
| ^^^^^ cannot borrow as mutable
38+
|
39+
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Pin<&MyPinType>`
40+
41+
error[E0596]: cannot borrow data in dereference of `Pin<&mut MyPinType>` as mutable
42+
--> $DIR/pin-deref.rs:71:5
43+
|
44+
LL | r_pin
45+
| ^^^^^ cannot borrow as mutable
46+
|
47+
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Pin<&mut MyPinType>`
48+
49+
error: aborting due to 6 previous errors
50+
51+
For more information about this error, try `rustc --explain E0596`.

tests/ui/deref/pin-impl-deref.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// The purpose of this file is to track the error messages from Pin and DerefMut interacting.
2+
3+
//@ check-fail
4+
5+
use std::ops::DerefMut;
6+
use std::pin::Pin;
7+
8+
struct MyUnpinType {}
9+
10+
impl MyUnpinType {
11+
fn at_self(&self) {}
12+
fn at_mut_self(&mut self) {}
13+
}
14+
15+
struct MyPinType(core::marker::PhantomPinned);
16+
17+
impl MyPinType {
18+
fn at_self(&self) {}
19+
fn at_mut_self(&mut self) {}
20+
}
21+
22+
fn impl_deref_mut(_: impl DerefMut) {}
23+
fn unpin_impl_ref(r_unpin: Pin<&MyUnpinType>) {
24+
impl_deref_mut(r_unpin)
25+
//~^ ERROR: the trait bound `Pin<&MyUnpinType>: DerefMut` is not satisfied
26+
}
27+
fn unpin_impl_mut(r_unpin: Pin<&mut MyUnpinType>) {
28+
impl_deref_mut(r_unpin)
29+
}
30+
fn pin_impl_ref(r_pin: Pin<&MyPinType>) {
31+
impl_deref_mut(r_pin)
32+
//~^ ERROR: `PhantomPinned` cannot be unpinned
33+
//~| ERROR: the trait bound `Pin<&MyPinType>: DerefMut` is not satisfied
34+
}
35+
fn pin_impl_mut(r_pin: Pin<&mut MyPinType>) {
36+
impl_deref_mut(r_pin)
37+
//~^ ERROR: `PhantomPinned` cannot be unpinned
38+
}
39+
40+
fn main() {}

tests/ui/deref/pin-impl-deref.stderr

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
error[E0277]: the trait bound `Pin<&MyUnpinType>: DerefMut` is not satisfied
2+
--> $DIR/pin-impl-deref.rs:24:20
3+
|
4+
LL | impl_deref_mut(r_unpin)
5+
| -------------- ^^^^^^^ the trait `DerefMut` is not implemented for `Pin<&MyUnpinType>`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= note: required for `Pin<&MyUnpinType>` to implement `DerefMut`
10+
note: required by a bound in `impl_deref_mut`
11+
--> $DIR/pin-impl-deref.rs:22:27
12+
|
13+
LL | fn impl_deref_mut(_: impl DerefMut) {}
14+
| ^^^^^^^^ required by this bound in `impl_deref_mut`
15+
help: consider mutably borrowing here
16+
|
17+
LL | impl_deref_mut(&mut r_unpin)
18+
| ++++
19+
20+
error[E0277]: the trait bound `Pin<&MyPinType>: DerefMut` is not satisfied
21+
--> $DIR/pin-impl-deref.rs:31:20
22+
|
23+
LL | impl_deref_mut(r_pin)
24+
| -------------- ^^^^^ the trait `DerefMut` is not implemented for `Pin<&MyPinType>`
25+
| |
26+
| required by a bound introduced by this call
27+
|
28+
= note: required for `Pin<&MyPinType>` to implement `DerefMut`
29+
note: required by a bound in `impl_deref_mut`
30+
--> $DIR/pin-impl-deref.rs:22:27
31+
|
32+
LL | fn impl_deref_mut(_: impl DerefMut) {}
33+
| ^^^^^^^^ required by this bound in `impl_deref_mut`
34+
help: consider mutably borrowing here
35+
|
36+
LL | impl_deref_mut(&mut r_pin)
37+
| ++++
38+
39+
error[E0277]: `PhantomPinned` cannot be unpinned
40+
--> $DIR/pin-impl-deref.rs:31:20
41+
|
42+
LL | impl_deref_mut(r_pin)
43+
| -------------- ^^^^^ within `MyPinType`, the trait `Unpin` is not implemented for `PhantomPinned`
44+
| |
45+
| required by a bound introduced by this call
46+
|
47+
= note: consider using the `pin!` macro
48+
consider using `Box::pin` if you need to access the pinned value outside of the current scope
49+
note: required because it appears within the type `MyPinType`
50+
--> $DIR/pin-impl-deref.rs:15:8
51+
|
52+
LL | struct MyPinType(core::marker::PhantomPinned);
53+
| ^^^^^^^^^
54+
= note: required for `Pin<&MyPinType>` to implement `DerefMut`
55+
note: required by a bound in `impl_deref_mut`
56+
--> $DIR/pin-impl-deref.rs:22:27
57+
|
58+
LL | fn impl_deref_mut(_: impl DerefMut) {}
59+
| ^^^^^^^^ required by this bound in `impl_deref_mut`
60+
61+
error[E0277]: `PhantomPinned` cannot be unpinned
62+
--> $DIR/pin-impl-deref.rs:36:20
63+
|
64+
LL | impl_deref_mut(r_pin)
65+
| -------------- ^^^^^ within `MyPinType`, the trait `Unpin` is not implemented for `PhantomPinned`
66+
| |
67+
| required by a bound introduced by this call
68+
|
69+
= note: consider using the `pin!` macro
70+
consider using `Box::pin` if you need to access the pinned value outside of the current scope
71+
note: required because it appears within the type `MyPinType`
72+
--> $DIR/pin-impl-deref.rs:15:8
73+
|
74+
LL | struct MyPinType(core::marker::PhantomPinned);
75+
| ^^^^^^^^^
76+
= note: required for `Pin<&mut MyPinType>` to implement `DerefMut`
77+
note: required by a bound in `impl_deref_mut`
78+
--> $DIR/pin-impl-deref.rs:22:27
79+
|
80+
LL | fn impl_deref_mut(_: impl DerefMut) {}
81+
| ^^^^^^^^ required by this bound in `impl_deref_mut`
82+
83+
error: aborting due to 4 previous errors
84+
85+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)