-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #120752 - compiler-errors:more-relevant-bounds, r=lcnr
Collect relevant item bounds from trait clauses for nested rigid projections Rust currently considers trait where-clauses that bound the trait's *own* associated types to act like an item bound: ```rust trait Foo where Self::Assoc: Bar { type Assoc; } // acts as if: trait Foo { type Assoc: Bar; } ``` ### Background This behavior has existed since essentially forever (i.e. before Rust 1.0), since we originally started out by literally looking at the where clauses written on the trait when assembling `SelectionCandidate::ProjectionCandidate` for projections. However, looking at the predicates of the associated type themselves was not sound, since it was unclear which predicates were *assumed* and which predicates were *implied*, and therefore this was reworked in #72788 (which added a query for the predicates we consider for `ProjectionCandidate`s), and then finally item bounds and predicates were split in #73905. ### Problem 1: GATs don't uplift bounds correctly All the while, we've still had logic to uplift associated type bounds from a trait's where clauses. However, with the introduction of GATs, this logic was never really generalized correctly for them, since we were using simple equality to test if the self type of a trait where clause is a projection. This leads to shortcomings, such as: ```rust trait Foo where for<'a> Self::Gat<'a>: Debug, { type Gat<'a>; } fn test<T: Foo>(x: T::Gat<'static>) { //~^ ERROR `<T as Foo>::Gat<'a>` doesn't implement `Debug` println!("{:?}", x); } ``` ### Problem 2: Nested associated type bounds are not uplifted We also don't attempt to uplift bounds on nested associated types, something that we couldn't really support until #120584. This can be demonstrated best with an example: ```rust trait A where Self::Assoc: B, where <Self::Assoc as B>::Assoc2: C, { type Assoc; // <~ The compiler *should* treat this like it has an item bound `B<Assoc2: C>`. } trait B { type Assoc2; } trait C {} fn is_c<T: C>() {} fn test<T: A>() { is_c::<<Self::Assoc as B>::Assoc2>(); //~^ ERROR the trait bound `<<T as A>::Assoc as B>::Assoc2: C` is not satisfied } ``` Why does this matter? Well, generalizing this behavior bridges a gap between the associated type bounds (ATB) feature and trait where clauses. Currently, all bounds that can be stably written on associated types can also be expressed as where clauses on traits; however, with the stabilization of ATB, there are now bounds that can't be desugared in the same way. This fixes that. ## How does this PR fix things? First, when scraping item bounds from the trait's where clauses, given a trait predicate, we'll loop of the self type of the predicate as long as it's a projection. If we find a projection whose trait ref matches, we'll uplift the bound. This allows us to uplift, for example `<Self as Trait>::Assoc: Bound` (pre-existing), but also `<<Self as Trait>::Assoc as Iterator>::Item: Bound` (new). If that projection is a GAT, we will check if all of the GAT's *own* args are all unique late-bound vars. We then map the late-bound vars to early-bound vars from the GAT -- this allows us to uplift `for<'a, 'b> Self::Assoc<'a, 'b>: Trait` into an item bound, but we will leave `for<'a> Self::Assoc<'a, 'a>: Trait` and `Self::Assoc<'static, 'static>: Trait` alone. ### Okay, but does this *really* matter? I consider this to be an improvement of the status quo because it makes GATs a bit less magical, and makes rigid projections a bit more expressive.
- Loading branch information
Showing
7 changed files
with
380 additions
and
10 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
28 changes: 28 additions & 0 deletions
28
tests/ui/associated-type-bounds/nested-associated-type-bound-incompleteness.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,28 @@ | ||
// Demonstrates a mostly-theoretical inference guidance now that we turn the where | ||
// clause on `Trait` into an item bound, given that we prefer item bounds somewhat | ||
// greedily in trait selection. | ||
|
||
trait Bound<T> {} | ||
impl<T, U> Bound<T> for U {} | ||
|
||
trait Trait | ||
where | ||
<<Self as Trait>::Assoc as Other>::Assoc: Bound<u32>, | ||
{ | ||
type Assoc: Other; | ||
} | ||
|
||
trait Other { | ||
type Assoc; | ||
} | ||
|
||
fn impls_trait<T: Bound<U>, U>() -> Vec<U> { vec![] } | ||
|
||
fn foo<T: Trait>() { | ||
let mut vec_u = impls_trait::<<<T as Trait>::Assoc as Other>::Assoc, _>(); | ||
vec_u.sort(); | ||
drop::<Vec<u8>>(vec_u); | ||
//~^ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
16 changes: 16 additions & 0 deletions
16
tests/ui/associated-type-bounds/nested-associated-type-bound-incompleteness.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,16 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/nested-associated-type-bound-incompleteness.rs:24:21 | ||
| | ||
LL | drop::<Vec<u8>>(vec_u); | ||
| --------------- ^^^^^ expected `Vec<u8>`, found `Vec<u32>` | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected struct `Vec<u8>` | ||
found struct `Vec<u32>` | ||
note: function defined here | ||
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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,31 @@ | ||
//@ check-pass | ||
|
||
trait Trait | ||
where | ||
for<'a> Self::Gat<'a>: OtherTrait, | ||
for<'a, 'b, 'c> <Self::Gat<'a> as OtherTrait>::OtherGat<'b>: HigherRanked<'c>, | ||
{ | ||
type Gat<'a>; | ||
} | ||
|
||
trait OtherTrait { | ||
type OtherGat<'b>; | ||
} | ||
|
||
trait HigherRanked<'c> {} | ||
|
||
fn lower_ranked<T: for<'b, 'c> OtherTrait<OtherGat<'b>: HigherRanked<'c>>>() {} | ||
|
||
fn higher_ranked<T: Trait>() | ||
where | ||
for<'a> T::Gat<'a>: OtherTrait, | ||
for<'a, 'b, 'c> <T::Gat<'a> as OtherTrait>::OtherGat<'b>: HigherRanked<'c>, | ||
{ | ||
} | ||
|
||
fn test<T: Trait>() { | ||
lower_ranked::<T::Gat<'_>>(); | ||
higher_ranked::<T>(); | ||
} | ||
|
||
fn main() {} |
28 changes: 28 additions & 0 deletions
28
tests/ui/associated-types/imply-relevant-nested-item-bounds-2.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,28 @@ | ||
//@ check-pass | ||
//@ revisions: current next | ||
//@[next] compile-flags: -Znext-solver | ||
|
||
trait Trait | ||
where | ||
Self::Assoc: Clone, | ||
{ | ||
type Assoc; | ||
} | ||
|
||
fn foo<T: Trait>(x: &T::Assoc) -> T::Assoc { | ||
x.clone() | ||
} | ||
|
||
trait Trait2 | ||
where | ||
Self::Assoc: Iterator, | ||
<Self::Assoc as Iterator>::Item: Clone, | ||
{ | ||
type Assoc; | ||
} | ||
|
||
fn foo2<T: Trait2>(x: &<T::Assoc as Iterator>::Item) -> <T::Assoc as Iterator>::Item { | ||
x.clone() | ||
} | ||
|
||
fn main() {} |
19 changes: 19 additions & 0 deletions
19
tests/ui/associated-types/imply-relevant-nested-item-bounds-for-gat.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,19 @@ | ||
//@ check-pass | ||
|
||
// Test that `for<'a> Self::Gat<'a>: Debug` is implied in the definition of `Foo`, | ||
// just as it would be if it weren't a GAT but just a regular associated type. | ||
|
||
use std::fmt::Debug; | ||
|
||
trait Foo | ||
where | ||
for<'a> Self::Gat<'a>: Debug, | ||
{ | ||
type Gat<'a>; | ||
} | ||
|
||
fn test<T: Foo>(x: T::Gat<'static>) { | ||
println!("{:?}", x); | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.