-
Notifications
You must be signed in to change notification settings - Fork 476
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
Add the #[diagnostic]
attribute namespace and the #[diagnostic::on_unimplemented]
feature to the reference
#1449
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81fe01a
Add the `#[diagnostic]` attribute namespace and the
weiznich 5baf87c
Apply suggestions from code review
weiznich 99b19d9
Apply more review suggestions manually
weiznich 5e29b01
Various fixes and editing.
ehuss 52874b8
Update on_unimplemented for format string changes.
ehuss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -301,6 +301,72 @@ When used on a function in a trait implementation, the attribute does nothing. | |||||||||||
> let _ = five(); | ||||||||||||
> ``` | ||||||||||||
|
||||||||||||
## The `diagnostic` tool attribute namespace | ||||||||||||
|
||||||||||||
The `#[diagnostic]` attribute namespace is a home for attributes to influence compile-time error messages. | ||||||||||||
The hints provided by these attributes are not guaranteed to be used. | ||||||||||||
Unknown attributes in this namespace are accepted, though they may emit warnings for unused attributes. | ||||||||||||
Additionally, invalid inputs to known attributes will typically be a warning (see the attribute definitions for details). | ||||||||||||
This is meant to allow adding or discarding attributes and changing inputs in the future to allow changes without the need to keep the non-meaningful attributes or options working. | ||||||||||||
|
||||||||||||
### The `diagnostic::on_unimplemented` attribute | ||||||||||||
|
||||||||||||
The `#[diagnostic::on_unimplemented]` attribute is a hint to the compiler to supplement the error message that would normally be generated in scenarios where a trait is required but not implemented on a type. | ||||||||||||
The attribute should be placed on a [trait declaration], though it is not an error to be located in other positions. | ||||||||||||
The attribute uses the [_MetaListNameValueStr_] syntax to specify its inputs, though any malformed input to the attribute is not considered as an error to provide both forwards and backwards compatibility. | ||||||||||||
The following keys have the given meaning: | ||||||||||||
|
||||||||||||
* `message` — The text for the top level error message. | ||||||||||||
* `label` — The text for the label shown inline in the broken code in the error message. | ||||||||||||
* `note` — Provides additional notes. | ||||||||||||
|
||||||||||||
The `note` option can appear several times, which results in several note messages being emitted. | ||||||||||||
If any of the other options appears several times the first occurrence of the relevant option specifies the actually used value. | ||||||||||||
Any other occurrence generates an lint warning. | ||||||||||||
For any other non-existing option a lint-warning is generated. | ||||||||||||
|
||||||||||||
All three options accept a string as an argument. | ||||||||||||
The text in the string may contain the following format parameters which provide substitutions in the generated message: | ||||||||||||
|
||||||||||||
* `{Self}` — The name of the type implementing the trait. | ||||||||||||
* `{` *GenericParameterName* `}` — The name of the generic argument's type for the given generic parameter. | ||||||||||||
|
||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sound reasonable for me and that mirrors the actual implementation after the fix implemented in rust-lang/rust#122402 |
||||||||||||
Any other format parameter will generate a warning, but will otherwise be included in the string as-is. | ||||||||||||
|
||||||||||||
In this example: | ||||||||||||
|
||||||||||||
```rust,compile_fail,E0277 | ||||||||||||
#[diagnostic::on_unimplemented( | ||||||||||||
message = "My Message for `ImportantTrait<{A}>` implemented for `{Self}`", | ||||||||||||
label = "My Label", | ||||||||||||
note = "Note 1", | ||||||||||||
note = "Note 2" | ||||||||||||
)] | ||||||||||||
trait ImportantTrait<A> {} | ||||||||||||
|
||||||||||||
fn use_my_trait(_: impl ImportantTrait<i32>) {} | ||||||||||||
|
||||||||||||
fn main() { | ||||||||||||
use_my_trait(String::new()); | ||||||||||||
} | ||||||||||||
``` | ||||||||||||
|
||||||||||||
the compiler may generate an error message which looks like this: | ||||||||||||
|
||||||||||||
```text | ||||||||||||
error[E0277]: My Message for `ImportantTrait<i32>` implemented for `String` | ||||||||||||
--> src/main.rs:14:18 | ||||||||||||
| | ||||||||||||
14 | use_my_trait(String::new()); | ||||||||||||
| ------------ ^^^^^^^^^^^^^ My Label | ||||||||||||
| | | ||||||||||||
| required by a bound introduced by this call | ||||||||||||
| | ||||||||||||
= help: the trait `ImportantTrait<i32>` is not implemented for `String` | ||||||||||||
= note: Note 1 | ||||||||||||
= note: Note 2 | ||||||||||||
``` | ||||||||||||
|
||||||||||||
[Clippy]: https://github.com/rust-lang/rust-clippy | ||||||||||||
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax | ||||||||||||
[_MetaListPaths_]: ../attributes.md#meta-item-attribute-syntax | ||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If this is the actual intention here, then I think an edit like the following would make sense:
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.
That sound reasonable for me and that mirrors the actual implementation after the fix implemented in rust-lang/rust#122402