-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
Better error message for attempting to use an associated type as a TypeParamBound #91180
Comments
@rustbot claim |
I looked through the code for a bit, and it's not going to be as trivial a fix as I initially thought. I don't yet know enough to say whether it will require some larger refactor, but here's what I've learned. The error is occurring at the resolve stage. When I think this is just due to the fact that The good news is that this isn't some baked-in limitation of the language itself. The following example compiles, demonstrating that the language allows a trait to refer to its own associated types "before" they have been defined: trait A: Iterator<Item = Self::X> {
type X;
} However, the only reason that works is because rust/compiler/rustc_resolve/src/late.rs Line 1991 in 7f5d3ff
And sadly, it looks like resolution cannot be deferred for trait paths: rust/compiler/rustc_resolve/src/late.rs Lines 219 to 228 in 7f5d3ff
So the only way forward that I can think of is to make the Would love to hear input from anybody that has any! I'm still relatively new to rustc contribution. |
Hey Ben, thanks for looking into this! I think that such a lookahead / deferral might not be necessary. The way I understand it is that there is currently no way that trait A: Self::X { .. } will ever compile. Because While other keywords can be used as identifiers by using raw identifiers (e.g. So I think when a trait is expected the resolver should be able to error out just on the basis that |
This is a good point, and I agree that it's true. However, it doesn't make the problem of deciding what to say in the error message any easier. Suppose we make the compiler use the following logic: if the inherited trait path starts with trait A: Self::DoesNotExist {
type X;
} Well, maybe isn't the wrong error message, but it seems less good than I'm now starting to doubt if the proposed improvement is enough of an improvement to justify the work that it would take to make it happen. As it stands, the diagnostic is mostly truthful, even if only by accident (there is indeed no trait named Idk. I've unassigned myself from the issue to leave it up for somebody else should they want to take it on. |
Yes I think that:
is a bad error message because there can be no traits in
(I updated the issue description accordingly) |
Given the following code:
The current output is:
Ideally the output should look like:
The text was updated successfully, but these errors were encountered: