Document changes introduced by kind-less SessionDiagnostics

This commit is contained in:
Xiretza 2022-08-19 19:04:16 +02:00 committed by Michael Goulet
parent 6eaa13346d
commit 391f3bdd9e
1 changed files with 11 additions and 16 deletions

View File

@ -17,7 +17,7 @@ shown below:
```rust,ignore ```rust,ignore
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[error(typeck::field_already_declared, code = "E0124")] #[diag(typeck::field_already_declared, code = "E0124")]
pub struct FieldAlreadyDeclared { pub struct FieldAlreadyDeclared {
pub field_name: Ident, pub field_name: Ident,
#[primary_span] #[primary_span]
@ -29,21 +29,19 @@ pub struct FieldAlreadyDeclared {
``` ```
`SessionDiagnostic` can only be applied to structs. Every `SessionDiagnostic` `SessionDiagnostic` can only be applied to structs. Every `SessionDiagnostic`
has to have one attribute applied to the struct itself: either `#[error(..)]` has to have one attribute, `#[diag(...)]`, applied to the struct itself.
for defining errors, or `#[warning(..)]` for defining warnings.
If an error has an error code (e.g. "E0624"), then that can be specified using If an error has an error code (e.g. "E0624"), then that can be specified using
the `code` sub-attribute. Specifying a `code` isn't mandatory, but if you are the `code` sub-attribute. Specifying a `code` isn't mandatory, but if you are
porting a diagnostic that uses `DiagnosticBuilder` to use `SessionDiagnostic` porting a diagnostic that uses `DiagnosticBuilder` to use `SessionDiagnostic`
then you should keep the code if there was one. then you should keep the code if there was one.
Both `#[error(..)]` and `#[warning(..)]` must provide a slug as the first `#[diag(..)]` must provide a slug as the first positional argument (a path to an
positional argument (a path to an item in `rustc_errors::fluent::*`). A slug item in `rustc_errors::fluent::*`). A slug uniquely identifies the diagnostic
uniquely identifies the diagnostic and is also how the compiler knows what and is also how the compiler knows what error message to emit (in the default
error message to emit (in the default locale of the compiler, or in the locale locale of the compiler, or in the locale requested by the user). See
requested by the user). See [translation documentation](./translation.md) to [translation documentation](./translation.md) to learn more about how
learn more about how translatable error messages are written and how slug translatable error messages are written and how slug items are generated.
items are generated.
In our example, the Fluent message for the "field already declared" diagnostic In our example, the Fluent message for the "field already declared" diagnostic
looks like this: looks like this:
@ -146,13 +144,10 @@ tcx.sess.emit_err(FieldAlreadyDeclared {
`#[derive(SessionDiagnostic)]` and `#[derive(LintDiagnostic)]` support the `#[derive(SessionDiagnostic)]` and `#[derive(LintDiagnostic)]` support the
following attributes: following attributes:
- `#[error(slug, code = "...")]`, `#[warning(slug, code = "...")]`, - `#[diag(slug, code = "...")]`
`#[fatal(slug, code = "...")]` or `#[lint(slug, code = "...")]`
- _Applied to struct._ - _Applied to struct._
- _Mandatory_ - _Mandatory_
- Defines the struct to be representing an error, fatal error, a warning or a - Defines the text and error code to be associated with the diagnostic.
lint. Errors, fatal errors and warnings only supported by
`SessionDiagnostic`, and lints by `LintDiagnostic`.
- Slug (_Mandatory_) - Slug (_Mandatory_)
- Uniquely identifies the diagnostic and corresponds to its Fluent message, - Uniquely identifies the diagnostic and corresponds to its Fluent message,
mandatory. mandatory.
@ -218,7 +213,7 @@ following attributes:
`#[derive(SessionSubdiagnostic)]`)._ `#[derive(SessionSubdiagnostic)]`)._
- Adds the subdiagnostic represented by the subdiagnostic struct. - Adds the subdiagnostic represented by the subdiagnostic struct.
- `#[primary_span]` (_Optional_) - `#[primary_span]` (_Optional_)
- _Applied to `Span` fields._ - _Applied to `Span` fields on `SessionSubdiagnostic`s. Not used for `LintDiagnostic`s._
- Indicates the primary span of the diagnostic. - Indicates the primary span of the diagnostic.
- `#[skip_arg]` (_Optional_) - `#[skip_arg]` (_Optional_)
- _Applied to any field._ - _Applied to any field._