Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed May 23, 2024
1 parent ed172db commit dc8d1bc
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 6 deletions.
14 changes: 14 additions & 0 deletions tests/ui/impl-trait/unsize_adt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Test that we do not allow unsizing `Foo<[Opaque; N]>` to `Foo<[Concrete]>`.

struct Foo<T: ?Sized>(T);

fn hello() -> Foo<[impl Sized; 2]> {
if false {
let x = hello();
let _: &Foo<[i32]> = &x;
//~^ ERROR: mismatched types
}
todo!()
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/impl-trait/unsize_adt.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0308]: mismatched types
--> $DIR/unsize_adt.rs:8:30
|
LL | fn hello() -> Foo<[impl Sized; 2]> {
| ---------- the found opaque type
...
LL | let _: &Foo<[i32]> = &x;
| ----------- ^^ expected `&Foo<[i32]>`, found `&Foo<[impl Sized; 2]>`
| |
| expected due to this
|
= note: expected reference `&Foo<[i32]>`
found reference `&Foo<[impl Sized; 2]>`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
12 changes: 12 additions & 0 deletions tests/ui/impl-trait/unsize_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Test that we do not allow unsizing `[Opaque; N]` to `[Concrete]`.

fn hello() -> [impl Sized; 2] {
if false {
let x = hello();
let _: &[i32] = &x;
//~^ ERROR: mismatched types
}
todo!()
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/impl-trait/unsize_slice.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0308]: mismatched types
--> $DIR/unsize_slice.rs:6:25
|
LL | fn hello() -> [impl Sized; 2] {
| ---------- the found opaque type
...
LL | let _: &[i32] = &x;
| ------ ^^ expected `&[i32]`, found `&[impl Sized; 2]`
| |
| expected due to this
|
= note: expected reference `&[i32]`
found reference `&[impl Sized; 2]`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
14 changes: 14 additions & 0 deletions tests/ui/impl-trait/unsize_tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Test that we do not allow unsizing `([Opaque; N],)` to `([Concrete],)`.

#![feature(unsized_tuple_coercion)]

fn hello() -> ([impl Sized; 2],) {
if false {
let x = hello();
let _: &([i32],) = &x;
//~^ ERROR: mismatched types
}
todo!()
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/impl-trait/unsize_tuple.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0308]: mismatched types
--> $DIR/unsize_tuple.rs:8:28
|
LL | fn hello() -> ([impl Sized; 2],) {
| ---------- the found opaque type
...
LL | let _: &([i32],) = &x;
| --------- ^^ expected `&([i32],)`, found `&([impl Sized; 2],)`
| |
| expected due to this
|
= note: expected reference `&([i32],)`
found reference `&([impl Sized; 2],)`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
28 changes: 28 additions & 0 deletions tests/ui/traits/trait-upcasting/illegal-upcast-to-impl-opaque.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//@ revisions: current next
//@[next] compile-flags: -Znext-solver
//@[next] failure-status: 101
//@[next] known-bug: unknown
//@[next] normalize-stderr-test "note: .*\n\n" -> ""
//@[next] normalize-stderr-test "thread 'rustc' panicked.*\n.*\n" -> ""
//@[next] normalize-stderr-test "(error: internal compiler error: [^:]+):\d+:\d+: " -> "$1:LL:CC: "
//@[next] normalize-stderr-test "delayed at .*" -> ""
//@[next] rustc-env:RUST_BACKTRACE=0
//@ check-pass

#![feature(trait_upcasting)]

trait Super {
type Assoc;
}

trait Sub: Super {}

impl<T: ?Sized> Super for T {
type Assoc = i32;
}

fn illegal(x: &dyn Sub<Assoc = i32>) -> &dyn Super<Assoc = impl Sized> {
x
}

fn main() {}
4 changes: 4 additions & 0 deletions tests/ui/traits/trait-upcasting/type-checking-test-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ fn test_correct(x: &dyn Foo<'static>) {
let _ = x as &dyn Bar<'static, 'static>;
}

fn test_correct2<'a>(x: &dyn Foo<'a>) {
let _ = x as &dyn Bar<'_, '_>;
}

fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) {
let _ = x as &dyn Bar<'static, 'a>; // Error
//~^ ERROR lifetime may not live long enough
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/traits/trait-upcasting/type-checking-test-4.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
error: lifetime may not live long enough
--> $DIR/type-checking-test-4.rs:15:13
--> $DIR/type-checking-test-4.rs:19:13
|
LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) {
| -- lifetime `'a` defined here
LL | let _ = x as &dyn Bar<'static, 'a>; // Error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`

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

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

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

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

error: lifetime may not live long enough
--> $DIR/type-checking-test-4.rs:44:5
--> $DIR/type-checking-test-4.rs:48:5
|
LL | fn test_wrong6<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> {
| -- lifetime `'a` defined here
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/traits/trait-upcasting/type-checking-test-opaques.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(trait_upcasting, type_alias_impl_trait)]

type Tait = impl Sized;

trait Foo<'a>: Bar<'a, 'a, Tait> {}
trait Bar<'a, 'b, T> {}

fn test_correct(x: &dyn Foo<'static>) {
let _ = x as &dyn Bar<'static, 'static, Tait>;
}

fn test_correct2<'a>(x: &dyn Foo<'a>) {
let _ = x as &dyn Bar<'_, '_, Tait>;
}

fn test_correct3<'a>(x: &dyn Foo<'a>, _: Tait) {
let _ = x as &dyn Bar<'_, '_, ()>;
//~^ ERROR: non-primitive cast
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0605]: non-primitive cast: `&dyn Foo<'a>` as `&dyn Bar<'_, '_, ()>`
--> $DIR/type-checking-test-opaques.rs:17:13
|
LL | let _ = x as &dyn Bar<'_, '_, ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0605`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0277]: the trait bound `Foo: Trait<Bar>` is not satisfied
--> $DIR/constrain_in_projection.rs:24:14
|
LL | let x = <Foo as Trait<Bar>>::Assoc::default();
| ^^^ the trait `Trait<Bar>` is not implemented for `Foo`
|
= help: the trait `Trait<()>` is implemented for `Foo`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
28 changes: 28 additions & 0 deletions tests/ui/type-alias-impl-trait/constrain_in_projection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Check that projections will constrain opaque types while looking for
//! matching impls.

//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver
//@[next]check-pass

#![feature(type_alias_impl_trait)]

struct Foo;

type Bar = impl Sized;

trait Trait<T> {
type Assoc: Default;
}

impl Trait<()> for Foo {
type Assoc = u32;
}

fn bop(_: Bar) {
let x = <Foo as Trait<Bar>>::Assoc::default();
//[current]~^ `Foo: Trait<Bar>` is not satisfied
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0277]: the trait bound `Foo: Trait<Bar>` is not satisfied
--> $DIR/constrain_in_projection2.rs:27:14
|
LL | let x = <Foo as Trait<Bar>>::Assoc::default();
| ^^^ the trait `Trait<Bar>` is not implemented for `Foo`
|
= help: the following other types implement trait `Trait<T>`:
<Foo as Trait<()>>
<Foo as Trait<u32>>

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0283]: type annotations needed: cannot satisfy `Foo: Trait<Bar>`
--> $DIR/constrain_in_projection2.rs:27:14
|
LL | let x = <Foo as Trait<Bar>>::Assoc::default();
| ^^^ help: use the fully qualified path to an implementation: `<Type as Trait>::Assoc`
|
note: multiple `impl`s satisfying `Foo: Trait<Bar>` found
--> $DIR/constrain_in_projection2.rs:18:1
|
LL | impl Trait<()> for Foo {
| ^^^^^^^^^^^^^^^^^^^^^^
...
LL | impl Trait<u32> for Foo {
| ^^^^^^^^^^^^^^^^^^^^^^^
= note: associated types cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0283`.
32 changes: 32 additions & 0 deletions tests/ui/type-alias-impl-trait/constrain_in_projection2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! Check that projections will constrain opaque types while looking for
//! matching impls and error if ambiguous.

//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver

#![feature(type_alias_impl_trait)]

struct Foo;

type Bar = impl Sized;

trait Trait<T> {
type Assoc: Default;
}

impl Trait<()> for Foo {
type Assoc = u32;
}

impl Trait<u32> for Foo {
type Assoc = u32;
}

fn bop(_: Bar) {
let x = <Foo as Trait<Bar>>::Assoc::default();
//[next]~^ ERROR: cannot satisfy `Foo: Trait<Bar>`
//[current]~^^ ERROR: `Foo: Trait<Bar>` is not satisfied
}

fn main() {}

0 comments on commit dc8d1bc

Please sign in to comment.