1
Fork 0

Rename IntoDiagnostic as Diagnostic.

To match `derive(Diagnostic)`.

Also rename `into_diagnostic` as `into_diag`.
This commit is contained in:
Nicholas Nethercote 2024-03-06 11:02:56 +11:00
parent a09b1d33a7
commit 7a294e998b
28 changed files with 146 additions and 164 deletions

View file

@ -112,39 +112,39 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
/// When implemented manually, it should be generic over the emission
/// guarantee, i.e.:
/// ```ignore (fragment)
/// impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for Foo { ... }
/// impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Foo { ... }
/// ```
/// rather than being specific:
/// ```ignore (fragment)
/// impl<'a> IntoDiagnostic<'a> for Bar { ... } // the default type param is `ErrorGuaranteed`
/// impl<'a> IntoDiagnostic<'a, ()> for Baz { ... }
/// 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_diagnostic` from outside. Even if in practice it 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 `IntoDiagnostic` impl.
/// rather than the `Diagnostic` impl.
/// - Derived impls are always generic, and it's good for the hand-written
/// impls to be consistent with them.
#[rustc_diagnostic_item = "IntoDiagnostic"]
pub trait IntoDiagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
#[rustc_diagnostic_item = "Diagnostic"]
pub trait Diagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
/// Write out as a diagnostic out of `DiagCtxt`.
#[must_use]
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G>;
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G>;
}
impl<'a, T, G> IntoDiagnostic<'a, G> for Spanned<T>
impl<'a, T, G> Diagnostic<'a, G> for Spanned<T>
where
T: IntoDiagnostic<'a, G>,
T: Diagnostic<'a, G>,
G: EmissionGuarantee,
{
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
self.node.into_diagnostic(dcx, level).with_span(self.span)
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
self.node.into_diag(dcx, level).with_span(self.span)
}
}
/// Converts a value of a type into a `DiagArg` (typically a field of an `IntoDiagnostic` struct).
/// Converts a value of a type into a `DiagArg` (typically a field of an `Diag` struct).
/// Implemented as a custom trait rather than `From` so that it is implemented on the type being
/// converted rather than on `DiagArgValue`, which enables types from other `rustc_*` crates to
/// implement this.
@ -482,7 +482,7 @@ pub struct Subdiag {
/// - The `EmissionGuarantee`, which determines the type returned from `emit`.
///
/// Each constructed `Diag` must be consumed by a function such as `emit`,
/// `cancel`, `delay_as_bug`, or `into_diagnostic`. A panic occurrs if a `Diag`
/// `cancel`, `delay_as_bug`, or `into_diag`. A panic occurrs if a `Diag`
/// is dropped without being consumed by one of these functions.
///
/// If there is some state in a downstream crate you would like to access in

View file

@ -1,7 +1,7 @@
use crate::diagnostic::DiagLocation;
use crate::{fluent_generated as fluent, AddToDiagnostic};
use crate::{
Diag, DiagArgValue, DiagCtxt, EmissionGuarantee, ErrCode, IntoDiagArg, IntoDiagnostic, Level,
Diag, DiagArgValue, DiagCtxt, Diagnostic, EmissionGuarantee, ErrCode, IntoDiagArg, Level,
SubdiagMessageOp,
};
use rustc_ast as ast;
@ -249,8 +249,8 @@ impl<Id> IntoDiagArg for hir::def::Res<Id> {
}
}
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetDataLayoutErrors<'_> {
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetDataLayoutErrors<'_> {
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
match self {
TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => {
Diag::new(dcx, level, fluent::errors_target_invalid_address_space)

View file

@ -38,7 +38,7 @@ extern crate self as rustc_errors;
pub use codes::*;
pub use diagnostic::{
AddToDiagnostic, BugAbort, DecorateLint, Diag, DiagArg, DiagArgMap, DiagArgName, DiagArgValue,
DiagInner, DiagStyledString, EmissionGuarantee, FatalAbort, IntoDiagArg, IntoDiagnostic,
DiagInner, DiagStyledString, Diagnostic, EmissionGuarantee, FatalAbort, IntoDiagArg,
StringPart, Subdiag, SubdiagMessageOp,
};
pub use diagnostic_impls::{
@ -1134,12 +1134,12 @@ impl DiagCtxt {
}
#[track_caller]
pub fn create_bug<'a>(&'a self, bug: impl IntoDiagnostic<'a, BugAbort>) -> Diag<'a, BugAbort> {
bug.into_diagnostic(self, Bug)
pub fn create_bug<'a>(&'a self, bug: impl Diagnostic<'a, BugAbort>) -> Diag<'a, BugAbort> {
bug.into_diag(self, Bug)
}
#[track_caller]
pub fn emit_bug<'a>(&'a self, bug: impl IntoDiagnostic<'a, BugAbort>) -> ! {
pub fn emit_bug<'a>(&'a self, bug: impl Diagnostic<'a, BugAbort>) -> ! {
self.create_bug(bug).emit()
}
@ -1174,29 +1174,26 @@ impl DiagCtxt {
#[track_caller]
pub fn create_fatal<'a>(
&'a self,
fatal: impl IntoDiagnostic<'a, FatalAbort>,
fatal: impl Diagnostic<'a, FatalAbort>,
) -> Diag<'a, FatalAbort> {
fatal.into_diagnostic(self, Fatal)
fatal.into_diag(self, Fatal)
}
#[track_caller]
pub fn emit_fatal<'a>(&'a self, fatal: impl IntoDiagnostic<'a, FatalAbort>) -> ! {
pub fn emit_fatal<'a>(&'a self, fatal: impl Diagnostic<'a, FatalAbort>) -> ! {
self.create_fatal(fatal).emit()
}
#[track_caller]
pub fn create_almost_fatal<'a>(
&'a self,
fatal: impl IntoDiagnostic<'a, FatalError>,
fatal: impl Diagnostic<'a, FatalError>,
) -> Diag<'a, FatalError> {
fatal.into_diagnostic(self, Fatal)
fatal.into_diag(self, Fatal)
}
#[track_caller]
pub fn emit_almost_fatal<'a>(
&'a self,
fatal: impl IntoDiagnostic<'a, FatalError>,
) -> FatalError {
pub fn emit_almost_fatal<'a>(&'a self, fatal: impl Diagnostic<'a, FatalError>) -> FatalError {
self.create_almost_fatal(fatal).emit()
}
@ -1234,12 +1231,12 @@ impl DiagCtxt {
}
#[track_caller]
pub fn create_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> Diag<'a> {
err.into_diagnostic(self, Error)
pub fn create_err<'a>(&'a self, err: impl Diagnostic<'a>) -> Diag<'a> {
err.into_diag(self, Error)
}
#[track_caller]
pub fn emit_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> ErrorGuaranteed {
pub fn emit_err<'a>(&'a self, err: impl Diagnostic<'a>) -> ErrorGuaranteed {
self.create_err(err).emit()
}
@ -1297,12 +1294,12 @@ impl DiagCtxt {
}
#[track_caller]
pub fn create_warn<'a>(&'a self, warning: impl IntoDiagnostic<'a, ()>) -> Diag<'a, ()> {
warning.into_diagnostic(self, Warning)
pub fn create_warn<'a>(&'a self, warning: impl Diagnostic<'a, ()>) -> Diag<'a, ()> {
warning.into_diag(self, Warning)
}
#[track_caller]
pub fn emit_warn<'a>(&'a self, warning: impl IntoDiagnostic<'a, ()>) {
pub fn emit_warn<'a>(&'a self, warning: impl Diagnostic<'a, ()>) {
self.create_warn(warning).emit()
}
@ -1335,12 +1332,12 @@ impl DiagCtxt {
}
#[track_caller]
pub fn create_note<'a>(&'a self, note: impl IntoDiagnostic<'a, ()>) -> Diag<'a, ()> {
note.into_diagnostic(self, Note)
pub fn create_note<'a>(&'a self, note: impl Diagnostic<'a, ()>) -> Diag<'a, ()> {
note.into_diag(self, Note)
}
#[track_caller]
pub fn emit_note<'a>(&'a self, note: impl IntoDiagnostic<'a, ()>) {
pub fn emit_note<'a>(&'a self, note: impl Diagnostic<'a, ()>) {
self.create_note(note).emit()
}