Skip to content

Commit 3fbdec4

Browse files
committed
Add a comment about how IntoDiagnostic should be impl'd.
1 parent c475e23 commit 3fbdec4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

compiler/rustc_errors/src/diagnostic.rs

+19
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
108108

109109
/// Trait implemented by error types. This is rarely implemented manually. Instead, use
110110
/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
111+
///
112+
/// When implemented manually, it should be generic over the emission
113+
/// guarantee, i.e.:
114+
/// ```ignore (fragment)
115+
/// impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for Foo { ... }
116+
/// ```
117+
/// rather than being specific:
118+
/// ```ignore (fragment)
119+
/// impl<'a> IntoDiagnostic<'a> for Bar { ... } // the default type param is `ErrorGuaranteed`
120+
/// impl<'a> IntoDiagnostic<'a, ()> for Baz { ... }
121+
/// ```
122+
/// There are two reasons for this.
123+
/// - A diagnostic like `Foo` *could* be emitted at any level -- `level` is
124+
/// passed in to `into_diagnostic` from outside. Even if in practice it is
125+
/// always emitted at a single level, we let the diagnostic creation/emission
126+
/// site determine the level (by using `create_err`, `emit_warn`, etc.)
127+
/// rather than the `IntoDiagnostic` impl.
128+
/// - Derived impls are always generic, and it's good for the hand-written
129+
/// impls to be consistent with them.
111130
#[rustc_diagnostic_item = "IntoDiagnostic"]
112131
pub trait IntoDiagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
113132
/// Write out as a diagnostic out of `DiagCtxt`.

0 commit comments

Comments
 (0)