Skip to content

Commit a9fc409

Browse files
committed
Incorporate suggestions from the @joshtriplett around the general
semantics of the `#[diagnostic]` attribute namespace
1 parent 30f575c commit a9fc409

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

text/3366-diagnostic-attribute-namespace.md

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,50 @@ I expect the new attributes to be documented on the existing [Diagnostics](https
7171

7272
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.
7373

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+
7480
Any attribute in this namespace may:
7581

7682
* 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
7784

7885
Any attribute in this namespace is not allowed to:
7986

8087
* 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
8189

8290
The compiler is allowed to:
8391

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
8797

88-
The compiler must:
98+
The compiler must not:
8999

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
95102

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+
96114

97115
## The `#[diagnostic::on_unimplemented]` attribute
98116

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
100118

101119
```rust
102120
#[diagnostic::on_unimplemented(
@@ -164,6 +182,10 @@ Another drawback is that crates might hint lower quality error messages than the
164182

165183
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.
166184

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+
167189
There are alternative designs for the proposed `on_unimplemented` attribute:
168190

169191
* 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.
196218

197219
Clarify the procedure of various potential changes prior stabilisation of the attribute namespace:
198220

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?
210221
* Exact syntax of the `on_unimplemented` attribute
211222
+ Can `Self` be accepted in that position or do we need another name?
212223
+ Is `if()` a valid identifier in the proposed position?

0 commit comments

Comments
 (0)