Skip to content

Commit cb6cd8b

Browse files
committed
add a test for pointer casts involving un/re/wrapping trait objects
the errors should not be there, this is a bug/missing feature.
1 parent 9e48dfe commit cb6cd8b

8 files changed

+603
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Combination of `ptr-to-trait-obj-wrap.rs` and `ptr-to-trait-obj-add-auto.rs`.
2+
//
3+
// Checks that you *can't* add auto traits to trait object in pointer casts involving wrapping said
4+
// traits structures.
5+
#![allow(unused)]
6+
#![deny(ptr_cast_add_auto_to_object)]
7+
8+
trait A {}
9+
10+
struct W<T: ?Sized>(T);
11+
struct X<T: ?Sized>(T);
12+
13+
fn unwrap(a: *const W<dyn A>) -> *const (dyn A + Send) {
14+
a as _
15+
//~^ error
16+
//~| error
17+
//~| error
18+
}
19+
20+
fn unwrap_nested(a: *const W<W<dyn A>>) -> *const W<dyn A + Send> {
21+
a as _
22+
//~^ error
23+
//~| error
24+
//~| error
25+
}
26+
27+
fn rewrap(a: *const W<dyn A>) -> *const X<dyn A + Send> {
28+
a as _
29+
//~^ error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
30+
//~| warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
31+
}
32+
33+
fn rewrap_nested(a: *const W<W<dyn A>>) -> *const W<X<dyn A + Send>> {
34+
a as _
35+
//~^ error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
36+
//~| warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
37+
}
38+
39+
fn wrap(a: *const dyn A) -> *const W<dyn A + Send> {
40+
a as _
41+
//~^ error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
42+
//~| warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
43+
}
44+
45+
fn main() {}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
error[E0277]: the trait bound `W<(dyn A + 'static)>: A` is not satisfied
2+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:14:5
3+
|
4+
LL | a as _
5+
| ^ the trait `A` is not implemented for `W<(dyn A + 'static)>`
6+
|
7+
help: this trait has no implementations, consider adding one
8+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:8:1
9+
|
10+
LL | trait A {}
11+
| ^^^^^^^
12+
= note: required for the cast from `*const W<(dyn A + 'static)>` to `*const dyn A + Send`
13+
14+
error[E0277]: `(dyn A + 'static)` cannot be sent between threads safely
15+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:14:5
16+
|
17+
LL | a as _
18+
| ^ `(dyn A + 'static)` cannot be sent between threads safely
19+
|
20+
= help: within `W<(dyn A + 'static)>`, the trait `Send` is not implemented for `(dyn A + 'static)`
21+
note: required because it appears within the type `W<(dyn A + 'static)>`
22+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:10:8
23+
|
24+
LL | struct W<T: ?Sized>(T);
25+
| ^
26+
= note: required for the cast from `*const W<(dyn A + 'static)>` to `*const dyn A + Send`
27+
28+
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
29+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:14:5
30+
|
31+
LL | a as _
32+
| ^ doesn't have a size known at compile-time
33+
|
34+
= help: within `W<(dyn A + 'static)>`, the trait `Sized` is not implemented for `(dyn A + 'static)`
35+
note: required because it appears within the type `W<(dyn A + 'static)>`
36+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:10:8
37+
|
38+
LL | struct W<T: ?Sized>(T);
39+
| ^
40+
= note: required for the cast from `*const W<(dyn A + 'static)>` to `*const dyn A + Send`
41+
42+
error[E0277]: the trait bound `W<(dyn A + 'static)>: A` is not satisfied
43+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:21:5
44+
|
45+
LL | a as _
46+
| ^ the trait `A` is not implemented for `W<(dyn A + 'static)>`
47+
|
48+
help: this trait has no implementations, consider adding one
49+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:8:1
50+
|
51+
LL | trait A {}
52+
| ^^^^^^^
53+
= note: required for the cast from `*const W<W<(dyn A + 'static)>>` to `*const W<dyn A + Send>`
54+
55+
error[E0277]: `(dyn A + 'static)` cannot be sent between threads safely
56+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:21:5
57+
|
58+
LL | a as _
59+
| ^ `(dyn A + 'static)` cannot be sent between threads safely
60+
|
61+
= help: within `W<(dyn A + 'static)>`, the trait `Send` is not implemented for `(dyn A + 'static)`
62+
note: required because it appears within the type `W<(dyn A + 'static)>`
63+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:10:8
64+
|
65+
LL | struct W<T: ?Sized>(T);
66+
| ^
67+
= note: required for the cast from `*const W<W<(dyn A + 'static)>>` to `*const W<dyn A + Send>`
68+
69+
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
70+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:21:5
71+
|
72+
LL | a as _
73+
| ^ doesn't have a size known at compile-time
74+
|
75+
= help: within `W<(dyn A + 'static)>`, the trait `Sized` is not implemented for `(dyn A + 'static)`
76+
note: required because it appears within the type `W<(dyn A + 'static)>`
77+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:10:8
78+
|
79+
LL | struct W<T: ?Sized>(T);
80+
| ^
81+
= note: required for the cast from `*const W<W<(dyn A + 'static)>>` to `*const W<dyn A + Send>`
82+
83+
error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
84+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:28:5
85+
|
86+
LL | a as _
87+
| ^^^^^^
88+
|
89+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
90+
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
91+
note: the lint level is defined here
92+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:6:9
93+
|
94+
LL | #![deny(ptr_cast_add_auto_to_object)]
95+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
96+
97+
error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
98+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:34:5
99+
|
100+
LL | a as _
101+
| ^^^^^^
102+
|
103+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
104+
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
105+
106+
error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
107+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:40:5
108+
|
109+
LL | a as _
110+
| ^^^^^^
111+
|
112+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
113+
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
114+
115+
error: aborting due to 9 previous errors
116+
117+
For more information about this error, try `rustc --explain E0277`.
118+
Future incompatibility report: Future breakage diagnostic:
119+
error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
120+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:28:5
121+
|
122+
LL | a as _
123+
| ^^^^^^
124+
|
125+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
126+
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
127+
note: the lint level is defined here
128+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:6:9
129+
|
130+
LL | #![deny(ptr_cast_add_auto_to_object)]
131+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
132+
133+
Future breakage diagnostic:
134+
error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
135+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:34:5
136+
|
137+
LL | a as _
138+
| ^^^^^^
139+
|
140+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
141+
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
142+
note: the lint level is defined here
143+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:6:9
144+
|
145+
LL | #![deny(ptr_cast_add_auto_to_object)]
146+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
147+
148+
Future breakage diagnostic:
149+
error: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
150+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:40:5
151+
|
152+
LL | a as _
153+
| ^^^^^^
154+
|
155+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
156+
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
157+
note: the lint level is defined here
158+
--> $DIR/ptr-to-trait-obj-wrap-add-auto.rs:6:9
159+
|
160+
LL | #![deny(ptr_cast_add_auto_to_object)]
161+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
162+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Combination of `ptr-to-trait-obj-different-args.rs` and `ptr-to-trait-obj-wrap.rs`.
2+
//
3+
// Checks that you *can't* change type arguments of trait objects in pointer casts involving
4+
// wrapping said traits structures.
5+
#![allow(unused)]
6+
7+
trait A<T> {}
8+
9+
struct W<T: ?Sized>(T);
10+
struct X<T: ?Sized>(T);
11+
12+
fn unwrap<F, G>(a: *const W<dyn A<F>>) -> *const dyn A<G> {
13+
a as _
14+
//~^ error
15+
//~| error
16+
}
17+
18+
fn unwrap_nested<F, G>(a: *const W<W<dyn A<F>>>) -> *const W<dyn A<G>> {
19+
a as _
20+
//~^ error
21+
//~| error
22+
}
23+
24+
fn rewrap<F, G>(a: *const W<dyn A<F>>) -> *const X<dyn A<G>> {
25+
a as _
26+
//~^ error: casting `*const W<(dyn A<F> + 'static)>` as `*const X<dyn A<G>>` is invalid
27+
}
28+
29+
fn rewrap_nested<F, G>(a: *const W<W<dyn A<F>>>) -> *const W<X<dyn A<G>>> {
30+
a as _
31+
//~^ error: casting `*const W<W<(dyn A<F> + 'static)>>` as `*const W<X<dyn A<G>>>` is invalid
32+
}
33+
34+
fn wrap<F, G>(a: *const dyn A<F>) -> *const W<dyn A<G>> {
35+
a as _
36+
//~^ error: casting `*const (dyn A<F> + 'static)` as `*const W<dyn A<G>>` is invalid
37+
}
38+
39+
fn main() {}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
error[E0277]: the trait bound `W<(dyn A<F> + 'static)>: A<G>` is not satisfied
2+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:13:5
3+
|
4+
LL | a as _
5+
| ^ the trait `A<G>` is not implemented for `W<(dyn A<F> + 'static)>`
6+
|
7+
help: this trait has no implementations, consider adding one
8+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:7:1
9+
|
10+
LL | trait A<T> {}
11+
| ^^^^^^^^^^
12+
= note: required for the cast from `*const W<(dyn A<F> + 'static)>` to `*const dyn A<G>`
13+
14+
error[E0277]: the size for values of type `(dyn A<F> + 'static)` cannot be known at compilation time
15+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:13:5
16+
|
17+
LL | a as _
18+
| ^ doesn't have a size known at compile-time
19+
|
20+
= help: within `W<(dyn A<F> + 'static)>`, the trait `Sized` is not implemented for `(dyn A<F> + 'static)`
21+
note: required because it appears within the type `W<(dyn A<F> + 'static)>`
22+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:9:8
23+
|
24+
LL | struct W<T: ?Sized>(T);
25+
| ^
26+
= note: required for the cast from `*const W<(dyn A<F> + 'static)>` to `*const dyn A<G>`
27+
28+
error[E0277]: the trait bound `W<(dyn A<F> + 'static)>: A<G>` is not satisfied
29+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:19:5
30+
|
31+
LL | a as _
32+
| ^ the trait `A<G>` is not implemented for `W<(dyn A<F> + 'static)>`
33+
|
34+
help: this trait has no implementations, consider adding one
35+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:7:1
36+
|
37+
LL | trait A<T> {}
38+
| ^^^^^^^^^^
39+
= note: required for the cast from `*const W<W<(dyn A<F> + 'static)>>` to `*const W<dyn A<G>>`
40+
41+
error[E0277]: the size for values of type `(dyn A<F> + 'static)` cannot be known at compilation time
42+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:19:5
43+
|
44+
LL | a as _
45+
| ^ doesn't have a size known at compile-time
46+
|
47+
= help: within `W<(dyn A<F> + 'static)>`, the trait `Sized` is not implemented for `(dyn A<F> + 'static)`
48+
note: required because it appears within the type `W<(dyn A<F> + 'static)>`
49+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:9:8
50+
|
51+
LL | struct W<T: ?Sized>(T);
52+
| ^
53+
= note: required for the cast from `*const W<W<(dyn A<F> + 'static)>>` to `*const W<dyn A<G>>`
54+
55+
error[E0606]: casting `*const W<(dyn A<F> + 'static)>` as `*const X<dyn A<G>>` is invalid
56+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:25:5
57+
|
58+
LL | a as _
59+
| ^^^^^^
60+
|
61+
= note: the trait objects may have different vtables
62+
63+
error[E0606]: casting `*const W<W<(dyn A<F> + 'static)>>` as `*const W<X<dyn A<G>>>` is invalid
64+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:30:5
65+
|
66+
LL | a as _
67+
| ^^^^^^
68+
|
69+
= note: the trait objects may have different vtables
70+
71+
error[E0606]: casting `*const (dyn A<F> + 'static)` as `*const W<dyn A<G>>` is invalid
72+
--> $DIR/ptr-to-trait-obj-wrap-different-args.rs:35:5
73+
|
74+
LL | a as _
75+
| ^^^^^^
76+
|
77+
= note: the trait objects may have different vtables
78+
79+
error: aborting due to 7 previous errors
80+
81+
Some errors have detailed explanations: E0277, E0606.
82+
For more information about an error, try `rustc --explain E0277`.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Combination of `ptr-to-trait-obj-different-regions-misc.rs` and `ptr-to-trait-obj-wrap.rs`.
2+
//
3+
// Checks that you *can't* change lifetime arguments of trait objects in pointer casts involving
4+
// wrapping said traits structures.
5+
#![allow(unused)]
6+
7+
trait A<'a> {}
8+
9+
struct W<T: ?Sized>(T);
10+
struct X<T: ?Sized>(T);
11+
12+
fn unwrap<'a, 'b>(a: *const W<dyn A<'a>>) -> *const dyn A<'b> {
13+
a as _
14+
//~^ error
15+
//~| error
16+
}
17+
18+
fn unwrap_nested<'a, 'b>(a: *const W<W<dyn A<'a>>>) -> *const W<dyn A<'b>> {
19+
a as _
20+
//~^ error
21+
//~| error
22+
}
23+
24+
fn rewrap<'a, 'b>(a: *const W<dyn A<'a>>) -> *const X<dyn A<'b>> {
25+
a as _
26+
//~^ error: lifetime may not live long enough
27+
//~| error: lifetime may not live long enough
28+
}
29+
30+
fn rewrap_nested<'a, 'b>(a: *const W<W<dyn A<'a>>>) -> *const W<X<dyn A<'b>>> {
31+
a as _
32+
//~^ error: lifetime may not live long enough
33+
//~| error: lifetime may not live long enough
34+
}
35+
36+
fn wrap<'a, 'b>(a: *const dyn A<'a>) -> *const W<dyn A<'b>> {
37+
a as _
38+
//~^ error: lifetime may not live long enough
39+
//~| error: lifetime may not live long enough
40+
}
41+
42+
fn main() {}

0 commit comments

Comments
 (0)