@@ -4,8 +4,7 @@ rustc has three diagnostic traits that can be used to create diagnostics:
44
55For simple diagnostics,
66derived impls can be used, e.g. ` #[derive(Diagnostic)] ` . They are only suitable for simple diagnostics that
7- don't require much logic in deciding whether or not to add additional
8- subdiagnostics.
7+ don't require much logic in deciding whether or not to add additional subdiagnostics.
98
109In cases where diagnostics require more complex or dynamic behavior, such as conditionally adding subdiagnostics,
1110customizing the rendering logic, or selecting messages at runtime, you will need to manually implement
@@ -16,8 +15,7 @@ Diagnostic can be translated into different languages.
1615
1716## ` #[derive(Diagnostic)] ` and ` #[derive(LintDiagnostic)] `
1817
19- Consider the [ definition] [ defn ] of the "field already declared" diagnostic
20- shown below:
18+ Consider the [ definition] [ defn ] of the "field already declared" diagnostic shown below:
2119
2220``` rust,ignore
2321#[derive(Diagnostic)]
@@ -32,47 +30,47 @@ pub struct FieldAlreadyDeclared {
3230}
3331```
3432
35- ` Diagnostic ` can only be derived on structs and enums.
33+ ` Diagnostic ` can only be derived on structs and enums.
3634Attributes that are placed on the type for structs are placed on each
37- variants for enums (or vice versa). Each ` Diagnostic ` has to have one
35+ variants for enums (or vice versa).
36+ Each ` Diagnostic ` has to have one
3837attribute, ` #[diag(...)] ` , applied to the struct or each enum variant.
3938
4039If an error has an error code (e.g. "E0624"), then that can be specified using
41- the ` code ` sub-attribute. Specifying a ` code ` isn't mandatory, but if you are
40+ the ` code ` sub-attribute.
41+ Specifying a ` code ` isn't mandatory, but if you are
4242porting a diagnostic that uses ` Diag ` to use ` Diagnostic `
4343then you should keep the code if there was one.
4444
45- ` #[diag(..)] ` must provide a message as the first positional argument.
46- The message is written in English, but might be translated to the locale requested by the user. See
47- [ translation documentation] ( ./translation.md ) to learn more about how
45+ ` #[diag(..)] ` must provide a message as the first positional argument.
46+ The message is written in English, but might be translated to the locale requested by the user.
47+ See [ translation documentation] ( ./translation.md ) to learn more about how
4848translatable error messages are written and how they are generated.
4949
5050Every field of the ` Diagnostic ` which does not have an annotation is
51- available in Fluent messages as a variable, like ` field_name ` in the example
52- above. Fields can be annotated ` #[skip_arg] ` if this is undesired.
51+ available in Fluent messages as a variable, like ` field_name ` in the example above.
52+ Fields can be annotated ` #[skip_arg] ` if this is undesired.
5353
5454Using the ` #[primary_span] ` attribute on a field (that has type ` Span ` )
55- indicates the primary span of the diagnostic which will have the main message
56- of the diagnostic.
55+ indicates the primary span of the diagnostic which will have the main message of the diagnostic.
5756
5857Diagnostics are more than just their primary message, they often include
59- labels, notes, help messages and suggestions, all of which can also be
60- specified on a ` Diagnostic ` .
58+ labels, notes, help messages and suggestions, all of which can also be specified on a ` Diagnostic ` .
6159
6260` #[label] ` , ` #[help] ` , ` #[warning] ` and ` #[note] ` can all be applied to fields which have the
63- type ` Span ` . Applying any of these attributes will create the corresponding
64- subdiagnostic with that ` Span ` . These attributes take a diagnostic message as an argument.
61+ type ` Span ` .
62+ Applying any of these attributes will create the corresponding subdiagnostic with that ` Span ` .
63+ These attributes take a diagnostic message as an argument.
6564
6665Other types have special behavior when used in a ` Diagnostic ` derive:
6766
6867- Any attribute applied to an ` Option<T> ` will only emit a
6968 subdiagnostic if the option is ` Some(..) ` .
70- - Any attribute applied to a ` Vec<T> ` will be repeated for each element of the
71- vector.
69+ - Any attribute applied to a ` Vec<T> ` will be repeated for each element of the vector.
7270
7371` #[help] ` , ` #[warning] ` and ` #[note] ` can also be applied to the struct itself, in which case
74- they work exactly like when applied to fields except the subdiagnostic won't
75- have a ` Span ` . These attributes can also be applied to fields of type ` () ` for
72+ they work exactly like when applied to fields except the subdiagnostic won't have a ` Span ` .
73+ These attributes can also be applied to fields of type ` () ` for
7674the same effect, which when combined with the ` Option ` type can be used to
7775represent optional ` #[note] ` /` #[help] ` /` #[warning] ` subdiagnostics.
7876
@@ -84,8 +82,8 @@ Suggestions can be emitted using one of four field attributes:
8482- ` #[suggestion_verbose("message", code = "...", applicability = "...")] `
8583
8684Suggestions must be applied on either a ` Span ` field or a `(Span,
87- MachineApplicability)` field. Similarly to other field attributes, a message
88- needs to be provided which will be shown to the user.
85+ MachineApplicability)` field.
86+ Similarly to other field attributes, a message needs to be provided which will be shown to the user.
8987` code ` specifies the code that should be suggested as a
9088replacement and is a format string (e.g. ` {field_name} ` would be replaced by
9189the value of the ` field_name ` field of the struct).
@@ -113,8 +111,8 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a> for FieldAlreadyDeclared {
113111}
114112```
115113
116- Now that we've defined our diagnostic, how do we [ use it] [ use ] ? It's quite
117- straightforward, just create an instance of the struct and pass it to
114+ Now that we've defined our diagnostic, how do we [ use it] [ use ] ?
115+ It's quite straightforward, just create an instance of the struct and pass it to
118116` emit_err ` (or ` emit_warning ` ):
119117
120118``` rust,ignore
@@ -126,8 +124,7 @@ tcx.dcx().emit_err(FieldAlreadyDeclared {
126124```
127125
128126### Reference for ` #[derive(Diagnostic)] ` and ` #[derive(LintDiagnostic)] `
129- ` #[derive(Diagnostic)] ` and ` #[derive(LintDiagnostic)] ` support the
130- following attributes:
127+ ` #[derive(Diagnostic)] ` and ` #[derive(LintDiagnostic)] ` support the following attributes:
131128
132129- ` #[diag("message", code = "...")] `
133130 - _ Applied to struct or enum variant._
@@ -164,32 +161,32 @@ following attributes:
164161 - Value is the suggestion message that will be shown to the user.
165162 - See [ translation documentation] ( ./translation.md ) .
166163 - ` code = "..." ` /` code("...", ...) ` (_ Mandatory_ )
167- - One or multiple format strings indicating the code to be suggested as a
168- replacement. Multiple values signify multiple possible replacements.
164+ - One or multiple format strings indicating the code to be suggested as a replacement.
165+ Multiple values signify multiple possible replacements.
169166 - ` applicability = "..." ` (_ Optional_ )
170167 - String which must be one of ` machine-applicable ` , ` maybe-incorrect ` ,
171168 ` has-placeholders ` or ` unspecified ` .
172169- ` #[subdiagnostic] `
173- - _ Applied to a type that implements ` Subdiagnostic ` (from
174- ` #[derive(Subdiagnostic)] ` )._
170+ - _ Applied to a type that implements ` Subdiagnostic ` (from ` #[derive(Subdiagnostic)] ` )._
175171 - Adds the subdiagnostic represented by the subdiagnostic struct.
176172- ` #[primary_span] ` (_ Optional_ )
177- - _ Applied to ` Span ` fields on ` Subdiagnostic ` s. Not used for ` LintDiagnostic ` s._
173+ - _ Applied to ` Span ` fields on ` Subdiagnostic ` s.
174+ Not used for ` LintDiagnostic ` s._
178175 - Indicates the primary span of the diagnostic.
179176- ` #[skip_arg] ` (_ Optional_ )
180177 - _ Applied to any field._
181178 - Prevents the field from being provided as a diagnostic argument.
182179
183180## ` #[derive(Subdiagnostic)] `
184181It is common in the compiler to write a function that conditionally adds a
185- specific subdiagnostic to an error if it is applicable. Oftentimes these
186- subdiagnostics could be represented using a diagnostic struct even if the
187- overall diagnostic could not. In this circumstance, the ` Subdiagnostic `
182+ specific subdiagnostic to an error if it is applicable.
183+ Oftentimes these subdiagnostics could be represented using a diagnostic struct even if the
184+ overall diagnostic could not.
185+ In this circumstance, the ` Subdiagnostic `
188186derive can be used to represent a partial diagnostic (e.g a note, label, help or
189187suggestion) as a struct.
190188
191- Consider the [ definition] [ subdiag_defn ] of the "expected return type" label
192- shown below:
189+ Consider the [ definition] [ subdiag_defn ] of the "expected return type" label shown below:
193190
194191``` rust
195192#[derive(Subdiagnostic )]
@@ -208,10 +205,10 @@ pub enum ExpectedReturnTypeLabel<'tcx> {
208205}
209206```
210207
211- Like ` Diagnostic ` , ` Subdiagnostic ` can be derived for structs or
212- enums. Attributes that are placed on the type for structs are placed on each
213- variants for enums (or vice versa). Each ` Subdiagnostic ` should have one
214- attribute applied to the struct or each variant, one of:
208+ Like ` Diagnostic ` , ` Subdiagnostic ` can be derived for structs or enums.
209+ Attributes that are placed on the type for structs are placed on each
210+ variants for enums (or vice versa).
211+ Each ` Subdiagnostic ` should have one attribute applied to the struct or each variant, one of:
215212
216213- ` #[label(..)] ` for defining a label
217214- ` #[note(..)] ` for defining a note
@@ -224,15 +221,14 @@ See [translation documentation](./translation.md) to learn more about how
224221translatable error messages are generated.
225222
226223Using the ` #[primary_span] ` attribute on a field (with type ` Span ` ) will denote
227- the primary span of the subdiagnostic. A primary span is only necessary for a
228- label or suggestion, which can not be spanless.
224+ the primary span of the subdiagnostic.
225+ A primary span is only necessary for a label or suggestion, which can not be spanless.
229226
230227Every field of the type/variant which does not have an annotation is available
231- in Fluent messages as a variable. Fields can be annotated ` #[skip_arg] ` if this
232- is undesired.
228+ in Fluent messages as a variable.
229+ Fields can be annotated ` #[skip_arg] ` if this is undesired.
233230
234- Like ` Diagnostic ` , ` Subdiagnostic ` supports ` Option<T> ` and
235- ` Vec<T> ` fields.
231+ Like ` Diagnostic ` , ` Subdiagnostic ` supports ` Option<T> ` and ` Vec<T> ` fields.
236232
237233Suggestions can be emitted using one of four attributes on the type/variant:
238234
@@ -241,8 +237,7 @@ Suggestions can be emitted using one of four attributes on the type/variant:
241237- ` #[suggestion_short("...", code = "...", applicability = "...")] `
242238- ` #[suggestion_verbose("...", code = "...", applicability = "...")] `
243239
244- Suggestions require ` #[primary_span] ` be set on a field and can have the
245- following sub-attributes:
240+ Suggestions require ` #[primary_span] ` be set on a field and can have the following sub-attributes:
246241
247242- The first positional argument specifies the message which will be shown to the user.
248243- ` code ` specifies the code that should be suggested as a replacement and is a
@@ -276,8 +271,7 @@ impl<'tcx> Subdiagnostic for ExpectedReturnTypeLabel<'tcx> {
276271
277272Once defined, a subdiagnostic can be used by passing it to the ` subdiagnostic `
278273function ([ example] [ subdiag_use_1 ] and [ example] [ subdiag_use_2 ] ) on a
279- diagnostic or by assigning it to a ` #[subdiagnostic] ` -annotated field of a
280- diagnostic struct.
274+ diagnostic or by assigning it to a ` #[subdiagnostic] ` -annotated field of a diagnostic struct.
281275
282276### Argument sharing and isolation
283277
@@ -310,22 +304,24 @@ Additionally, subdiagnostics can access arguments from the main diagnostic with
310304` #[derive(Subdiagnostic)] ` supports the following attributes:
311305
312306- ` #[label("message")] ` , ` #[help("message")] ` , ` #[warning("message")] ` or ` #[note("message")] `
313- - _ Applied to struct or enum variant. Mutually exclusive with struct/enum variant attributes._
307+ - _ Applied to struct or enum variant.
308+ Mutually exclusive with struct/enum variant attributes._
314309 - _ Mandatory_
315310 - Defines the type to be representing a label, help or note.
316311 - Message (_ Mandatory_ )
317312 - The diagnostic message that will be shown to the user.
318313 - See [ translation documentation] ( ./translation.md ) .
319314- ` #[suggestion{,_hidden,_short,_verbose}("message", code = "...", applicability = "...")] `
320- - _ Applied to struct or enum variant. Mutually exclusive with struct/enum variant attributes._
315+ - _ Applied to struct or enum variant.
316+ Mutually exclusive with struct/enum variant attributes._
321317 - _ Mandatory_
322318 - Defines the type to be representing a suggestion.
323319 - Message (_ Mandatory_ )
324320 - The diagnostic message that will be shown to the user.
325321 - See [ translation documentation] ( ./translation.md ) .
326322 - ` code = "..." ` /` code("...", ...) ` (_ Mandatory_ )
327- - One or multiple format strings indicating the code to be suggested as a
328- replacement. Multiple values signify multiple possible replacements.
323+ - One or multiple format strings indicating the code to be suggested as a replacement.
324+ Multiple values signify multiple possible replacements.
329325 - ` applicability = "..." ` (_ Optional_ )
330326 - _ Mutually exclusive with ` #[applicability] ` on a field._
331327 - Value is the applicability of the suggestion.
@@ -335,7 +331,8 @@ Additionally, subdiagnostics can access arguments from the main diagnostic with
335331 - ` has-placeholders `
336332 - ` unspecified `
337333- ` #[multipart_suggestion{,_hidden,_short,_verbose}("message", applicability = "...")] `
338- - _ Applied to struct or enum variant. Mutually exclusive with struct/enum variant attributes._
334+ - _ Applied to struct or enum variant.
335+ Mutually exclusive with struct/enum variant attributes._
339336 - _ Mandatory_
340337 - Defines the type to be representing a multipart suggestion.
341338 - Message (_ Mandatory_ ): see ` #[suggestion] `
@@ -348,8 +345,7 @@ to multipart suggestions)
348345 - _ Applied to ` Span ` fields._
349346 - Indicates the span to be one part of the multipart suggestion.
350347 - ` code = "..." ` (_ Mandatory_ )
351- - Value is a format string indicating the code to be suggested as a
352- replacement.
348+ - Value is a format string indicating the code to be suggested as a replacement.
353349- ` #[applicability] ` (_ Optional_ ; only applicable to (simple and multipart) suggestions)
354350 - _ Applied to ` Applicability ` fields._
355351 - Indicates the applicability of the suggestion.
0 commit comments