Skip to content

Commit 5c7942f

Browse files
committed
Add some more tests
1 parent 137a9fd commit 5c7942f

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

tests/ui/traits/trait-upcasting/type-checking-test-4.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ fn test_correct(x: &dyn Foo<'static>) {
1111
let _ = x as &dyn Bar<'static, 'static>;
1212
}
1313

14+
fn test_correct2<'a>(x: &dyn Foo<'a>) {
15+
let _ = x as &dyn Bar<'_, '_>;
16+
}
17+
1418
fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) {
1519
let _ = x as &dyn Bar<'static, 'a>; // Error
1620
//~^ ERROR lifetime may not live long enough

tests/ui/traits/trait-upcasting/type-checking-test-4.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
error: lifetime may not live long enough
2-
--> $DIR/type-checking-test-4.rs:15:13
2+
--> $DIR/type-checking-test-4.rs:19:13
33
|
44
LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) {
55
| -- lifetime `'a` defined here
66
LL | let _ = x as &dyn Bar<'static, 'a>; // Error
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
88

99
error: lifetime may not live long enough
10-
--> $DIR/type-checking-test-4.rs:20:13
10+
--> $DIR/type-checking-test-4.rs:24:13
1111
|
1212
LL | fn test_wrong2<'a>(x: &dyn Foo<'static>, y: &'a u32) {
1313
| -- lifetime `'a` defined here
1414
LL | let _ = x as &dyn Bar<'a, 'static>; // Error
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
1616

1717
error: lifetime may not live long enough
18-
--> $DIR/type-checking-test-4.rs:26:5
18+
--> $DIR/type-checking-test-4.rs:30:5
1919
|
2020
LL | fn test_wrong3<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> {
2121
| -- lifetime `'a` defined here
@@ -24,23 +24,23 @@ LL | y.get_b() // ERROR
2424
| ^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
2525

2626
error: lifetime may not live long enough
27-
--> $DIR/type-checking-test-4.rs:31:5
27+
--> $DIR/type-checking-test-4.rs:35:5
2828
|
2929
LL | fn test_wrong4<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> {
3030
| -- lifetime `'a` defined here
3131
LL | <_ as Bar>::get_b(x) // ERROR
3232
| ^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
3333

3434
error: lifetime may not live long enough
35-
--> $DIR/type-checking-test-4.rs:36:5
35+
--> $DIR/type-checking-test-4.rs:40:5
3636
|
3737
LL | fn test_wrong5<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> {
3838
| -- lifetime `'a` defined here
3939
LL | <_ as Bar<'_, '_>>::get_b(x) // ERROR
4040
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
4141

4242
error: lifetime may not live long enough
43-
--> $DIR/type-checking-test-4.rs:44:5
43+
--> $DIR/type-checking-test-4.rs:48:5
4444
|
4545
LL | fn test_wrong6<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> {
4646
| -- lifetime `'a` defined here
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(trait_upcasting, type_alias_impl_trait)]
2+
3+
type Tait = impl Sized;
4+
5+
trait Foo<'a>: Bar<'a, 'a, Tait> {}
6+
trait Bar<'a, 'b, T> {}
7+
8+
fn test_correct(x: &dyn Foo<'static>) {
9+
let _ = x as &dyn Bar<'static, 'static, Tait>;
10+
}
11+
12+
fn test_correct2<'a>(x: &dyn Foo<'a>) {
13+
let _ = x as &dyn Bar<'_, '_, Tait>;
14+
}
15+
16+
fn test_correct3<'a>(x: &dyn Foo<'a>, _: Tait) {
17+
let _ = x as &dyn Bar<'_, '_, ()>;
18+
//~^ ERROR: non-primitive cast
19+
}
20+
21+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0605]: non-primitive cast: `&dyn Foo<'a>` as `&dyn Bar<'_, '_, ()>`
2+
--> $DIR/type-checking-test-opaques.rs:17:13
3+
|
4+
LL | let _ = x as &dyn Bar<'_, '_, ()>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0605`.

0 commit comments

Comments
 (0)