Skip to content

Commit 494e971

Browse files
authored
Rollup merge of #116611 - mejrs:diagnostic_namespace, r=ehuss
Document `diagnostic_namespace` feature This adds it to the rust unstable book. FWIW: I couldn't find a way to serve the book locally (please send help), so I can't check that this renders correctly. cc `@weiznich`
2 parents a7042a9 + ba02a99 commit 494e971

File tree

1 file changed

+84
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)