-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Better error message when trying to write default impls #49372
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 when trying to write default impls #49372
Conversation
Previously, if you tried to write this (using the specialization feature flag): default impl PartialEq<MyType> { ... } The compiler would give you the mysterious warning "inherent impls cannot be default". What it really means is that you're trying to write an impl for a Structure or *Trait Object*, and that cannot be "default". However, one of the ways to encounter this error (as shown by the above example) is when you forget to write "for MyType". This PR adds a help message that reads "maybe missing a `for` keyword?" This is useful, actionable advice that will help any user identify their mistake, and doesn't get in the way or mislead any user that really meant to use the "default" keyword for this weird purpose. In particular, this help message will be useful for any users who don't know the "inherent impl" terminology, and/or users who forget that inherent impls CAN be written for traits (they apply to the trait objects). Both of these are somewhat confusing, seldom- used concepts; a one-line error message without any error number for longer explanation is NOT the place to introduce these ideas.
Highfive failed to pick a reviewer for this PR. r? @estebank |
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.
I would prefer a suggestion if it could be provided, with text close to
help: if you meant to write an `impl` for `PartialEq`, use the `for` keyword: `default impl for PartialEq`
(I hope I'm doing that notation right)
Only members of the org can CC teams.
CC @rust-lang/docs
I'm gonna be offline for two weeks. Apologies in advance for the unresponsiveness.
self.err_handler().span_err(item.span, "inherent impls cannot be default"); | ||
self.err_handler() | ||
.struct_span_err(item.span, "inherent impls cannot be default") | ||
.help("maybe a missing `for` keyword?"); |
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.
This is creating a DiagnosticBuilder
but never has .emit()
called on it, which causes an Internal Compiler Error in test compile-fail/specialization/defaultimpl/validation.rs
.
Assign the result of the struct_span_err
to a mutable variable err
and then err.help(...)
and err.emit()
.
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.
Oh, okay. Sorry, I'm not very familiar with internals. And there's no rush for feedback, thanks for the warning.
And another way is to actually write |
True... I don't know that we can do that here, though. Isn't this AST validation, before we've resolved trait names and the like? Also, I don't know how to do that; I'm not very familiar with the rustc internals. Do you happen to know how to do that, or someplace I can start looking? |
Rather than trying to be helpful here and phrasing your suggestion as a question, I’d recommend using a |
Ping from triage @Phlosioneer! The reviewer left some comments, could you address them? |
Ping from triage @nagisa! The author addressed your concerns, could you review this again? |
@bors r+ |
📌 Commit c61e641 has been approved by |
…e, r=nagisa Better error message when trying to write default impls Previously, if you tried to write this (using the specialization feature flag): default impl PartialEq<MyType> { ... } The compiler would give you the mysterious warning "inherent impls cannot be default". What it really means is that you're trying to write an impl for a Structure or *Trait Object*, and that cannot be "default". However, one of the ways to encounter this error (as shown by the above example) is when you forget to write "for MyType". This PR adds a help message that reads "maybe missing a `for` keyword?" This is useful, actionable advice that will help any user identify their mistake, and doesn't get in the way or mislead any user that really meant to use the "default" keyword for this weird purpose. In particular, this help message will be useful for any users who don't know the "inherent impl" terminology, and/or users who forget that inherent impls CAN be written for traits (they apply to the trait objects). Both of these are somewhat confusing, seldom- used concepts; a one-line error message without any error number for longer explanation is NOT the place to introduce these ideas. I wasn't quite sure what grammar / wording to use. I'm open to suggestions. CC @rust-lang/docs (I hope I'm doing that notation right) (Apparently not. :( )
☀️ Test successful - status-appveyor, status-travis |
Previously, if you tried to write this (using the specialization
feature flag):
default impl PartialEq {
...
}
The compiler would give you the mysterious warning "inherent impls
cannot be default". What it really means is that you're trying to
write an impl for a Structure or Trait Object, and that cannot
be "default". However, one of the ways to encounter this error
(as shown by the above example) is when you forget to write "for
MyType".
This PR adds a help message that reads "maybe missing a
for
keyword?" This is useful, actionable advice that will help any user
identify their mistake, and doesn't get in the way or mislead any
user that really meant to use the "default" keyword for this weird
purpose. In particular, this help message will be useful for any
users who don't know the "inherent impl" terminology, and/or users
who forget that inherent impls CAN be written for traits (they apply
to the trait objects). Both of these are somewhat confusing, seldom-
used concepts; a one-line error message without any error number for
longer explanation is NOT the place to introduce these ideas.
I wasn't quite sure what grammar / wording to use. I'm open to suggestions. CC @rust-lang/docs (I hope I'm doing that notation right)
(Apparently not. :( )