Skip to main content

diagnostic_derive

Function diagnostic_derive 

Source
pub(crate) fn diagnostic_derive(s: Structure<'_>) -> TokenStream
Expand description

Implements #[derive(Diagnostic)], which allows for errors to be specified as a struct, independent from the actual diagnostics emitting code.

ⓘ
#[derive(Diagnostic)]
#[diag("this is an example message", code = E0123)]
pub(crate) struct ExampleError<'tcx> {
    pub name: Ident,
    pub ty: Ty<'tcx>,
    #[primary_span]
    #[label("with a label")]
    pub span: Span,
    #[label("with a label")]
    pub other_span: Span,
    #[suggestion("with a suggestion", code = "{name}.clone()")]
    pub opt_sugg: Option<(Span, Applicability)>,
}

Then, later, to emit the error:

ⓘ
sess.emit_err(ExampleError {
    name, ty, span, other_span, opt_sugg
});

See rustc dev guide for more examples on using the #[derive(Diagnostic)]: https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-structs.html