You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/attributes/diagnostics.md
+54
Original file line number
Diff line number
Diff line change
@@ -301,6 +301,60 @@ When used on a function in a trait implementation, the attribute does nothing.
301
301
> let _ = five();
302
302
> ```
303
303
304
+
## The `diagnostic` toolattributenamespace
305
+
306
+
The `#[diagnostic]` attributenamespaceismeanttoprovideahomeforattributethatallowuserstoinfluenceerrormessagesemittedbythecompiler.Thecompilerisnotguaranteedtouse any of this hints, however it should accept any (non-)existing attribute in this namespace and potentially emit lint-warnings for unused attributes and options.This is meant to allow discarding certain attributes/options in the future to allow fundamental changes to the compiler without the need to keep then non-meaningful options working.
307
+
308
+
### The `diagnostic::on_unimplemented` attribute
309
+
310
+
The `#[diagnostic::on_unimplemented]` attribute is allowed to appear on trait definitions.This allows crate authors to hint the compiler to emit a specific worded error message if a certain trait is not implemented.The hinted message is supposed to replace the otherwise emitted error message.For the `#[diagnostic::on_unimplemented]` attribute the following options are implemented:
311
+
312
+
* `message` which provides the text for the top level error message
313
+
* `label` which provides the text for the label shown inline in the broken code in the error message
314
+
* `note` which provides additional notes.
315
+
316
+
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.
317
+
318
+
All three options accept a text as argument.This text is allowed to contain format parameters referring to generic argument or `Self` by name via the `{Self}` or `{NameOfGenericArgument}` syntax.For any non-existing argument a lint warning is generated.
319
+
320
+
This allows to have a trait definition like:
321
+
322
+
```rust
323
+
#[diagnostic::on_unimplemented(
324
+
message = "MyMessagefor `ImportantTrait<{A}>` implemented for `{Self}`",
325
+
label = "MyLabel",
326
+
note = "Note 1",
327
+
note = "Note 2"
328
+
)]
329
+
traitImportantTrait<A> {}
330
+
```
331
+
332
+
which then generates the for the following code
333
+
334
+
```rust
335
+
fn use_my_trait(_:implImportantTrait<i32>) {}
336
+
337
+
fn main() {
338
+
use_my_trait(String::new());
339
+
}
340
+
```
341
+
342
+
this error message:
343
+
344
+
```
345
+
error[E0277]: My Message for `ImportantTrait<i32>` implemented for `String`
346
+
--> src/main.rs:14:18
347
+
|
348
+
14 | use_my_trait(String::new());
349
+
| ------------ ^^^^^^^^^^^^^ My Label
350
+
| |
351
+
| required by a bound introduced by this call
352
+
|
353
+
= help: the trait `ImportantTrait<i32>` is not implemented for `String`
0 commit comments