-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Allow qualified paths in struct construction (both expressions and patterns) #80080
Conversation
r? @oli-obk (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This lgtm. At this point we should probably do a cleanup (as a follow-up) by introducing some sort of |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I have reopened #61933 which was originally fixed but is reintroduced with this PR. Even if this is technically just a bug fix, since this is an insta-stable user-visible language change, I do believe the language team needs to sign off on it. This PR allows the use of qualified paths in struct patterns and expressions, just like we already allow it for tuple struct patterns and expressions. As an example: let <Bar as A>::Assoc { br: _br } = <Bar as A>::Assoc { br: 2 }; is allowed after this PR, when it wasn't allowed before. |
@nikomatsakis I believe you were going to comment on next steps for this? |
I think it was me :) We discussed this in the last few lang team meetings, and there was general agreement that this seems like the right path. However, this PR is rather loose on exact description of the change made, and it would be good to write that up (perhaps as the PR description). Specifically, a description of what was allowed before, and what is allowed now; if there's some reason to believe this case was intentionally excluded it would be good to reference the PR that did so and it's rationale as well. We were also interested in knowing if there was a specific driver for this change being made (beyond fixing the mentioned issue). |
@Mark-Simulacrum thanks! The question at hand is should qualified paths type aliases be useable in patterns and expressions for both struct-structs and tuple-structs. This question can be further broken down to: should they be parseable but not pass analysis, not even parseable, or fully useable? For example, should this code which does not compile in master be made to compile (or at the very least parse)? fn main() {
let <WithStructStruct as Trait>::AssociatedType { a } = <WithStructStruct as Trait>::AssociatedType { a: 0 };
let <WithTupleStruct as Trait>::AssociatedType(a) = <WithTupleStruct as Trait>::AssociatedType(0);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This is the only thing that currently parses
let s = StructStruct { a : 0 };
match s {
<WithStructStruct as Trait>::AssociatedType { a } => a,
};
let s = TupleStruct(0);
match s {
<WithTupleStruct as Trait>::AssociatedType(a) => a,
};
}
struct StructStruct {
a: usize
}
struct TupleStruct(usize);
trait Trait {
type AssociatedType;
}
struct WithStructStruct;
impl Trait for WithStructStruct {
type AssociatedType = StructStruct;
}
struct WithTupleStruct;
impl Trait for WithTupleStruct {
type AssociatedType = TupleStruct;
} Currently in master only tuple structs in expression position parse - though they are not allowed past analysis (rustc_resolve prevents them from being used). Everything else fails to even parse: let _ = <WithTupleStruct as Trait>::AssociatedType(0);
// ^ parses but does not compile It is important to note that the use of struct-structs (but not tuple-structs) with qualified paths is allowed if that struct-struct is referred to behind an alias. For example, the following compiles already: type Alias = <WithStructStruct as Trait>::AssociatedType;
let alias = Alias { a: 0 } ;
match alias {
Alias { a } => a,
}; This PR allows the full use of qualified paths in struct-struct patterns and expressions and the parsing of tuple-structs in patterns. Tuple-structs are still not allowed past analysis in both expression and pattern position but this seems wrong (and was an oversight) and should be made consistent with whatever direction we decide to go with struct-structs. ReasoningThe reason I decided to work on this was due to the need for this in generated code where the generated code knew the trait in question but not the specific associated type. Because of the nature of the associated type it was possible to construct that type but not to name it. To work around this, the alias trick above was used. This seems like a strange inconsistency that should be resolved. Although it's not often useful, being able to refer to types through qualified paths can be useful in limited circumstances and it does seem natural and unsurprising. |
To clarify, is this change made in this PR? I think what you have written presents good rationale but it would be helpful to have a more bullet point summary, e.g., something like this, though the following is my hypothesis and not from reading this PR or anything: Before this PR:
After this PR:
It would also be good to figure out why this restriction was added/exists - is this just by accident? Maybe @petrochenkov can provide some insight here. |
Sorry for the confusion! Let me give it another try: Before PR
After this PR
Work that remains to be doneAs the PR currently stands there is still an inconsistency as struct-struct and tuple-structs are treated differently. This needs to be changed. I am holding off on fixing this inconsistency until we decide on how to fix the inconsistency. I believe all these usages should at least parse so at the very least we can provide better error messages. What needs to be decided is if we should allow the user to actually use the qualified path syntax in the expression and/or pattern position. Therefore, this PR needs to be updated to either:
It was my understanding that this restriction is purely a historical accident as usage of this syntax is likely not very common as @petrochenkov points out here. |
This comment has been minimized.
This comment has been minimized.
merge conflicts @JohnCSimon label: +S-waiting-on-author -S-waiting-on-author |
@bors delegate+ please rebase and then use |
✌️ @rylev can now approve this pull request |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
r=me with commits squashed. |
…pattern position.
@bors r+ |
📌 Commit 6936349 has been approved by |
☀️ Test successful - checks-actions |
Where's the tracking issue for this? it just points to this pull request... |
@programmerjake there was no tracking issue for this. I will make one, and change where the feature gate points to. |
…, r=nagisa Change linked tracking issue for more_qualified_paths This updates the linked tracking issue for the `more_qualified_paths` feature from the implementation PR rust-lang#80080 to an actual tracking issue rust-lang#86935.
Fixes #79658