mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
683c582c1e
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` |
||
---|---|---|
.. | ||
rustc | ||
rustc_apfloat | ||
rustc_arena | ||
rustc_ast | ||
rustc_ast_lowering | ||
rustc_ast_passes | ||
rustc_ast_pretty | ||
rustc_attr | ||
rustc_borrowck | ||
rustc_builtin_macros | ||
rustc_codegen_cranelift | ||
rustc_codegen_gcc | ||
rustc_codegen_llvm | ||
rustc_codegen_ssa | ||
rustc_const_eval | ||
rustc_data_structures | ||
rustc_driver | ||
rustc_error_codes | ||
rustc_error_messages | ||
rustc_errors | ||
rustc_expand | ||
rustc_feature | ||
rustc_fs_util | ||
rustc_graphviz | ||
rustc_hir | ||
rustc_hir_pretty | ||
rustc_incremental | ||
rustc_index | ||
rustc_infer | ||
rustc_interface | ||
rustc_lexer | ||
rustc_lint | ||
rustc_lint_defs | ||
rustc_llvm | ||
rustc_log | ||
rustc_macros | ||
rustc_metadata | ||
rustc_middle | ||
rustc_mir_build | ||
rustc_mir_dataflow | ||
rustc_mir_transform | ||
rustc_monomorphize | ||
rustc_parse | ||
rustc_parse_format | ||
rustc_passes | ||
rustc_plugin_impl | ||
rustc_privacy | ||
rustc_query_impl | ||
rustc_query_system | ||
rustc_resolve | ||
rustc_save_analysis | ||
rustc_serialize | ||
rustc_session | ||
rustc_span | ||
rustc_symbol_mangling | ||
rustc_target | ||
rustc_trait_selection | ||
rustc_traits | ||
rustc_ty_utils | ||
rustc_type_ir | ||
rustc_typeck |