1
Fork 0

add regression tests for opaque types in impl headers

This commit is contained in:
Rémy Rakic 2021-07-22 20:26:46 +02:00 committed by Oli Scherer
parent 7d5bbf55f2
commit d652cd4959
4 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,23 @@
// Regression test for issues #84660 and #86411: both are variations on #76202.
// Tests that we don't ICE when we have an opaque type appearing anywhere in an impl header.
#![feature(min_type_alias_impl_trait)]
trait Foo {}
impl Foo for () {}
type Bar = impl Foo;
fn _defining_use() -> Bar {}
trait TraitArg<T> {
fn f();
}
impl TraitArg<Bar> for () { //~ ERROR cannot implement trait
fn f() {
println!("ho");
}
}
fn main() {
<() as TraitArg<Bar>>::f();
}

View file

@ -0,0 +1,14 @@
error: cannot implement trait on type alias impl trait
--> $DIR/issue-84660-trait-impl-for-tait.rs:15:1
|
LL | impl TraitArg<Bar> for () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: type alias impl trait defined here
--> $DIR/issue-84660-trait-impl-for-tait.rs:8:12
|
LL | type Bar = impl Foo;
| ^^^^^^^^
error: aborting due to previous error

View file

@ -0,0 +1,41 @@
// Another example from issue #84660, this time weaponized as a safe transmut: an opaque type in an
// impl header being accepted was used to create unsoundness.
#![feature(min_type_alias_impl_trait)]
trait Foo {}
impl Foo for () {}
type Bar = impl Foo;
fn _defining_use() -> Bar {}
trait Trait<T, In> {
type Out;
fn convert(i: In) -> Self::Out;
}
impl<In, Out> Trait<Bar, In> for Out { //~ ERROR cannot implement trait
type Out = Out;
fn convert(_i: In) -> Self::Out {
unreachable!();
}
}
impl<In, Out> Trait<(), In> for Out {
type Out = In;
fn convert(i: In) -> Self::Out {
i
}
}
fn transmute<In, Out>(i: In) -> Out {
<Out as Trait<Bar, In>>::convert(i)
}
fn main() {
let d;
{
let x = "Hello World".to_string();
d = transmute::<&String, &String>(&x);
}
println!("{}", d);
}

View file

@ -0,0 +1,14 @@
error: cannot implement trait on type alias impl trait
--> $DIR/issue-84660-unsoundness.rs:16:1
|
LL | impl<In, Out> Trait<Bar, In> for Out {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: type alias impl trait defined here
--> $DIR/issue-84660-unsoundness.rs:8:12
|
LL | type Bar = impl Foo;
| ^^^^^^^^
error: aborting due to previous error