Skip to content

Commit 11b4065

Browse files
authored
diagnostics: structs with new slug syntax (rust-lang#1377)
Update the documentation for diagnostic structs to use the new typed identifier syntax for referring to slugs. Signed-off-by: David Wood <[email protected]>
1 parent 03fafb5 commit 11b4065

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/diagnostics/diagnostic-structs.md

+37-21
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ shown below:
1717

1818
```rust,ignore
1919
#[derive(SessionDiagnostic)]
20-
#[error(code = "E0124", slug = "typeck-field-already-declared")]
20+
#[error(typeck::field_already_declared, code = "E0124")]
2121
pub struct FieldAlreadyDeclared {
2222
pub field_name: Ident,
2323
#[primary_span]
@@ -37,12 +37,13 @@ the `code` sub-attribute. Specifying a `code` isn't mandatory, but if you are
3737
porting a diagnostic that uses `DiagnosticBuilder` to use `SessionDiagnostic`
3838
then you should keep the code if there was one.
3939

40-
Both `#[error(..)]` and `#[warning(..)]` must set a value for the `slug`
41-
sub-attribute. `slug` uniquely identifies the diagnostic and is also how the
42-
compiler knows what error message to emit (in the default locale of the
43-
compiler, or in the locale requested by the user). See [translation
44-
documentation](./translation.md) to learn more about how translatable error
45-
messages are written.
40+
Both `#[error(..)]` and `#[warning(..)]` must provide a slug as the first
41+
positional argument (a path to an item in `rustc_errors::fluent::*`). A slug
42+
uniquely identifies the diagnostic and is also how the compiler knows what
43+
error message to emit (in the default locale of the compiler, or in the locale
44+
requested by the user). See [translation documentation](./translation.md) to
45+
learn more about how translatable error messages are written and how slug
46+
items are generated.
4647

4748
In our example, the Fluent message for the "field already declared" diagnostic
4849
looks like this:
@@ -54,7 +55,7 @@ typeck-field-already-declared =
5455
.previous-decl-label = `{$field_name}` first declared here
5556
```
5657

57-
`typeck-field-already-declared` is the `slug` from our example and is followed
58+
`typeck-field-already-declared` is the slug from our example and is followed
5859
by the diagnostic message.
5960

6061
Every field of the `SessionDiagnostic` which does not have an annotation is
@@ -147,15 +148,22 @@ tcx.sess.emit_err(FieldAlreadyDeclared {
147148
### Reference
148149
`#[derive(SessionDiagnostic)]` supports the following attributes:
149150

150-
- `#[error(code = "...", slug = "...")]` or `#[warning(code = "...", slug = "...")]`
151+
- `#[error(slug, code = "...")]` or `#[warning(slug, code = "...")]`
151152
- _Applied to struct._
152153
- _Mandatory_
153154
- Defines the struct to be representing an error or a warning.
154-
- `code = "..."` (_Optional_)
155-
- Specifies the error code.
156-
- `slug = "..."` (_Mandatory_)
155+
- Slug (_Mandatory_)
157156
- Uniquely identifies the diagnostic and corresponds to its Fluent message,
158157
mandatory.
158+
- A path to an item in `rustc_errors::fluent`. Always in a module starting
159+
with a Fluent resource name (which is typically the name of the crate
160+
that the diagnostic is from), e.g.
161+
`rustc_errors::fluent::typeck::field_already_declared`
162+
(`rustc_errors::fluent` is implicit in the attribute, so just
163+
`typeck::field_already_declared`).
164+
- See [translation documentation](./translation.md).
165+
- `code = "..."` (_Optional_)
166+
- Specifies the error code.
159167
- `#[note]` or `#[note = "..."]` (_Optional_)
160168
- _Applied to struct or `Span`/`()` fields._
161169
- Adds a note subdiagnostic.
@@ -215,12 +223,12 @@ shown below:
215223
```rust
216224
#[derive(SessionSubdiagnostic)]
217225
pub enum ExpectedReturnTypeLabel<'tcx> {
218-
#[label(slug = "typeck-expected-default-return-type")]
226+
#[label(typeck::expected_default_return_type)]
219227
Unit {
220228
#[primary_span]
221229
span: Span,
222230
},
223-
#[label(slug = "typeck-expected-return-type")]
231+
#[label(typeck::expected_return_type)]
224232
Other {
225233
#[primary_span]
226234
span: Span,
@@ -239,11 +247,12 @@ attribute applied to the struct or each variant, one of:
239247
- `#[help(..)]` for defining a help
240248
- `#[suggestion{,_hidden,_short,_verbose}(..)]` for defining a suggestion
241249

242-
All of the above must have a value set for the `slug` sub-attribute. `slug`
243-
uniquely identifies the diagnostic and is also how the compiler knows what
244-
error message to emit (in the default locale of the compiler, or in the locale
245-
requested by the user). See [translation documentation](./translation.md) to
246-
learn more about how translatable error messages are written.
250+
All of the above must provide a slug as the first positional argument (a path
251+
to an item in `rustc_errors::fluent::*`). A slug uniquely identifies the
252+
diagnostic and is also how the compiler knows what error message to emit (in
253+
the default locale of the compiler, or in the locale requested by the user).
254+
See [translation documentation](./translation.md) to learn more about how
255+
translatable error messages are written and how slug items are generated.
247256

248257
In our example, the Fluent message for the "expected return type" label
249258
looks like this:
@@ -315,13 +324,20 @@ diagnostic struct.
315324
### Reference
316325
`#[derive(SessionSubdiagnostic)]` supports the following attributes:
317326

318-
- `#[label(slug = "...")]`, `#[help(slug = "...")]` or `#[note(slug = "...")]`
327+
- `#[label(slug)]`, `#[help(slug)]` or `#[note(slug)]`
319328
- _Applied to struct or enum variant. Mutually exclusive with struct/enum variant attributes._
320329
- _Mandatory_
321330
- Defines the type to be representing a label, help or note.
322-
- `slug = "..."` (_Mandatory_)
331+
- Slug (_Mandatory_)
323332
- Uniquely identifies the diagnostic and corresponds to its Fluent message,
324333
mandatory.
334+
- A path to an item in `rustc_errors::fluent`. Always in a module starting
335+
with a Fluent resource name (which is typically the name of the crate
336+
that the diagnostic is from), e.g.
337+
`rustc_errors::fluent::typeck::field_already_declared`
338+
(`rustc_errors::fluent` is implicit in the attribute, so just
339+
`typeck::field_already_declared`).
340+
- See [translation documentation](./translation.md).
325341
- `#[suggestion{,_hidden,_short,_verbose}(message = "...", code = "...", applicability = "...")]`
326342
- _Applied to struct or enum variant. Mutually exclusive with struct/enum variant attributes._
327343
- _Mandatory_

0 commit comments

Comments
 (0)