Skip to main content

## Diagnostic

# Trait Diagnostic

Source

    pub trait Diagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
        // Required method
        fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G>;
    }

Expand description

Trait implemented by error types. This is rarely implemented manually. Instead, use `#[derive(Diagnostic)]` – see [rustc_macros::Diagnostic].

When implemented manually, it should be generic over the emission guarantee, i.e.:

ⓘ

    impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Foo { ... }

rather than being specific:

ⓘ

    impl<'a> Diagnostic<'a> for Bar { ... }  // the default type param is `ErrorGuaranteed`
    impl<'a> Diagnostic<'a, ()> for Baz { ... }

There are two reasons for this.

- A diagnostic like `Foo` could be emitted at any level – `level` is passed in to `into_diag` from outside. Even if in practice it is always emitted at a single level, we let the diagnostic creation/emission site determine the level (by using `create_err`, `emit_warn`, etc.) rather than the `Diagnostic` impl.
- Derived impls are always generic, and it’s good for the hand-written impls to be consistent with them.

## Required Methods§

Source

#### fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G>

Write out as a diagnostic out of `DiagCtxt`.

## Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

## Implementations on Foreign Types§

Source§

### impl<'a, T, G> Diagnostic<'a, G> for Spanned<T>
where T: Diagnostic<'a, G>, G: EmissionGuarantee,

Source§

#### fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G>

Source§

### impl<'a> Diagnostic<'a, ()> for Box<dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSync + DynSend + 'static>

Source§

#### fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()>

## Implementors§

Source§

### impl<'_sess, G> Diagnostic<'_sess, G> for UnusedDuplicate
where G: EmissionGuarantee,

Source§

### impl<'a, F: FnOnce(&mut Diag<'_, ()>)> Diagnostic<'a, ()> for DiagDecorator<F>