|
| 1 | +# `diagnostic_namespace` |
| 2 | + |
| 3 | +The tracking issue for this feature is: [#111996] |
| 4 | + |
| 5 | +[#111996]: https://github.com./rust-lang/rust/issues/111996 |
| 6 | + |
| 7 | +------------------------ |
| 8 | + |
| 9 | +The `diagnostic_namespace` feature permits customization of compilation errors. |
| 10 | + |
| 11 | + |
| 12 | +# diagnostic::on_unimplemented |
| 13 | + |
| 14 | +With [#114452] support for `diagnostic::on_unimplemented` was added. |
| 15 | + |
| 16 | +When used on a trait declaration, the following options are available: |
| 17 | + |
| 18 | +* `message` to customize the primary error message |
| 19 | +* `note` to add a customized note message to an error message |
| 20 | +* `label` to customize the label part of the error message |
| 21 | + |
| 22 | +The attribute will hint to the compiler to use these in error messages: |
| 23 | +```rust |
| 24 | +// some library |
| 25 | + |
| 26 | +#![feature(diagnostic_namespace)] |
| 27 | + |
| 28 | +#[diagnostic::on_unimplemented( |
| 29 | + message = "cannot insert element", |
| 30 | + label = "cannot be put into a table", |
| 31 | + note = "see <link> for more information about the Table api" |
| 32 | +)] |
| 33 | +pub trait Element { |
| 34 | + // ... |
| 35 | +} |
| 36 | +# struct Table; |
| 37 | +# impl Table { |
| 38 | +# fn insert<T: Element>(&self, element: T) { |
| 39 | +# // ... |
| 40 | +# } |
| 41 | +#} |
| 42 | +# fn main() { |
| 43 | +# let table = Table; |
| 44 | +# let element = (); |
| 45 | +// user code |
| 46 | +table.insert(element); |
| 47 | +#} |
| 48 | +``` |
| 49 | +```text |
| 50 | +error[E0277]: cannot insert element |
| 51 | + --> src/main.rs:24:18 |
| 52 | + | |
| 53 | +24 | table.insert(element); |
| 54 | + | ------ ^^^^^^^ cannot be put into a table |
| 55 | + | | |
| 56 | + | required by a bound introduced by this call |
| 57 | + | |
| 58 | + = help: the trait `Element` is not implemented for `<type>` |
| 59 | + = note: see <link> for more information about the Table api |
| 60 | +note: required by a bound in `Table::insert` |
| 61 | + --> src/main.rs:15:18 |
| 62 | + | |
| 63 | +15 | fn insert<T: Element>(&self, element: T) { |
| 64 | + | ^^^^^^^ required by this bound in `Table::insert` |
| 65 | +
|
| 66 | +For more information about this error, try `rustc --explain E0277`. |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +See [RFC 3368] for more information. |
| 71 | + |
| 72 | +[#114452]: https://github.com./rust-lang/rust/pull/114452 |
| 73 | +[RFC 3368]: https://github.com./rust-lang/rfcs/blob/master/text/3368-diagnostic-attribute-namespace.md |
0 commit comments