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 9, 2024
1 parent 4f8ebac commit 9155570
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
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() {}

0 comments on commit 9155570

Please sign in to comment.