-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fix coherence checking for impl trait in type aliases #63934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
// Tests that type alias impls traits do not leak auto-traits for | ||
// the purposes of coherence checking | ||
#![feature(type_alias_impl_trait)] | ||
|
||
trait OpaqueTrait { } | ||
impl<T> OpaqueTrait for T { } | ||
type OpaqueType = impl OpaqueTrait; | ||
fn mk_opaque() -> OpaqueType { () } | ||
|
||
#[derive(Debug)] | ||
struct D<T>(T); | ||
|
||
trait AnotherTrait { } | ||
impl<T: Send> AnotherTrait for T { } | ||
|
||
// This is in error, because we cannot assume that `OpaqueType: !Send`. | ||
// (We treat opaque types as "foreign types" that could grow more impls | ||
// in the future.) | ||
impl AnotherTrait for D<OpaqueType> { | ||
//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `D<OpaqueType>` | ||
} | ||
|
||
fn main() {} |
This file contains hidden or 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,12 @@ | ||
error[E0119]: conflicting implementations of trait `AnotherTrait` for type `D<OpaqueType>`: | ||
--> $DIR/auto-trait.rs:19:1 | ||
| | ||
LL | impl<T: Send> AnotherTrait for T { } | ||
| -------------------------------- first implementation here | ||
... | ||
LL | impl AnotherTrait for D<OpaqueType> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `D<OpaqueType>` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0119`. |
This file contains hidden or 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,22 @@ | ||
// Tests that we cannot assume that an opaque type does *not* implement some | ||
// other trait | ||
#![feature(type_alias_impl_trait)] | ||
|
||
trait OpaqueTrait { } | ||
impl<T> OpaqueTrait for T { } | ||
type OpaqueType = impl OpaqueTrait; | ||
fn mk_opaque() -> OpaqueType { () } | ||
|
||
#[derive(Debug)] | ||
struct D<T>(T); | ||
|
||
trait AnotherTrait { } | ||
impl<T: std::fmt::Debug> AnotherTrait for T { } | ||
|
||
|
||
// This is in error, because we cannot assume that `OpaqueType: !Debug` | ||
impl AnotherTrait for D<OpaqueType> { | ||
//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `D<OpaqueType>` | ||
} | ||
|
||
fn main() {} |
This file contains hidden or 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[E0119]: conflicting implementations of trait `AnotherTrait` for type `D<OpaqueType>`: | ||
--> $DIR/negative-reasoning.rs:18:1 | ||
| | ||
LL | impl<T: std::fmt::Debug> AnotherTrait for T { } | ||
| ------------------------------------------- first implementation here | ||
... | ||
LL | impl AnotherTrait for D<OpaqueType> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `D<OpaqueType>` | ||
| | ||
= note: upstream crates may add a new impl of trait `std::fmt::Debug` for type `OpaqueType` in future versions | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0119`. |
This file contains hidden or 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,2 @@ | ||
pub trait ForeignTrait {} | ||
pub struct ForeignType<T>(pub T); |
This file contains hidden or 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,17 @@ | ||
// aux-build:foreign-crate.rs | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![feature(type_alias_impl_trait)] | ||
|
||
extern crate foreign_crate; | ||
|
||
trait LocalTrait {} | ||
impl<T> LocalTrait for foreign_crate::ForeignType<T> {} | ||
|
||
type AliasOfForeignType<T> = impl LocalTrait; | ||
fn use_alias<T>(val: T) -> AliasOfForeignType<T> { | ||
foreign_crate::ForeignType(val) | ||
} | ||
|
||
impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {} | ||
//~^ ERROR the type parameter `T` is not constrained by the impl trait, self type, or predicates | ||
|
||
fn main() {} |
This file contains hidden or 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,9 @@ | ||
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates | ||
--> $DIR/coherence.rs:14:6 | ||
| | ||
LL | impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {} | ||
| ^ unconstrained type parameter | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0207`. |
21 changes: 21 additions & 0 deletions
21
src/test/ui/type-alias-impl-trait/issue-63677-type-alias-coherence.rs
This file contains hidden or 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,21 @@ | ||
// check-pass | ||
// Regression test for issue #63677 - ensure that | ||
// coherence checking can properly handle 'impl trait' | ||
// in type aliases | ||
#![feature(type_alias_impl_trait)] | ||
|
||
pub trait Trait {} | ||
pub struct S1<T>(T); | ||
pub struct S2<T>(T); | ||
|
||
pub type T1 = impl Trait; | ||
pub type T2 = S1<T1>; | ||
pub type T3 = S2<T2>; | ||
|
||
impl<T> Trait for S1<T> {} | ||
impl<T: Trait> S2<T> {} | ||
impl T3 {} | ||
|
||
pub fn use_t1() -> T1 { S1(()) } | ||
|
||
fn main() {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.