-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Collect relevant item bounds from trait clauses for nested rigid projections #120752
Conversation
r=me after fcp though after this PR, associated type bounds are always just a desugaring of otherwise writeable where-clauses on the trait, are they? |
So not exactly. We don't currently uplift associated types for GATs properly, e.g. trait Foo where Self::Assoc<'a>: Iterator<Item = &'a ()> { type Assoc<'a>; } IDK if we should. Currently that code even errors with E0582:
|
I feel like the following should compile: trait Foo where for<'a> Self::Assoc<'a>: Iterator<Item = &'a ()> { type Assoc<'a>; } |
Shouldn't be too hard, just need to map them to their early-bound indices.
IDK much about the motivation behind E0582. |
fa551a1
to
35d8667
Compare
This comment has been minimized.
This comment has been minimized.
35d8667
to
98fcb15
Compare
Ok this is ready for a review at least :> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a test where a GAT is inside of another projection, for<'a, 'b> <<Self::Gat<'a> as Bound>::OtherGat<'b>
or sth
If I am not wrong, this PR ends up causing associated type bounds to be a simple desugaring. This seems valuable as they can be ignored when explaining the language. I also think that uplifting gat where-bounds to item bounds is very clearly desirable and doing so for nested also seems like an improvement, even if it likely only infrequently used. With this @rfcbot fcp merge |
Team member @lcnr has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
It's a simple desugaring in where clauses, APIT, and in associated type's item bounds. The only place it remains not a simple deusgaring is for opaques, i.e. |
What's the meaming of |
Yes; this was what #120584 achieved by always lowering associated type bounds as bounds and never as nested RPITs.
Yep, since we're not introducing a new RPIT here, we don't need to care about captures (especially higher-ranked ones). See the test: rust/tests/ui/associated-type-bounds/higher-ranked.rs Lines 1 to 17 in 96561a8
Specifically, |
I think it's somewhat obvious, but, uh, maybe mention in the main post that this is an insta-stable change 😆 So... this change can't break any code, because either it was erroring before due to the bound not being satisfied, or it already had the bound explicitly. There is no situation where the new implied bound in param env is now preferred over looking at global information? |
🤔 it only affects rigid associated types, and at this point impls don't really apply to it. I guess it can impact inference for I don't expect this to ever happen in practice, but it would be theoretically breaking: 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);
}
fn main() {} please add this as a test 😁 |
thanks. I imagined something like that but was unable to actually build the test 😆 |
OK, I've finally quit procrastinating rebasing this. @rustbot ready |
@bors r+ rollup=never |
…, 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 rust-lang#72788 (which added a query for the predicates we consider for `ProjectionCandidate`s), and then finally item bounds and predicates were split in rust-lang#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 rust-lang#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.
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
517208f
to
c591475
Compare
Needed reformat after the rustfmt bump. @bors r=lcnr |
☀️ Test successful - checks-actions |
Finished benchmarking commit (9e394f5): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (primary 2.6%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 772.322s -> 773.661s (0.17%) |
Rust currently considers trait where-clauses that bound the trait's own associated types to act like an item bound:
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 forProjectionCandidate
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:
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:
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 leavefor<'a> Self::Assoc<'a, 'a>: Trait
andSelf::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.