2 mod diagnostic_builder
;
8 use diagnostic
::{DiagnosticDerive, LintDiagnosticDerive}
;
9 pub(crate) use fluent
::fluent_messages
;
10 use proc_macro2
::TokenStream
;
11 use quote
::format_ident
;
12 use subdiagnostic
::SubdiagnosticDeriveBuilder
;
13 use synstructure
::Structure
;
15 /// Implements `#[derive(Diagnostic)]`, which allows for errors to be specified as a struct,
16 /// independent from the actual diagnostics emitting code.
19 /// # extern crate rustc_errors;
20 /// # use rustc_errors::Applicability;
21 /// # extern crate rustc_span;
22 /// # use rustc_span::{symbol::Ident, Span};
23 /// # extern crate rust_middle;
24 /// # use rustc_middle::ty::Ty;
25 /// #[derive(Diagnostic)]
26 /// #[diag(borrowck_move_out_of_borrow, code = "E0505")]
27 /// pub struct MoveOutOfBorrowError<'tcx> {
33 /// #[label(first_borrow_label)]
34 /// pub first_borrow_span: Span,
35 /// #[suggestion(code = "{name}.clone()")]
36 /// pub clone_sugg: Option<(Span, Applicability)>
41 /// move_out_of_borrow = cannot move out of {$name} because it is borrowed
42 /// .label = cannot move out of borrow
43 /// .first_borrow_label = `{$ty}` first borrowed here
44 /// .suggestion = consider cloning here
47 /// Then, later, to emit the error:
50 /// sess.emit_err(MoveOutOfBorrowError {
54 /// first_borrow_span,
55 /// clone_sugg: Some(suggestion, Applicability::MachineApplicable),
59 /// See rustc dev guide for more examples on using the `#[derive(Diagnostic)]`:
60 /// <https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-structs.html>
61 pub fn session_diagnostic_derive(s
: Structure
<'_
>) -> TokenStream
{
62 DiagnosticDerive
::new(format_ident
!("diag"), format_ident
!("handler"), s
).into_tokens()
65 /// Implements `#[derive(LintDiagnostic)]`, which allows for lints to be specified as a struct,
66 /// independent from the actual lint emitting code.
69 /// #[derive(LintDiagnostic)]
70 /// #[diag(lint_atomic_ordering_invalid_fail_success)]
71 /// pub struct AtomicOrderingInvalidLint {
73 /// success_ordering: Symbol,
74 /// fail_ordering: Symbol,
75 /// #[label(fail_label)]
76 /// fail_order_arg_span: Span,
77 /// #[label(success_label)]
79 /// code = "std::sync::atomic::Ordering::{success_suggestion}",
80 /// applicability = "maybe-incorrect"
82 /// success_order_arg_span: Span,
87 /// lint_atomic_ordering_invalid_fail_success = `{$method}`'s success ordering must be at least as strong as its failure ordering
88 /// .fail_label = `{$fail_ordering}` failure ordering
89 /// .success_label = `{$success_ordering}` success ordering
90 /// .suggestion = consider using `{$success_suggestion}` success ordering instead
93 /// Then, later, to emit the error:
96 /// cx.struct_span_lint(INVALID_ATOMIC_ORDERING, fail_order_arg_span, AtomicOrderingInvalidLint {
100 /// fail_order_arg_span,
101 /// success_order_arg_span,
105 /// See rustc dev guide for more examples on using the `#[derive(LintDiagnostic)]`:
106 /// <https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-structs.html#reference>
107 pub fn lint_diagnostic_derive(s
: Structure
<'_
>) -> TokenStream
{
108 LintDiagnosticDerive
::new(format_ident
!("diag"), s
).into_tokens()
111 /// Implements `#[derive(Subdiagnostic)]`, which allows for labels, notes, helps and
112 /// suggestions to be specified as a structs or enums, independent from the actual diagnostics
113 /// emitting code or diagnostic derives.
116 /// #[derive(Subdiagnostic)]
117 /// pub enum ExpectedIdentifierLabel<'tcx> {
118 /// #[label(expected_identifier)]
123 /// #[label(expected_identifier_found)]
131 /// #[derive(Subdiagnostic)]
132 /// #[suggestion_verbose(parser::raw_identifier)]
133 /// pub struct RawIdentifierSuggestion<'tcx> {
137 /// applicability: Applicability,
143 /// parser_expected_identifier = expected identifier
145 /// parser_expected_identifier-found = expected identifier, found {$found}
147 /// parser_raw_identifier = escape `{$ident}` to use it as an identifier
150 /// Then, later, to add the subdiagnostic:
153 /// diag.subdiagnostic(ExpectedIdentifierLabel::WithoutFound { span });
155 /// diag.subdiagnostic(RawIdentifierSuggestion { span, applicability, ident });
157 pub fn session_subdiagnostic_derive(s
: Structure
<'_
>) -> TokenStream
{
158 SubdiagnosticDeriveBuilder
::new().into_tokens(s
)