Skip to content

Commit 5569491

Browse files
authored
Merge pull request #1449 from weiznich/diagnostic_namespace
Add the `#[diagnostic]` attribute namespace and the `#[diagnostic::on_unimplemented]` feature to the reference
2 parents 1c03c9d + 52874b8 commit 5569491

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/attributes.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ struct S {
196196
pub fn f() {}
197197
```
198198

199-
> Note: `rustc` currently recognizes the tools "clippy" and "rustfmt".
199+
> Note: `rustc` currently recognizes the tools "clippy", "rustfmt" and "diagnostic".
200200
201201
## Built-in attributes index
202202

@@ -224,6 +224,8 @@ The following is an index of all built-in attributes.
224224
- [`allow`], [`warn`], [`deny`], [`forbid`] — Alters the default lint level.
225225
- [`deprecated`] — Generates deprecation notices.
226226
- [`must_use`] — Generates a lint for unused values.
227+
- [`diagnostic::on_unimplemented`] — Hints the compiler to emit a certain error
228+
message if a trait is not implemented.
227229
- ABI, linking, symbols, and FFI
228230
- [`link`] — Specifies a native library to link with an `extern` block.
229231
- [`link_name`] — Specifies the name of the symbol for functions or statics
@@ -352,3 +354,4 @@ The following is an index of all built-in attributes.
352354
[closure]: expressions/closure-expr.md
353355
[function pointer]: types/function-pointer.md
354356
[variadic functions]: items/external-blocks.html#variadic-functions
357+
[`diagnostic::on_unimplemented`]: attributes/diagnostics.md#the-diagnosticon_unimplemented-attribute

src/attributes/diagnostics.md

+70
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,76 @@ When used on a function in a trait implementation, the attribute does nothing.
301301
> let _ = five();
302302
> ```
303303
304+
## The `diagnostic` tool attribute namespace
305+
306+
The `#[diagnostic]` attribute namespace is a home for attributes to influence compile-time error messages.
307+
The hints provided by these attributes are not guaranteed to be used.
308+
Unknown attributes in this namespace are accepted, though they may emit warnings for unused attributes.
309+
Additionally, invalid inputs to known attributes will typically be a warning (see the attribute definitions for details).
310+
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.
311+
312+
### The `diagnostic::on_unimplemented` attribute
313+
314+
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.
315+
The attribute should be placed on a [trait declaration], though it is not an error to be located in other positions.
316+
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.
317+
The following keys have the given meaning:
318+
319+
* `message` — The text for the top level error message.
320+
* `label` — The text for the label shown inline in the broken code in the error message.
321+
* `note` — Provides additional notes.
322+
323+
The `note` option can appear several times, which results in several note messages being emitted.
324+
If any of the other options appears several times the first occurrence of the relevant option specifies the actually used value.
325+
Any other occurrence generates an lint warning.
326+
For any other non-existing option a lint-warning is generated.
327+
328+
All three options accept a string as an argument, interpreted using the same formatting as a [`std::fmt`] string.
329+
Format parameters with the given named parameter will be replaced with the following text:
330+
331+
* `{Self}` — The name of the type implementing the trait.
332+
* `{` *GenericParameterName* `}` — The name of the generic argument's type for the given generic parameter.
333+
334+
Any other format parameter will generate a warning, but will otherwise be included in the string as-is.
335+
336+
Invalid format strings may generate a warning, but are otherwise allowed, but may not display as intended.
337+
Format specifiers may generate a warning, but are otherwise ignored.
338+
339+
In this example:
340+
341+
```rust,compile_fail,E0277
342+
#[diagnostic::on_unimplemented(
343+
message = "My Message for `ImportantTrait<{A}>` implemented for `{Self}`",
344+
label = "My Label",
345+
note = "Note 1",
346+
note = "Note 2"
347+
)]
348+
trait ImportantTrait<A> {}
349+
350+
fn use_my_trait(_: impl ImportantTrait<i32>) {}
351+
352+
fn main() {
353+
use_my_trait(String::new());
354+
}
355+
```
356+
357+
the compiler may generate an error message which looks like this:
358+
359+
```text
360+
error[E0277]: My Message for `ImportantTrait<i32>` implemented for `String`
361+
--> src/main.rs:14:18
362+
|
363+
14 | use_my_trait(String::new());
364+
| ------------ ^^^^^^^^^^^^^ My Label
365+
| |
366+
| required by a bound introduced by this call
367+
|
368+
= help: the trait `ImportantTrait<i32>` is not implemented for `String`
369+
= note: Note 1
370+
= note: Note 2
371+
```
372+
373+
[`std::fmt`]: ../../std/fmt/index.html
304374
[Clippy]: https://github.com/rust-lang/rust-clippy
305375
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
306376
[_MetaListPaths_]: ../attributes.md#meta-item-attribute-syntax

0 commit comments

Comments
 (0)