Skip to content

Commit d652cd4

Browse files
lqdoli-obk
authored andcommitted
add regression tests for opaque types in impl headers
1 parent 7d5bbf5 commit d652cd4

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Regression test for issues #84660 and #86411: both are variations on #76202.
2+
// Tests that we don't ICE when we have an opaque type appearing anywhere in an impl header.
3+
4+
#![feature(min_type_alias_impl_trait)]
5+
6+
trait Foo {}
7+
impl Foo for () {}
8+
type Bar = impl Foo;
9+
fn _defining_use() -> Bar {}
10+
11+
trait TraitArg<T> {
12+
fn f();
13+
}
14+
15+
impl TraitArg<Bar> for () { //~ ERROR cannot implement trait
16+
fn f() {
17+
println!("ho");
18+
}
19+
}
20+
21+
fn main() {
22+
<() as TraitArg<Bar>>::f();
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot implement trait on type alias impl trait
2+
--> $DIR/issue-84660-trait-impl-for-tait.rs:15:1
3+
|
4+
LL | impl TraitArg<Bar> for () {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: type alias impl trait defined here
8+
--> $DIR/issue-84660-trait-impl-for-tait.rs:8:12
9+
|
10+
LL | type Bar = impl Foo;
11+
| ^^^^^^^^
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Another example from issue #84660, this time weaponized as a safe transmut: an opaque type in an
2+
// impl header being accepted was used to create unsoundness.
3+
4+
#![feature(min_type_alias_impl_trait)]
5+
6+
trait Foo {}
7+
impl Foo for () {}
8+
type Bar = impl Foo;
9+
fn _defining_use() -> Bar {}
10+
11+
trait Trait<T, In> {
12+
type Out;
13+
fn convert(i: In) -> Self::Out;
14+
}
15+
16+
impl<In, Out> Trait<Bar, In> for Out { //~ ERROR cannot implement trait
17+
type Out = Out;
18+
fn convert(_i: In) -> Self::Out {
19+
unreachable!();
20+
}
21+
}
22+
23+
impl<In, Out> Trait<(), In> for Out {
24+
type Out = In;
25+
fn convert(i: In) -> Self::Out {
26+
i
27+
}
28+
}
29+
30+
fn transmute<In, Out>(i: In) -> Out {
31+
<Out as Trait<Bar, In>>::convert(i)
32+
}
33+
34+
fn main() {
35+
let d;
36+
{
37+
let x = "Hello World".to_string();
38+
d = transmute::<&String, &String>(&x);
39+
}
40+
println!("{}", d);
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot implement trait on type alias impl trait
2+
--> $DIR/issue-84660-unsoundness.rs:16:1
3+
|
4+
LL | impl<In, Out> Trait<Bar, In> for Out {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: type alias impl trait defined here
8+
--> $DIR/issue-84660-unsoundness.rs:8:12
9+
|
10+
LL | type Bar = impl Foo;
11+
| ^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)