Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 22, 2024
1 parent f75361f commit 4e07699
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/ui/impl-trait/rpit/early_bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::convert::identity;

fn test<'a: 'a>(n: bool) -> impl Sized + 'a {
//~^ ERROR concrete type differs from previous defining opaque type use
let true = n else { loop {} };
let _ = || {
let _ = identity::<&'a ()>(test(false));
//~^ ERROR hidden type for `impl Sized + 'a` captures lifetime that does not appear in bounds
};
loop {}
}

fn main() {}
26 changes: 26 additions & 0 deletions tests/ui/impl-trait/rpit/early_bound.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0700]: hidden type for `impl Sized + 'a` captures lifetime that does not appear in bounds
--> $DIR/early_bound.rs:7:17
|
LL | fn test<'a: 'a>(n: bool) -> impl Sized + 'a {
| -- --------------- opaque type defined here
| |
| hidden type `&'a ()` captures the lifetime `'a` as defined here
...
LL | let _ = identity::<&'a ()>(test(false));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: concrete type differs from previous defining opaque type use
--> $DIR/early_bound.rs:3:29
|
LL | fn test<'a: 'a>(n: bool) -> impl Sized + 'a {
| ^^^^^^^^^^^^^^^ expected `&()`, got `()`
|
note: previous use here
--> $DIR/early_bound.rs:7:36
|
LL | let _ = identity::<&'a ()>(test(false));
| ^^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0700`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! Check that we cannot instantiate a hidden type in the body
//! of an assoc fn or const unless mentioned in the signature.
#![feature(impl_trait_in_assoc_type)]

trait Trait: Sized {
type Assoc;
fn foo();
fn bar() -> Self::Assoc;
}

impl Trait for () {
type Assoc = impl std::fmt::Debug;
fn foo() {
let x: Self::Assoc = 42; //~ ERROR: mismatched types
}
fn bar() -> Self::Assoc {
""
}
}

trait Trait2: Sized {
type Assoc;
const FOO: ();
fn bar() -> Self::Assoc;
}

impl Trait2 for () {
type Assoc = impl std::fmt::Debug;
const FOO: () = {
let x: Self::Assoc = 42; //~ ERROR: mismatched types
};
fn bar() -> Self::Assoc {
""
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
error[E0308]: mismatched types
--> $DIR/impl_trait_in_trait_defined_outside_trait.rs:15:30
|
LL | type Assoc = impl std::fmt::Debug;
| -------------------- the expected opaque type
LL | fn foo() {
LL | let x: Self::Assoc = 42;
| ----------- ^^ expected opaque type, found integer
| |
| expected due to this
|
= note: expected opaque type `<() as Trait>::Assoc`
found type `{integer}`
note: this item must have the opaque type in its signature in order to be able to register hidden types
--> $DIR/impl_trait_in_trait_defined_outside_trait.rs:14:8
|
LL | fn foo() {
| ^^^

error[E0308]: mismatched types
--> $DIR/impl_trait_in_trait_defined_outside_trait.rs:31:30
|
LL | type Assoc = impl std::fmt::Debug;
| -------------------- the expected opaque type
LL | const FOO: () = {
LL | let x: Self::Assoc = 42;
| ----------- ^^ expected opaque type, found integer
| |
| expected due to this
|
= note: expected opaque type `<() as Trait2>::Assoc`
found type `{integer}`
note: this item must have the opaque type in its signature in order to be able to register hidden types
--> $DIR/impl_trait_in_trait_defined_outside_trait.rs:30:11
|
LL | const FOO: () = {
| ^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! Check that we cannot instantiate a hidden type from another assoc type.
#![feature(impl_trait_in_assoc_type)]

trait Trait: Sized {
type Assoc;
type Foo;
fn foo() -> Self::Assoc;
}

impl Trait for () {
type Assoc = impl std::fmt::Debug;
type Foo = [(); {
let x: Self::Assoc = 42; //~ ERROR: mismatched types
3
}];
fn foo() -> Self::Assoc {
""
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0308]: mismatched types
--> $DIR/impl_trait_in_trait_defined_outside_trait2.rs:14:30
|
LL | type Assoc = impl std::fmt::Debug;
| -------------------- the expected opaque type
LL | type Foo = [(); {
LL | let x: Self::Assoc = 42;
| ----------- ^^ expected opaque type, found integer
| |
| expected due to this
|
= note: expected opaque type `<() as Trait>::Assoc`
found type `{integer}`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! Check that non-defining assoc items can use the opaque type
//! opaquely.
// check-pass

#![feature(impl_trait_in_assoc_type)]

trait Trait: Sized {
type Assoc;
fn foo();
fn bar() -> Self::Assoc;
}

impl Trait for () {
type Assoc = impl std::fmt::Debug;
fn foo() {
let x: Self::Assoc = Self::bar();
}
fn bar() -> Self::Assoc {
""
}
}

trait Trait2: Sized {
type Assoc;
const FOO: ();
const BAR: Self::Assoc;
}

impl Trait2 for () {
type Assoc = impl Copy;
const FOO: () = {
let x: Self::Assoc = Self::BAR;
};
const BAR: Self::Assoc = "";
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/type-alias-impl-trait/in-assoc-ty-early-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(impl_trait_in_assoc_type)]

trait Foo {
type Assoc<'a, 'b>;
fn bar<'a: 'a, 'b: 'b>(_: &'a ()) -> Self::Assoc<'a, 'b>;
}

impl Foo for () {
type Assoc<'a, 'b> = impl Sized;
fn bar<'a: 'a, 'b: 'b>(x: &'a ()) -> Self::Assoc<'a, 'b> {
let closure = |x: &'a ()| -> Self::Assoc<'b, 'a> { x };
//~^ ERROR `<() as Foo>::Assoc<'b, 'a>` captures lifetime that does not appear in bounds
x
}
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/type-alias-impl-trait/in-assoc-ty-early-bound.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0700]: hidden type for `<() as Foo>::Assoc<'b, 'a>` captures lifetime that does not appear in bounds
--> $DIR/in-assoc-ty-early-bound.rs:11:60
|
LL | type Assoc<'a, 'b> = impl Sized;
| ---------- opaque type defined here
LL | fn bar<'a: 'a, 'b: 'b>(x: &'a ()) -> Self::Assoc<'a, 'b> {
| -- hidden type `&'a ()` captures the lifetime `'a` as defined here
LL | let closure = |x: &'a ()| -> Self::Assoc<'b, 'a> { x };
| ^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0700`.
21 changes: 21 additions & 0 deletions tests/ui/type-alias-impl-trait/in-assoc-ty-early-bound2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(impl_trait_in_assoc_type)]

