-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix coherence checking for impl trait in type aliases
Fixes #63677 RFC #2071 (impl-trait-existential-types) does not explicitly state how impl trait type alises should interact with coherence. However, there's only one choice which makes sense - coherence should look at the underlying type (i.e. the 'defining' type of the impl trait) of the type alias, just like we do for non-impl-trait type aliases. Specifically, impl trait type alises which resolve to a local type should be treated like a local type with respect to coherence (e.g. impl trait type aliases which resolve to a forieign type should be treated as a foreign type, and those that resolve to a local type should be treated as a local type). Since neither inherent impls nor direct trait impl (i.e. `impl MyType` or `impl MyTrait for MyType`) are allowd for type aliases, this usually does not come up. Before we ever attempt to do coherence checking, we will have errored out if an impl trait type alias was used directly in an 'impl' clause. However, during trait selection, we sometimes need to prove bounds like 'T: Sized' for some type 'T'. If 'T' is an impl trait type alias, this requires to know the coherence behavior for impl trait type aliases when we perform coherence checking. Note: Since determining the underlying type of an impl trait type alias requires us to perform body type checking, this commit causes us to type check some bodies easlier than we otherwise would have. However, since this is done through a query, this shouldn't cause any problems For completeness, I've added an additional test of the coherence-related behavior of impl trait type aliases.
- Loading branch information
Showing
6 changed files
with
63 additions
and
6 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
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 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 | ||
#![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 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 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() {} |