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: text/3366-diagnostic-attribute-namespace.md
+32-21Lines changed: 32 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -71,32 +71,50 @@ I expect the new attributes to be documented on the existing [Diagnostics](https
71
71
72
72
This RFC proposes to introduce a new `#[diagnostic]` attribute namespace. This namespace is supposed to contain different attributes, which allow users to hint the compiler to emit specific error messages in certain cases like type mismatches, unsatisfied trait bounds or similar situations. By collecting such attributes in a common namespace it is easier for users to find useful attributes and it is easier for the language team to establish a set of common rules for these attributes.
73
73
74
+
Attributes in this namespace are generally expected to be formed like:
75
+
```rust
76
+
#[diagnostic::attribute(option)]
77
+
```
78
+
where several `option` entries can appear in the same attribute. `option` is expected to be a valid attribute argument in this position. Attributes in this namespace are versioned. Any incompatible change to an existing stable attribute in this namespace requires bumping the version of the diagnostic attribute namespace. Crates using these attributes must specify the expected version of the diagnostic namespace via a `#![diagnostic::v{version_number}]` crate top-level attribute. If multiple such attributes are present the compiler will choose the highest version number and ignore the rest. The compiler is encouraged to provide a lint for outdated versions of the diagnostic attribute namespace. This mechanism exists so that a third party crate can provide a shim implementation of the corresponding attributes to translate between different versions of the diagnostic attribute namespace on different rust versions.
79
+
74
80
Any attribute in this namespace may:
75
81
76
82
* Hint the compiler to emit a specific error message in a specific situation
83
+
* Only affect the messages emitted by a compiler in case of a failed compilation
77
84
78
85
Any attribute in this namespace is not allowed to:
79
86
80
87
* Change the result of the compilation, which means applying such an attribute should never cause an error as long as they are syntactically valid
88
+
* pass-through information from the source of the diagnostic in a way that users can rely on. E.g. Such an attribute should not allow users to keep the compilation successful and dump information about `extern` blocks to generate C header files
81
89
82
90
The compiler is allowed to:
83
91
84
-
* Ignore the provided hints
85
-
* Use only parts of the provided hints, for example for the proposed `#[diagnostic::on_unimplemented]` only use the `message` option
86
-
* Change this behaviour at any time
92
+
* Ignore the hints provided by:
93
+
+ A specific attribute
94
+
+ A specific option
95
+
* Change the set of supported attributes and options at any time
96
+
* Provide warning lints for malformed or otherwise not accepted attributes
87
97
88
-
The compiler must:
98
+
The compiler must not:
89
99
90
-
* Verify that the attribute is syntactically correct, which means:
91
-
+ Its one of the accepted attributes
92
-
+ It only contains the allowed options
93
-
+ Any provided option follows the defined syntax
94
-
* Follow the described semantics if the attribute is not ignored.
100
+
* Change the semantic of an attribute or option, these kind of change requires a bump of the diagnostic attribute space version number
101
+
* Emit an hard error on malformed attribute, although emitting lints is fine
95
102
103
+
Adding a new attribute or option to the `#[diagnostic]` namespace is a decision of the compiler team. The envisioned procedure for this is as following:
104
+
105
+
1. The new attribute lands on nightly without a feature gate. It gets ignored on stable/beta until its explicitly activated there.
106
+
2. The time as nightly only attribute is used to verify that:
107
+
* The attribute solves a real problem
108
+
* The implemented syntax matches the expectations
109
+
3. To enable a certain attribute in stable release the following steps are required:
110
+
* The attribute is documented in the rust reference
111
+
* T-lang is informed
112
+
* T-compiler performs a FCP, where a simple majority (>50%) accepts the attribute
113
+
96
114
97
115
## The `#[diagnostic::on_unimplemented]` attribute
98
116
99
-
This section describes the syntax of the `on_unimplemented` attribute and additionally how it is supposed to work. Implementing the later part is optional as outlined in the previous section. Any syntax not explicitly described in this section must be rejected as invalid according to the general rules of the `#[diagnostic]` attribute namespace.
117
+
This section describes the syntax of the `on_unimplemented` attribute and additionally how it is supposed to work. The specification of this attribute is partially provided as example and motivation for the `#[diagnostic]` attribute namespace. In addition it is provided to give this RFC a
100
118
101
119
```rust
102
120
#[diagnostic::on_unimplemented(
@@ -164,6 +182,10 @@ Another drawback is that crates might hint lower quality error messages than the
164
182
165
183
This proposal tries to improve error messages generated by rustc. It would give crate authors a tool to influence what error message is emitted in a certain situation, as they might sometimes want to provide specific details on certain error conditions. Not implementing this proposal would result in the current status quo. Currently the compiler always shows a "general" error message, even if it would be helpful to show additional details.
166
184
185
+
There are alternatives for the naming of the `#[diagnostic]` namespace:
186
+
187
+
* Use a `#[rustc]` namespace for these attributes. This would signifies that these are rustc specific extensions. We likely want to encourage other rust implementations to utilise these information as well, therefore a more general attribute namespace seems to be a better solution.
188
+
167
189
There are alternative designs for the proposed `on_unimplemented` attribute:
168
190
169
191
* The `on()` based filtering might be replaceable by placing the attribute on negative trait impls. This would turn a filter like
@@ -196,17 +218,6 @@ Notably all of the listed similar features are unofficial language extensions.
196
218
197
219
Clarify the procedure of various potential changes prior stabilisation of the attribute namespace:
198
220
199
-
* Process of adding a new attribute to the `#[diagnostics]` attribute macro namespace
200
-
+ Requires a RFC? (t-lang seems to prefer that?)
201
-
+ Requires a language/compiler MCP?
202
-
+ Just a PR to the compiler?
203
-
* Process of extending an existing attribute by adding more options
204
-
+ Requires a RFC?
205
-
+ Requires a language/compiler MCP?
206
-
+ Just a PR to the compiler?
207
-
* Process of removing support for specific options or an entire attribute from rustc
208
-
+ Requires a compiler MCP?
209
-
+ Just a PR to the compiler?
210
221
* Exact syntax of the `on_unimplemented` attribute
211
222
+ Can `Self` be accepted in that position or do we need another name?
212
223
+ Is `if()` a valid identifier in the proposed position?
0 commit comments