@@ -301,6 +301,76 @@ When used on a function in a trait implementation, the attribute does nothing.
301
301
> let _ = five();
302
302
> ```
303
303
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
304
374
[ Clippy ] : https://github.com/rust-lang/rust-clippy
305
375
[ _MetaListNameValueStr_ ] : ../attributes.md#meta-item-attribute-syntax
306
376
[ _MetaListPaths_ ] : ../attributes.md#meta-item-attribute-syntax
0 commit comments