-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #95973 - oli-obk:tait_ub3, r=compiler-errors
prevent opaque types from appearing in impl headers cc `@lqd` opaque types are not distinguishable from their hidden type at the codegen stage. So we could either end up with cases where the hidden type doesn't implement the trait (which will thus ICE) or where the hidden type does implement the trait (so we'd be using its impl instead of the one written for the opaque type). This can even lead to unsound behaviour without unsafe code. Fixes #86411. Fixes #84660. rebase of #87382 plus some diagnostic tweaks
- Loading branch information
Showing
18 changed files
with
204 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 9 additions & 7 deletions
16
src/test/ui/impl-trait/recursive-type-alias-impl-trait-declaration-too-subtle-2.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
error[E0277]: can't compare `Bar` with `(Bar, i32)` | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:14:5 | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:7:17 | ||
| | ||
LL | Bar | ||
| ^^^ no implementation for `Bar == (Bar, i32)` | ||
LL | impl PartialEq<(Foo, i32)> for Bar { | ||
| ^^^ | ||
| | ||
= help: the trait `PartialEq<(Bar, i32)>` is not implemented for `Bar` | ||
= help: the trait `PartialEq<(Foo, i32)>` is implemented for `Bar` | ||
note: type alias impl trait defined here | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:3:12 | ||
| | ||
LL | type Foo = impl PartialEq<(Foo, i32)>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 7 additions & 18 deletions
25
src/test/ui/impl-trait/recursive-type-alias-impl-trait-declaration-too-subtle.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,14 @@ | ||
error: unconstrained opaque type | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:4:16 | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:20:21 | ||
| | ||
LL | type Foo = impl PartialEq<(Foo, i32)>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | impl PartialEq<(Foo, i32)> for Bar { | ||
| ^^^ | ||
| | ||
= note: `Foo` must be used in combination with a concrete type within the same module | ||
|
||
error: unconstrained opaque type | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:17:16 | ||
note: type alias impl trait defined here | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:16:16 | ||
| | ||
LL | type Foo = impl PartialEq<(Foo, i32)>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `Foo` must be used in combination with a concrete type within the same module | ||
|
||
error[E0276]: impl has stricter requirements than trait | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:23:9 | ||
| | ||
LL | fn eq(&self, _other: &(Bar, i32)) -> bool { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `b::Bar: PartialEq<(b::Bar, i32)>` | ||
|
||
error: aborting due to 3 previous errors | ||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0276`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/test/ui/type-alias-impl-trait/issue-76202-trait-impl-for-tait.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/ui/type-alias-impl-trait/issue-84660-trait-impl-for-tait.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(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(); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/type-alias-impl-trait/issue-84660-trait-impl-for-tait.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/issue-84660-trait-impl-for-tait.rs:15:15 | ||
| | ||
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 | ||
|
41 changes: 41 additions & 0 deletions
41
src/test/ui/type-alias-impl-trait/issue-84660-unsoundness.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Another example from issue #84660, this time weaponized as a safe transmute: an opaque type in an | ||
// impl header being accepted was used to create unsoundness. | ||
|
||
#![feature(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); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/type-alias-impl-trait/issue-84660-unsoundness.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/issue-84660-unsoundness.rs:16:21 | ||
| | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
src/test/ui/type-alias-impl-trait/nested-tait-inference3.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
error: unconstrained opaque type | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/nested-tait-inference3.rs:10:10 | ||
| | ||
LL | impl Foo<FooX> for () { } | ||
| ^^^^ | ||
| | ||
note: type alias impl trait defined here | ||
--> $DIR/nested-tait-inference3.rs:6:13 | ||
| | ||
LL | type FooX = impl Debug; | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `FooX` must be used in combination with a concrete type within the same module | ||
|
||
error: aborting due to previous error | ||
|