Skip to content

Commit 152a6f1

Browse files
DarksonnBoxyUwU
authored andcommitted
Test for casts removing principal trait
1 parent 8a06cde commit 152a6f1

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

tests/ui/cast/ptr-to-ptr-principalless.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,29 @@ fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sy
4646
//~^ ERROR: lifetime may not live long enough
4747
}
4848

49+
// Cast away principal trait
50+
trait Trait {}
51+
52+
fn unprincipled3<'a, 'b>(x: *mut (dyn Trait + Send + 'a)) -> *mut (dyn Send + 'b) {
53+
x as _
54+
//~^ ERROR: lifetime may not live long enough
55+
}
56+
57+
fn unprincipled3_static<'a>(x: *mut (dyn Trait + Send + 'a)) -> *mut (dyn Send + 'static) {
58+
x as _
59+
//~^ ERROR: lifetime may not live long enough
60+
}
61+
62+
fn unprincipled_wrap3<'a, 'b>(x: *mut (dyn Trait + Send + 'a)) -> *mut Wrapper<dyn Send + 'b> {
63+
x as _
64+
//~^ ERROR: casting `*mut (dyn Trait + Send + 'a)` as `*mut Wrapper<(dyn Send + 'b)>` is invalid
65+
}
66+
67+
fn unprincipled_wrap3_static<'a>(
68+
x: *mut (dyn Trait + Send + 'a)
69+
) -> *mut Wrapper<dyn Send + 'static> {
70+
x as _
71+
//~^ ERROR: casting `*mut (dyn Trait + Send + 'a)` as `*mut Wrapper<(dyn Send + 'static)>` is invalid
72+
}
73+
4974
fn main() {}

tests/ui/cast/ptr-to-ptr-principalless.stderr

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
error[E0606]: casting `*mut (dyn Trait + Send + 'a)` as `*mut Wrapper<(dyn Send + 'b)>` is invalid
2+
--> $DIR/ptr-to-ptr-principalless.rs:63:5
3+
|
4+
LL | x as _
5+
| ^^^^^^
6+
|
7+
= note: the trait objects may have different vtables
8+
9+
error[E0606]: casting `*mut (dyn Trait + Send + 'a)` as `*mut Wrapper<(dyn Send + 'static)>` is invalid
10+
--> $DIR/ptr-to-ptr-principalless.rs:70:5
11+
|
12+
LL | x as _
13+
| ^^^^^^
14+
|
15+
= note: the trait objects may have different vtables
16+
117
error: lifetime may not live long enough
218
--> $DIR/ptr-to-ptr-principalless.rs:8:5
319
|
@@ -202,5 +218,57 @@ LL - fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<d
202218
LL + fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'static)) -> *mut Wrapper<dyn Sync + 'static> {
203219
|
204220

205-
error: aborting due to 8 previous errors
221+
error: lifetime may not live long enough
222+
--> $DIR/ptr-to-ptr-principalless.rs:53:5
223+
|
224+
LL | fn unprincipled3<'a, 'b>(x: *mut (dyn Trait + Send + 'a)) -> *mut (dyn Send + 'b) {
225+
| -- -- lifetime `'b` defined here
226+
| |
227+
| lifetime `'a` defined here
228+
LL | x as _
229+
| ^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
230+
|
231+
= help: consider adding the following bound: `'a: 'b`
232+
= note: requirement occurs because of a mutable pointer to `dyn Send`
233+
= note: mutable pointers are invariant over their type parameter
234+
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
235+
note: raw pointer casts of trait objects do not cast away lifetimes
236+
--> $DIR/ptr-to-ptr-principalless.rs:53:5
237+
|
238+
LL | x as _
239+
| ^^^^^^
240+
= note: this was previously accepted by the compiler but was changed recently
241+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
242+
243+
error: lifetime may not live long enough
244+
--> $DIR/ptr-to-ptr-principalless.rs:58:5
245+
|
246+
LL | fn unprincipled3_static<'a>(x: *mut (dyn Trait + Send + 'a)) -> *mut (dyn Send + 'static) {
247+
| -- lifetime `'a` defined here
248+
LL | x as _
249+
| ^^^^^^ returning this value requires that `'a` must outlive `'static`
250+
|
251+
= note: requirement occurs because of a mutable pointer to `dyn Send`
252+
= note: mutable pointers are invariant over their type parameter
253+
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
254+
note: raw pointer casts of trait objects do not cast away lifetimes
255+
--> $DIR/ptr-to-ptr-principalless.rs:58:5
256+
|
257+
LL | x as _
258+
| ^^^^^^
259+
= note: this was previously accepted by the compiler but was changed recently
260+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
261+
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `x`
262+
|
263+
LL - fn unprincipled3_static<'a>(x: *mut (dyn Trait + Send + 'a)) -> *mut (dyn Send + 'static) {
264+
LL + fn unprincipled3_static<'a>(x: *mut (dyn Trait + Send + 'a)) -> *mut (dyn Send + 'a) {
265+
|
266+
help: alternatively, add an explicit `'static` bound to this reference
267+
|
268+
LL - fn unprincipled3_static<'a>(x: *mut (dyn Trait + Send + 'a)) -> *mut (dyn Send + 'static) {
269+
LL + fn unprincipled3_static<'a>(x: *mut (dyn Trait + Send + 'static)) -> *mut (dyn Send + 'static) {
270+
|
271+
272+
error: aborting due to 12 previous errors
206273

274+
For more information about this error, try `rustc --explain E0606`.

tests/ui/cast/ptr-to-trait-obj-ok.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ fn unprincipled<'a: 'b, 'b>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'b) {
2020
x as _
2121
}
2222

23+
fn remove_principal<'a: 'b, 'b, 't>(x: *mut (dyn Trait<'t> + Send + 'a)) -> *mut (dyn Send + 'b) {
24+
x as _
25+
}
26+
2327
// If it is possible to coerce from the source to the target type modulo
2428
// regions, then we skip the HIR checks for ptr-to-ptr casts and possibly
2529
// insert an unsizing coercion into the MIR before the ptr-to-ptr cast.

0 commit comments

Comments
 (0)