about summary refs log tree commit diff
path: root/compiler/rustc_parse_format/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-04-29 11:58:24 +0000
committerbors <bors@rust-lang.org>2022-04-29 11:58:24 +0000
commit683c582c1e88c573c454b7fa6f00bc6647421864 (patch)
treedb4d2b296b27bd7d137b8399343ed7a5f44b7b19 /compiler/rustc_parse_format/src
parent87937d3b6c302dfedfa5c4b94d0a30985d46298d (diff)
parentdca88612b96db761f86ab66d85836baf0e804910 (diff)
downloadrust-683c582c1e88c573c454b7fa6f00bc6647421864.tar.gz
rust-683c582c1e88c573c454b7fa6f00bc6647421864.zip
Auto merge of #96468 - davidtwco:diagnostic-translation-subdiagnostic, r=oli-obk
macros: subdiagnostic derive

Add a new macro, `#[derive(SessionSubdiagnostic)]`, which can be applied to structs that represent subdiagnostics, such as labels, notes, helps or suggestions.

`#[derive(SessionSubdiagnostic)]` can be used with the existing `#[derive(SessionDiagnostic)]`. All diagnostics implemented using either derive are translatable, and this new derive should make it easier to port existing diagnostics to using these derives.

For example, consider the following subdiagnostic types...

```rust
#[derive(SessionSubdiagnostic)]
pub enum ExpectedIdentifierLabel<'tcx> {
    #[label(slug = "parser-expected-identifier")]
    WithoutFound {
        #[primary_span]
        span: Span,
    }
    #[label(slug = "parser-expected-identifier-found")]
    WithFound {
        #[primary_span]
        span: Span,
        found: String,
    }
}

#[derive(SessionSubdiagnostic)]
#[suggestion_verbose(slug = "parser-raw-identifier")]
pub struct RawIdentifierSuggestion<'tcx> {
    #[primary_span]
    span: Span,
    #[applicability]
    applicability: Applicability,
    ident: Ident,
}
```

...and the corresponding Fluent messages:

```fluent
parser-expected-identifier = expected identifier

parser-expected-identifier-found = expected identifier, found {$found}

parser-raw-identifier = escape `{$ident}` to use it as an identifier
```

These can be emitted using the new `subdiagnostic` function on `Diagnostic`...

```rust
diag.subdiagnostic(ExpectedIdentifierLabel::WithoutFound { span });
diag.subdiagnostic(RawIdentifierSuggestion { span, applicability, ident });
```

...or as part of a larger `#[derive(SessionDiagnostic)]`:

```rust
#[derive(SessionDiagnostic)]
#[error(slug = "parser-expected-identifier")]
pub struct ExpectedIdentifier {
    #[primary_span]
    span: Span,
    token_descr: String,
    #[subdiagnostic]
    label: ExpectedIdentifierLabel,
    #[subdiagnostic]
    raw_identifier_suggestion: Option<RawIdentifierSuggestion>,
}
```

```rust
sess.emit_err(ExpectedIdentifier { ... });
```

r? `@oli-obk`
cc `@pvdrz`
Diffstat (limited to 'compiler/rustc_parse_format/src')
0 files changed, 0 insertions, 0 deletions