-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[RFC] On_unimplemented_trait_use #3643
base: master
Are you sure you want to change the base?
Conversation
Unresolved question: If the struct declares this diagnostic attribute for trait Foo but also implements Foo, what should be done? |
Do nothing? The diagnostic is only consulted "on unimplemented", so it is no-op "on implemented". |
For example: | ||
```rust | ||
#[diagnostic::on_unimplemented_trait_use( | ||
trait = Display, |
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.
what is the syntax when the trait is generic? e.g. std::ops::Index<T>
for str
.
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.
Tbh, I'm not sure. Let me add it into the Unresolved questions
If I understand this proposal correctly, this attribute is only intended to be used on types not traits. So it would be a logical error to both explain why a trait isn't implemented but also implement said trait no? |
@cyqsimon When the type is generic e.g. |
Yikes that is annoying. Would it be a good idea to emit a warning/error only when there exists a blanket implementation that is a superset of the unimplemented trait declared with this diagnostic item? So for example: #[diagnostic::on_unimplemented_trait_use(
trait = Bar,
where(T: Clone), // new syntax
...
)]
struct Foo<T>(T);
trait Bar {}
// this is fine because `Copy + Clone` is stricter than `Clone`
impl<T: Copy + Clone> Bar for Foo<T> {}
// but these are not
impl<T: Clone> Bar for Foo<T> {}
impl<T> Bar for Foo<T> {} |
Also what do you think about the |
Please note that the
|
Okay good to know, so no errors. Are we allowed to emit additional warnings though? |
@cyqsimon They can (in the same way
If you checked #3368 the proposed syntax was |
I would like to raise the same semi related note as in #3639: As far as I remember one of the main motivations for the specification of diagnostic attribute namespaces in #3368 That does not mean that there should be no discussion about new attributes, just that this likely doesn't need a full RFC. |
Summary
Add
[diagnostic::on_unimplemented_trait_use]
in#[diagnostic]
on structs that will influence error messages emitted by unsatisfied traits bounds.Motivation
The idea came about when I was trying to print out a PathBuf, there's a custom message that said:
And found out its hardcoded in trait
Display
It would be nice if this functionality is exposed to libraries as well, so that when the user tries to use an unimplemented trait (e.g. maybe Display isn't implemented because it's insufficient to clearly express intentions) the author can explain why via this diagnostic and offer a recommendation/alternative.
For example:
Unresolved questions