trait Foo {
type Assoc<'a>;
fn bar<'a: 'a>();
}

impl Foo for () {
type Assoc<'a> = impl Sized; //~ ERROR unconstrained opaque type
fn bar<'a: 'a>()
where
Self::Assoc<'a>:,
{
let _ = |x: &'a ()| {
let _: Self::Assoc<'a> = x;
//~^ ERROR `<() as Foo>::Assoc<'a>` captures lifetime that does not appear in bound
};
}
}

fn main() {}
22 changes: 22 additions & 0 deletions tests/ui/type-alias-impl-trait/in-assoc-ty-early-bound2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0700]: hidden type for `<() as Foo>::Assoc<'a>` captures lifetime that does not appear in bounds
--> $DIR/in-assoc-ty-early-bound2.rs:15:20
|
LL | type Assoc<'a> = impl Sized;
| ---------- opaque type defined here
LL | fn bar<'a: 'a>()
| -- hidden type `&'a ()` captures the lifetime `'a` as defined here
...
LL | let _: Self::Assoc<'a> = x;
| ^^^^^^^^^^^^^^^

error: unconstrained opaque type
--> $DIR/in-assoc-ty-early-bound2.rs:9:22
|
LL | type Assoc<'a> = impl Sized;
| ^^^^^^^^^^
|
= note: `Assoc` must be used in combination with a concrete type within the same impl

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0700`.

0 comments on commit 4e07699

Please sign in to comment.