-
Notifications
You must be signed in to change notification settings - Fork 1.8k
internal: change generic parameter order #13335
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
Conversation
This commit "inverts" the order of generic parameters/arguments of an item and its parent. This is to fulfill chalk's expectation on the order of `Substitution` for generic associated types and it's one step forward for their support (hopefully). Although chalk doesn't put any constraint on the order of `Substitution` for other items, it feels natural to get everything aligned rather than special casing GATs. One complication is that `TyBuilder` now demands its users to pass in parent's `Substitution` upon construction unless it's obvious that the the item has no parent (e.g. an ADT never has parent). All users *should* already know the parent of the item in question, and without this, it cannot be easily reasoned about whether we're pushing the argument for the item or for its parent. Quick comparison of how this commit changes `Substitution`: ```rust trait Trait<TP, const CP: usize> { type Type<TC, const CC: usize> = (); fn f<TC, const CC: usize>() {} } ``` - before this commit: `[Self, TP, CP, TC, CC]` for each trait item - after this commit: `[TC, CC, Self, TP, CP]` for each trait item
79094e8
to
7556f74
Compare
@bors r+ |
☀️ Test successful - checks-actions |
This immediately broke metrics 😅 https://github.com/rust-lang/rust-analyzer/actions/runs/3174097813/jobs/5170473829 |
I should really remember to run analysis-stats before submitting big changes 😕 It seems I messed up generics for enum variants. It shouldn't take too long to fix but I'm fine with reverting this PR if it's too problematic. |
if you have the time and want to try tackling it today feel free to, otherwise I'd say lets revert it as I believe this to break a lot of things for people |
Yeah agreed. I'm going to make attempt but will let you know (either here or on zulip) if I can't seem to fix in the time frame. |
fix: treat enum variants as generic item on their own Fixup for #13335 It turns out I tried to merge two procedures into one utility function without noticing the incompatibility. This time I *did* run analysis-stats on the four crates and confirmed it doesn't crash and this patch doesn't cause regression.
fix: use `BoundVar`s from current generic scope Fixup for #13335, addresses #13339 (comment) Before the change in generic parameter order, `BoundVar`s for trait reference didn't change whether you are in an impl's scope or in an associated item's scope. Now that item's generic params come before its parent's, we need to shift their indices when we are in an associated item's scope.
fix: use `BoundVar`s from current generic scope Fixup for #13335, addresses #13339 (comment) Before the change in generic parameter order, `BoundVar`s for trait reference didn't change whether you are in an impl's scope or in an associated item's scope. Now that item's generic params come before its parent's, we need to shift their indices when we are in an associated item's scope.
fix: use `BoundVar`s from current generic scope Fixup for #13335, addresses #13339 (comment) Before the change in generic parameter order, `BoundVar`s for trait reference didn't change whether you are in an impl's scope or in an associated item's scope. Now that item's generic params come before its parent's, we need to shift their indices when we are in an associated item's scope.
fix: use `BoundVar`s from current generic scope Fixup for #13335, addresses #13339 (comment) Before the change in generic parameter order, `BoundVar`s for trait reference didn't change whether you are in an impl's scope or in an associated item's scope. Now that item's generic params come before its parent's, we need to shift their indices when we are in an associated item's scope.
tl;dr: This PR changes the
Substitution
for trait items and methods like so:[Self, TP, CP, TC, CC]
for each trait item,[TP, CP, TC, CC]
forS::f
[TC, CC, Self, TP, CP]
for each trait item,[TC, CC, TP, CP]
forS::f
This PR "inverts" the generic parameters/arguments of an item and its parent. This is to fulfill chalk's expectation on the order of generic arguments in
Substitution
s for generic associated types and it's one step forward for GATs support (hopefully). Although chalk doesn't put any constraint for other items, it feels more natural to get everything aligned than special casing GATs.One complication is that
TyBuilder
now demands its users to pass in parent'sSubstitution
upon construction unless it's obvious that the the item has no parent (e.g. an ADT never has parent). All users should already know the parent of the item in question, and without this, it cannot be easily reasoned about whether we're pushing the argument for the item or for its parent.Some additional notes:
f8f5a5e: This isn't related to the change, but I felt it's nicer.
78977cd: There's one major change here other than the generic param order: Default arguments are now bound by the same
Binder
as the item in question rather than aBinder
limited to parameters they can refer to (i.e. arguments that syntactically appear before them). Now that the order of generic parameters is changed, it would be somewhat complicated to make suchBinder
s as before, and the "full"Binder
s shouldn't be a problem because we already make sure that the default arguments don't refer to the generic arguments after them withfallback_bound_vars()
.7556f74: This is split from 4385d3d to make it easy to revert if it turns out that the GATs with const generics panic is actually not resolved with this PR. cc fix: Paper over GAT panic #11878 fix panic on GAT #11957