Skip to content

Commit 8302f2e

Browse files
author
Lukas Markeffsky
committed
add even more tests for ptr-to-ptr casts on trait objects
1 parent a3f76a2 commit 8302f2e

4 files changed

+92
-7
lines changed

Diff for: tests/ui/cast/ptr-to-trait-obj-different-regions-misc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ fn change_lt_ba<'a, 'b: 'a>(x: *mut dyn Trait<'a>) -> *mut dyn Trait<'b> {
1515
x as _ //~ error: lifetime may not live long enough
1616
}
1717

18+
fn change_lt_hr<'a>(x: *mut dyn Trait<'a>) -> *mut dyn for<'b> Trait<'b> {
19+
x as _ //~ error: lifetime may not live long enough
20+
//~^ error: mismatched types
21+
//~| one type is more general than the other
22+
}
23+
1824
trait Assocked {
1925
type Assoc: ?Sized;
2026
}

Diff for: tests/ui/cast/ptr-to-trait-obj-different-regions-misc.stderr

+29-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,29 @@ LL | x as _
6161
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
6262

6363
error: lifetime may not live long enough
64-
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:25:5
64+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:19:5
65+
|
66+
LL | fn change_lt_hr<'a>(x: *mut dyn Trait<'a>) -> *mut dyn for<'b> Trait<'b> {
67+
| -- lifetime `'a` defined here
68+
LL | x as _
69+
| ^^^^^^ cast requires that `'a` must outlive `'static`
70+
|
71+
help: to declare that the trait object captures data from argument `x`, you can add an explicit `'a` lifetime bound
72+
|
73+
LL | fn change_lt_hr<'a>(x: *mut dyn Trait<'a>) -> *mut dyn for<'b> Trait<'b> + 'a {
74+
| ++++
75+
76+
error[E0308]: mismatched types
77+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:19:5
78+
|
79+
LL | x as _
80+
| ^^^^^^ one type is more general than the other
81+
|
82+
= note: expected trait object `dyn for<'b> Trait<'b>`
83+
found trait object `dyn Trait<'_>`
84+
85+
error: lifetime may not live long enough
86+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:31:5
6587
|
6688
LL | fn change_assoc_0<'a, 'b>(
6789
| -- -- lifetime `'b` defined here
@@ -77,7 +99,7 @@ LL | x as _
7799
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
78100

79101
error: lifetime may not live long enough
80-
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:25:5
102+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:31:5
81103
|
82104
LL | fn change_assoc_0<'a, 'b>(
83105
| -- -- lifetime `'b` defined here
@@ -97,7 +119,7 @@ help: `'b` and `'a` must be the same: replace one with the other
97119
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
98120

99121
error: lifetime may not live long enough
100-
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:32:5
122+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:38:5
101123
|
102124
LL | fn change_assoc_1<'a, 'b>(
103125
| -- -- lifetime `'b` defined here
@@ -113,7 +135,7 @@ LL | x as _
113135
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
114136

115137
error: lifetime may not live long enough
116-
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:32:5
138+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:38:5
117139
|
118140
LL | fn change_assoc_1<'a, 'b>(
119141
| -- -- lifetime `'b` defined here
@@ -133,12 +155,13 @@ help: `'b` and `'a` must be the same: replace one with the other
133155
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
134156

135157
error: lifetime may not live long enough
136-
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:39:20
158+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:45:20
137159
|
138160
LL | fn extend_to_static<'a>(ptr: *const dyn Trait<'a>) {
139161
| -- lifetime `'a` defined here
140162
LL | require_static(ptr as _)
141163
| ^^^^^^^^ cast requires that `'a` must outlive `'static`
142164

143-
error: aborting due to 9 previous errors
165+
error: aborting due to 11 previous errors
144166

167+
For more information about this error, try `rustc --explain E0308`.

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

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ check-pass
1+
//@ check-fail
22

33
trait Trait<'a> {}
44

@@ -10,8 +10,40 @@ fn cast_inherent_lt<'a, 'b>(x: *mut (dyn Trait<'static> + 'a)) -> *mut (dyn Trai
1010
x as _
1111
}
1212

13+
fn cast_away_higher_ranked<'a>(x: *mut dyn for<'b> Trait<'b>) -> *mut dyn Trait<'a> {
14+
x as _
15+
}
16+
1317
fn unprincipled<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'b) {
1418
x as _
1519
}
1620

21+
// If it is possible to coerce from the source to the target type modulo
22+
// regions, then we skip the HIR checks for ptr-to-ptr casts and possibly
23+
// insert an unsizing coercion into the MIR before the ptr-to-ptr cast.
24+
// By wrapping the target type, we ensure that no coercion happens
25+
// and also test the non-coercion cast behavior.
26+
struct Wrapper<T: ?Sized>(T);
27+
28+
fn remove_auto_wrap<'a>(x: *mut (dyn Trait<'a> + Send)) -> *mut Wrapper<dyn Trait<'a>> {
29+
x as _
30+
}
31+
32+
fn cast_inherent_lt_wrap<'a, 'b>(
33+
x: *mut (dyn Trait<'static> + 'a),
34+
) -> *mut Wrapper<dyn Trait<'static> + 'b> {
35+
x as _
36+
}
37+
38+
fn cast_away_higher_ranked_wrap<'a>(x: *mut dyn for<'b> Trait<'b>) -> *mut Wrapper<dyn Trait<'a>> {
39+
x as _
40+
//~^ error: lifetime may not live long enough
41+
//~| error: mismatched types
42+
//~| one type is more general than the other
43+
}
44+
45+
fn unprincipled_wrap<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sync + 'b> {
46+
x as _
47+
}
48+
1749
fn main() {}

Diff for: tests/ui/cast/ptr-to-trait-obj-ok.stderr

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/ptr-to-trait-obj-ok.rs:39:5
3+
|
4+
LL | fn cast_away_higher_ranked_wrap<'a>(x: *mut dyn for<'b> Trait<'b>) -> *mut Wrapper<dyn Trait<'a>> {
5+
| -- lifetime `'a` defined here
6+
LL | x as _
7+
| ^^^^^^ returning this value requires that `'a` must outlive `'static`
8+
|
9+
= note: requirement occurs because of a mutable pointer to `Wrapper<dyn Trait<'_>>`
10+
= note: mutable pointers are invariant over their type parameter
11+
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/ptr-to-trait-obj-ok.rs:39:5
15+
|
16+
LL | x as _
17+
| ^^^^^^ one type is more general than the other
18+
|
19+
= note: expected trait object `dyn for<'b> Trait<'b>`
20+
found trait object `dyn Trait<'_>`
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)