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

@ -1,9 +1,7 @@
use std::num::IntErrorKind; use std::num::IntErrorKind;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_errors::{ use rustc_errors::{codes::*, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level};
codes::*, Applicability, Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, Level,
};
use rustc_macros::Diagnostic; use rustc_macros::Diagnostic;
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
@ -50,8 +48,8 @@ pub(crate) struct UnknownMetaItem<'a> {
} }
// Manual implementation to be able to format `expected` items correctly. // Manual implementation to be able to format `expected` items correctly.
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UnknownMetaItem<'_> { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UnknownMetaItem<'_> {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
let expected = self.expected.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>(); let expected = self.expected.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
Diag::new(dcx, level, fluent::attr_unknown_meta_item) Diag::new(dcx, level, fluent::attr_unknown_meta_item)
.with_span(self.span) .with_span(self.span)
@ -203,8 +201,8 @@ pub(crate) struct UnsupportedLiteral {
pub start_point_span: Span, pub start_point_span: Span,
} }
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UnsupportedLiteral { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UnsupportedLiteral {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
let mut diag = Diag::new( let mut diag = Diag::new(
dcx, dcx,
level, level,

View File

@ -1,5 +1,5 @@
use rustc_errors::{ use rustc_errors::{
codes::*, AddToDiagnostic, Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, Level, MultiSpan, codes::*, AddToDiagnostic, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level, MultiSpan,
SingleLabelManySpans, SubdiagMessageOp, SingleLabelManySpans, SubdiagMessageOp,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
@ -425,9 +425,9 @@ pub(crate) struct EnvNotDefinedWithUserMessage {
} }
// Hand-written implementation to support custom user messages. // Hand-written implementation to support custom user messages.
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for EnvNotDefinedWithUserMessage { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for EnvNotDefinedWithUserMessage {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
#[expect( #[expect(
rustc::untranslatable_diagnostic, rustc::untranslatable_diagnostic,
reason = "cannot translate user-provided messages" reason = "cannot translate user-provided messages"
@ -785,8 +785,8 @@ pub(crate) struct AsmClobberNoReg {
pub(crate) clobbers: Vec<Span>, pub(crate) clobbers: Vec<Span>,
} }
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for AsmClobberNoReg { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AsmClobberNoReg {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
// eager translation as `span_labels` takes `AsRef<str>` // eager translation as `span_labels` takes `AsRef<str>`
let lbl1 = dcx.eagerly_translate_to_string( let lbl1 = dcx.eagerly_translate_to_string(
crate::fluent_generated::builtin_macros_asm_clobber_abi, crate::fluent_generated::builtin_macros_asm_clobber_abi,

View File

@ -1,4 +1,4 @@
use rustc_errors::{Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, Level}; use rustc_errors::{Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span; use rustc_span::Span;
@ -89,8 +89,8 @@ pub(crate) struct TargetFeatureDisableOrEnable<'a> {
#[help(codegen_gcc_missing_features)] #[help(codegen_gcc_missing_features)]
pub(crate) struct MissingFeatures; pub(crate) struct MissingFeatures;
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> {
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::codegen_gcc_target_feature_disable_or_enable); let mut diag = Diag::new(dcx, level, fluent::codegen_gcc_target_feature_disable_or_enable);
if let Some(span) = self.span { if let Some(span) = self.span {
diag.span(span); diag.span(span);

View File

@ -4,7 +4,7 @@ use std::path::Path;
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
use rustc_errors::{Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, Level}; use rustc_errors::{Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span; use rustc_span::Span;
@ -99,9 +99,9 @@ pub(crate) struct DynamicLinkingWithLTO;
pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>); pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>);
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for ParseTargetMachineConfig<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let diag: Diag<'_, G> = self.0.into_diagnostic(dcx, level); let diag: Diag<'_, G> = self.0.into_diag(dcx, level);
let (message, _) = diag.messages.first().expect("`LlvmError` with no message"); let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args.iter()); let message = dcx.eagerly_translate_to_string(message.clone(), diag.args.iter());
Diag::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config) Diag::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)
@ -119,8 +119,8 @@ pub(crate) struct TargetFeatureDisableOrEnable<'a> {
#[help(codegen_llvm_missing_features)] #[help(codegen_llvm_missing_features)]
pub(crate) struct MissingFeatures; pub(crate) struct MissingFeatures;
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> {
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::codegen_llvm_target_feature_disable_or_enable); let mut diag = Diag::new(dcx, level, fluent::codegen_llvm_target_feature_disable_or_enable);
if let Some(span) = self.span { if let Some(span) = self.span {
diag.span(span); diag.span(span);
@ -179,8 +179,8 @@ pub enum LlvmError<'a> {
pub(crate) struct WithLlvmError<'a>(pub LlvmError<'a>, pub String); pub(crate) struct WithLlvmError<'a>(pub LlvmError<'a>, pub String);
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for WithLlvmError<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for WithLlvmError<'_> {
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
use LlvmError::*; use LlvmError::*;
let msg_with_llvm_err = match &self.0 { let msg_with_llvm_err = match &self.0 {
WriteOutput { .. } => fluent::codegen_llvm_write_output_with_llvm_err, WriteOutput { .. } => fluent::codegen_llvm_write_output_with_llvm_err,
@ -198,7 +198,7 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for WithLlvmError<'_> {
ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err, ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
}; };
self.0 self.0
.into_diagnostic(dcx, level) .into_diag(dcx, level)
.with_primary_message(msg_with_llvm_err) .with_primary_message(msg_with_llvm_err)
.with_arg("llvm_err", self.1) .with_arg("llvm_err", self.1)
} }

View File

@ -4,7 +4,7 @@ use crate::assert_module_sources::CguReuse;
use crate::back::command::Command; use crate::back::command::Command;
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_errors::{ use rustc_errors::{
codes::*, Diag, DiagArgValue, DiagCtxt, EmissionGuarantee, IntoDiagArg, IntoDiagnostic, Level, codes::*, Diag, DiagArgValue, DiagCtxt, Diagnostic, EmissionGuarantee, IntoDiagArg, Level,
}; };
use rustc_macros::Diagnostic; use rustc_macros::Diagnostic;
use rustc_middle::ty::layout::LayoutError; use rustc_middle::ty::layout::LayoutError;
@ -214,8 +214,8 @@ pub enum LinkRlibError {
pub struct ThorinErrorWrapper(pub thorin::Error); pub struct ThorinErrorWrapper(pub thorin::Error);
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for ThorinErrorWrapper { impl<G: EmissionGuarantee> Diagnostic<'_, G> for ThorinErrorWrapper {
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
let build = |msg| Diag::new(dcx, level, msg); let build = |msg| Diag::new(dcx, level, msg);
match self.0 { match self.0 {
thorin::Error::ReadInput(_) => build(fluent::codegen_ssa_thorin_read_input_failure), thorin::Error::ReadInput(_) => build(fluent::codegen_ssa_thorin_read_input_failure),
@ -347,8 +347,8 @@ pub struct LinkingFailed<'a> {
pub escaped_output: String, pub escaped_output: String,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for LinkingFailed<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::codegen_ssa_linking_failed); let mut diag = Diag::new(dcx, level, fluent::codegen_ssa_linking_failed);
diag.arg("linker_path", format!("{}", self.linker_path.display())); diag.arg("linker_path", format!("{}", self.linker_path.display()));
diag.arg("exit_status", format!("{}", self.exit_status)); diag.arg("exit_status", format!("{}", self.exit_status));

View File

@ -1,6 +1,6 @@
use std::mem; use std::mem;
use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, IntoDiagArg, IntoDiagnostic}; use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, Diagnostic, IntoDiagArg};
use rustc_hir::CRATE_HIR_ID; use rustc_hir::CRATE_HIR_ID;
use rustc_middle::mir::AssertKind; use rustc_middle::mir::AssertKind;
use rustc_middle::query::TyCtxtAt; use rustc_middle::query::TyCtxtAt;
@ -130,7 +130,7 @@ pub(super) fn report<'tcx, C, F, E>(
where where
C: FnOnce() -> (Span, Vec<FrameNote>), C: FnOnce() -> (Span, Vec<FrameNote>),
F: FnOnce(Span, Vec<FrameNote>) -> E, F: FnOnce(Span, Vec<FrameNote>) -> E,
E: IntoDiagnostic<'tcx>, E: Diagnostic<'tcx>,
{ {
// Special handling for certain errors // Special handling for certain errors
match error { match error {

View File

@ -1,7 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use rustc_errors::{ use rustc_errors::{
codes::*, Diag, DiagArgValue, DiagCtxt, DiagMessage, EmissionGuarantee, IntoDiagnostic, Level, codes::*, Diag, DiagArgValue, DiagCtxt, DiagMessage, Diagnostic, EmissionGuarantee, Level,
}; };
use rustc_hir::ConstContext; use rustc_hir::ConstContext;
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
@ -858,8 +858,7 @@ impl<'tcx> ReportErrorExt for InvalidProgramInfo<'tcx> {
InvalidProgramInfo::Layout(e) => { InvalidProgramInfo::Layout(e) => {
// The level doesn't matter, `dummy_diag` is consumed without it being used. // The level doesn't matter, `dummy_diag` is consumed without it being used.
let dummy_level = Level::Bug; let dummy_level = Level::Bug;
let dummy_diag: Diag<'_, ()> = let dummy_diag: Diag<'_, ()> = e.into_diagnostic().into_diag(diag.dcx, dummy_level);
e.into_diagnostic().into_diagnostic(diag.dcx, dummy_level);
for (name, val) in dummy_diag.args.iter() { for (name, val) in dummy_diag.args.iter() {
diag.arg(name.clone(), val.clone()); diag.arg(name.clone(), val.clone());
} }

View File

@ -112,39 +112,39 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
/// When implemented manually, it should be generic over the emission /// When implemented manually, it should be generic over the emission
/// guarantee, i.e.: /// guarantee, i.e.:
/// ```ignore (fragment) /// ```ignore (fragment)
/// impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for Foo { ... } /// impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Foo { ... }
/// ``` /// ```
/// rather than being specific: /// rather than being specific:
/// ```ignore (fragment) /// ```ignore (fragment)
/// impl<'a> IntoDiagnostic<'a> for Bar { ... } // the default type param is `ErrorGuaranteed` /// impl<'a> Diagnostic<'a> for Bar { ... } // the default type param is `ErrorGuaranteed`
/// impl<'a> IntoDiagnostic<'a, ()> for Baz { ... } /// impl<'a> Diagnostic<'a, ()> for Baz { ... }
/// ``` /// ```
/// There are two reasons for this. /// There are two reasons for this.
/// - A diagnostic like `Foo` *could* be emitted at any level -- `level` is /// - 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 /// always emitted at a single level, we let the diagnostic creation/emission
/// site determine the level (by using `create_err`, `emit_warn`, etc.) /// 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 /// - Derived impls are always generic, and it's good for the hand-written
/// impls to be consistent with them. /// impls to be consistent with them.
#[rustc_diagnostic_item = "IntoDiagnostic"] #[rustc_diagnostic_item = "Diagnostic"]
pub trait IntoDiagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> { pub trait Diagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
/// Write out as a diagnostic out of `DiagCtxt`. /// Write out as a diagnostic out of `DiagCtxt`.
#[must_use] #[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 where
T: IntoDiagnostic<'a, G>, T: Diagnostic<'a, G>,
G: EmissionGuarantee, G: EmissionGuarantee,
{ {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
self.node.into_diagnostic(dcx, level).with_span(self.span) 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 /// 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 /// converted rather than on `DiagArgValue`, which enables types from other `rustc_*` crates to
/// implement this. /// implement this.
@ -482,7 +482,7 @@ pub struct Subdiag {
/// - The `EmissionGuarantee`, which determines the type returned from `emit`. /// - The `EmissionGuarantee`, which determines the type returned from `emit`.
/// ///
/// Each constructed `Diag` must be consumed by a function such as `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. /// 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 /// 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::diagnostic::DiagLocation;
use crate::{fluent_generated as fluent, AddToDiagnostic}; use crate::{fluent_generated as fluent, AddToDiagnostic};
use crate::{ use crate::{
Diag, DiagArgValue, DiagCtxt, EmissionGuarantee, ErrCode, IntoDiagArg, IntoDiagnostic, Level, Diag, DiagArgValue, DiagCtxt, Diagnostic, EmissionGuarantee, ErrCode, IntoDiagArg, Level,
SubdiagMessageOp, SubdiagMessageOp,
}; };
use rustc_ast as ast; use rustc_ast as ast;
@ -249,8 +249,8 @@ impl<Id> IntoDiagArg for hir::def::Res<Id> {
} }
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetDataLayoutErrors<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetDataLayoutErrors<'_> {
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
match self { match self {
TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => { TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => {
Diag::new(dcx, level, fluent::errors_target_invalid_address_space) 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 codes::*;
pub use diagnostic::{ pub use diagnostic::{
AddToDiagnostic, BugAbort, DecorateLint, Diag, DiagArg, DiagArgMap, DiagArgName, DiagArgValue, AddToDiagnostic, BugAbort, DecorateLint, Diag, DiagArg, DiagArgMap, DiagArgName, DiagArgValue,
DiagInner, DiagStyledString, EmissionGuarantee, FatalAbort, IntoDiagArg, IntoDiagnostic, DiagInner, DiagStyledString, Diagnostic, EmissionGuarantee, FatalAbort, IntoDiagArg,
StringPart, Subdiag, SubdiagMessageOp, StringPart, Subdiag, SubdiagMessageOp,
}; };
pub use diagnostic_impls::{ pub use diagnostic_impls::{
@ -1134,12 +1134,12 @@ impl DiagCtxt {
} }
#[track_caller] #[track_caller]
pub fn create_bug<'a>(&'a self, bug: impl IntoDiagnostic<'a, BugAbort>) -> Diag<'a, BugAbort> { pub fn create_bug<'a>(&'a self, bug: impl Diagnostic<'a, BugAbort>) -> Diag<'a, BugAbort> {
bug.into_diagnostic(self, Bug) bug.into_diag(self, Bug)
} }
#[track_caller] #[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() self.create_bug(bug).emit()
} }
@ -1174,29 +1174,26 @@ impl DiagCtxt {
#[track_caller] #[track_caller]
pub fn create_fatal<'a>( pub fn create_fatal<'a>(
&'a self, &'a self,
fatal: impl IntoDiagnostic<'a, FatalAbort>, fatal: impl Diagnostic<'a, FatalAbort>,
) -> Diag<'a, FatalAbort> { ) -> Diag<'a, FatalAbort> {
fatal.into_diagnostic(self, Fatal) fatal.into_diag(self, Fatal)
} }
#[track_caller] #[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() self.create_fatal(fatal).emit()
} }
#[track_caller] #[track_caller]
pub fn create_almost_fatal<'a>( pub fn create_almost_fatal<'a>(
&'a self, &'a self,
fatal: impl IntoDiagnostic<'a, FatalError>, fatal: impl Diagnostic<'a, FatalError>,
) -> Diag<'a, FatalError> { ) -> Diag<'a, FatalError> {
fatal.into_diagnostic(self, Fatal) fatal.into_diag(self, Fatal)
} }
#[track_caller] #[track_caller]
pub fn emit_almost_fatal<'a>( pub fn emit_almost_fatal<'a>(&'a self, fatal: impl Diagnostic<'a, FatalError>) -> FatalError {
&'a self,
fatal: impl IntoDiagnostic<'a, FatalError>,
) -> FatalError {
self.create_almost_fatal(fatal).emit() self.create_almost_fatal(fatal).emit()
} }
@ -1234,12 +1231,12 @@ impl DiagCtxt {
} }
#[track_caller] #[track_caller]
pub fn create_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> Diag<'a> { pub fn create_err<'a>(&'a self, err: impl Diagnostic<'a>) -> Diag<'a> {
err.into_diagnostic(self, Error) err.into_diag(self, Error)
} }
#[track_caller] #[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() self.create_err(err).emit()
} }
@ -1297,12 +1294,12 @@ impl DiagCtxt {
} }
#[track_caller] #[track_caller]
pub fn create_warn<'a>(&'a self, warning: impl IntoDiagnostic<'a, ()>) -> Diag<'a, ()> { pub fn create_warn<'a>(&'a self, warning: impl Diagnostic<'a, ()>) -> Diag<'a, ()> {
warning.into_diagnostic(self, Warning) warning.into_diag(self, Warning)
} }
#[track_caller] #[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() self.create_warn(warning).emit()
} }
@ -1335,12 +1332,12 @@ impl DiagCtxt {
} }
#[track_caller] #[track_caller]
pub fn create_note<'a>(&'a self, note: impl IntoDiagnostic<'a, ()>) -> Diag<'a, ()> { pub fn create_note<'a>(&'a self, note: impl Diagnostic<'a, ()>) -> Diag<'a, ()> {
note.into_diagnostic(self, Note) note.into_diag(self, Note)
} }
#[track_caller] #[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() self.create_note(note).emit()
} }

View File

@ -2,7 +2,7 @@
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_errors::{ use rustc_errors::{
codes::*, Applicability, Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, Level, MultiSpan, codes::*, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level, MultiSpan,
}; };
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
@ -355,10 +355,10 @@ pub struct MissingTypeParams {
pub empty_generic_args: bool, pub empty_generic_args: bool,
} }
// Manual implementation of `IntoDiagnostic` to be able to call `span_to_snippet`. // Manual implementation of `Diagnostic` to be able to call `span_to_snippet`.
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for MissingTypeParams { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
let mut err = Diag::new(dcx, level, fluent::hir_analysis_missing_type_params); let mut err = Diag::new(dcx, level, fluent::hir_analysis_missing_type_params);
err.span(self.span); err.span(self.span);
err.code(E0393); err.code(E0393);

View File

@ -186,7 +186,7 @@ lint_deprecated_lint_name =
.help = change it to {$replace} .help = change it to {$replace}
lint_diag_out_of_impl = lint_diag_out_of_impl =
diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls diagnostics should only be created in `Diagnostic`/`AddToDiagnostic` impls
lint_drop_glue = lint_drop_glue =
types that do not implement `Drop` can still have drop glue, consider instead using `{$needs_drop}` to detect whether a type is trivially dropped types that do not implement `Drop` can still have drop glue, consider instead using `{$needs_drop}` to detect whether a type is trivially dropped

View File

@ -351,7 +351,7 @@ declare_tool_lint! {
declare_tool_lint! { declare_tool_lint! {
/// The `diagnostic_outside_of_impl` lint detects calls to functions annotated with /// The `diagnostic_outside_of_impl` lint detects calls to functions annotated with
/// `#[rustc_lint_diagnostics]` that are outside an `IntoDiagnostic`, `AddToDiagnostic`, or /// `#[rustc_lint_diagnostics]` that are outside an `Diagnostic`, `AddToDiagnostic`, or
/// `DecorateLint` impl, or a `#[derive(Diagnostic)]`, `#[derive(Subdiagnostic)]`, /// `DecorateLint` impl, or a `#[derive(Diagnostic)]`, `#[derive(Subdiagnostic)]`,
/// `#[derive(DecorateLint)]` expansion. /// `#[derive(DecorateLint)]` expansion.
/// ///
@ -359,7 +359,7 @@ declare_tool_lint! {
/// [here](https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-structs.html). /// [here](https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-structs.html).
pub rustc::DIAGNOSTIC_OUTSIDE_OF_IMPL, pub rustc::DIAGNOSTIC_OUTSIDE_OF_IMPL,
Deny, Deny,
"prevent creation of diagnostics outside of `IntoDiagnostic`/`AddToDiagnostic` impls", "prevent creation of diagnostics outside of `Diagnostic`/`AddToDiagnostic` impls",
report_in_external_macro: true report_in_external_macro: true
} }
@ -455,7 +455,7 @@ impl LateLintPass<'_> for Diagnostics {
} }
// Calls to `#[rustc_lint_diagnostics]`-marked functions should only occur: // Calls to `#[rustc_lint_diagnostics]`-marked functions should only occur:
// - inside an impl of `IntoDiagnostic`, `AddToDiagnostic`, or `DecorateLint`, or // - inside an impl of `Diagnostic`, `AddToDiagnostic`, or `DecorateLint`, or
// - inside a parent function that is itself marked with `#[rustc_lint_diagnostics]`. // - inside a parent function that is itself marked with `#[rustc_lint_diagnostics]`.
// //
// Otherwise, emit a `DIAGNOSTIC_OUTSIDE_OF_IMPL` lint. // Otherwise, emit a `DIAGNOSTIC_OUTSIDE_OF_IMPL` lint.
@ -467,10 +467,7 @@ impl LateLintPass<'_> for Diagnostics {
&& let Impl { of_trait: Some(of_trait), .. } = impl_ && let Impl { of_trait: Some(of_trait), .. } = impl_
&& let Some(def_id) = of_trait.trait_def_id() && let Some(def_id) = of_trait.trait_def_id()
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id) && let Some(name) = cx.tcx.get_diagnostic_name(def_id)
&& matches!( && matches!(name, sym::Diagnostic | sym::AddToDiagnostic | sym::DecorateLint)
name,
sym::IntoDiagnostic | sym::AddToDiagnostic | sym::DecorateLint
)
{ {
is_inside_appropriate_impl = true; is_inside_appropriate_impl = true;
break; break;

View File

@ -10,7 +10,7 @@ use quote::quote;
use syn::spanned::Spanned; use syn::spanned::Spanned;
use synstructure::Structure; use synstructure::Structure;
/// The central struct for constructing the `into_diagnostic` method from an annotated struct. /// The central struct for constructing the `into_diag` method from an annotated struct.
pub(crate) struct DiagnosticDerive<'a> { pub(crate) struct DiagnosticDerive<'a> {
structure: Structure<'a>, structure: Structure<'a>,
} }
@ -72,14 +72,11 @@ impl<'a> DiagnosticDerive<'a> {
// A lifetime of `'a` causes conflicts, but `_sess` is fine. // A lifetime of `'a` causes conflicts, but `_sess` is fine.
let mut imp = structure.gen_impl(quote! { let mut imp = structure.gen_impl(quote! {
gen impl<'_sess, G> gen impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for @Self
rustc_errors::IntoDiagnostic<'_sess, G>
for @Self
where G: rustc_errors::EmissionGuarantee where G: rustc_errors::EmissionGuarantee
{ {
#[track_caller] #[track_caller]
fn into_diagnostic( fn into_diag(
self, self,
dcx: &'_sess rustc_errors::DiagCtxt, dcx: &'_sess rustc_errors::DiagCtxt,
level: rustc_errors::Level level: rustc_errors::Level

View File

@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use rustc_errors::{codes::*, Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, Level}; use rustc_errors::{codes::*, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::Diagnostic; use rustc_macros::Diagnostic;
use rustc_session::config; use rustc_session::config;
use rustc_span::{sym, Span, Symbol}; use rustc_span::{sym, Span, Symbol};
@ -495,8 +495,8 @@ pub(crate) struct MultipleCandidates {
pub candidates: Vec<PathBuf>, pub candidates: Vec<PathBuf>,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for MultipleCandidates { impl<G: EmissionGuarantee> Diagnostic<'_, G> for MultipleCandidates {
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::metadata_multiple_candidates); let mut diag = Diag::new(dcx, level, fluent::metadata_multiple_candidates);
diag.arg("crate_name", self.crate_name); diag.arg("crate_name", self.crate_name);
diag.arg("flavor", self.flavor); diag.arg("flavor", self.flavor);
@ -593,9 +593,9 @@ pub struct InvalidMetadataFiles {
pub crate_rejections: Vec<String>, pub crate_rejections: Vec<String>,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for InvalidMetadataFiles { impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidMetadataFiles {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::metadata_invalid_meta_files); let mut diag = Diag::new(dcx, level, fluent::metadata_invalid_meta_files);
diag.arg("crate_name", self.crate_name); diag.arg("crate_name", self.crate_name);
diag.arg("add_info", self.add_info); diag.arg("add_info", self.add_info);
@ -622,9 +622,9 @@ pub struct CannotFindCrate {
pub is_ui_testing: bool, pub is_ui_testing: bool,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for CannotFindCrate { impl<G: EmissionGuarantee> Diagnostic<'_, G> for CannotFindCrate {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::metadata_cannot_find_crate); let mut diag = Diag::new(dcx, level, fluent::metadata_cannot_find_crate);
diag.arg("crate_name", self.crate_name); diag.arg("crate_name", self.crate_name);
diag.arg("current_crate", self.current_crate); diag.arg("current_crate", self.current_crate);

View File

@ -5,7 +5,7 @@ use crate::ty::normalize_erasing_regions::NormalizationError;
use crate::ty::{self, ConstKind, Ty, TyCtxt, TypeVisitableExt}; use crate::ty::{self, ConstKind, Ty, TyCtxt, TypeVisitableExt};
use rustc_error_messages::DiagMessage; use rustc_error_messages::DiagMessage;
use rustc_errors::{ use rustc_errors::{
Diag, DiagArgValue, DiagCtxt, EmissionGuarantee, IntoDiagArg, IntoDiagnostic, Level, Diag, DiagArgValue, DiagCtxt, Diagnostic, EmissionGuarantee, IntoDiagArg, Level,
}; };
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
@ -1267,14 +1267,14 @@ pub enum FnAbiError<'tcx> {
AdjustForForeignAbi(call::AdjustForForeignAbiError), AdjustForForeignAbi(call::AdjustForForeignAbiError),
} }
impl<'a, 'b, G: EmissionGuarantee> IntoDiagnostic<'a, G> for FnAbiError<'b> { impl<'a, 'b, G: EmissionGuarantee> Diagnostic<'a, G> for FnAbiError<'b> {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
match self { match self {
Self::Layout(e) => e.into_diagnostic().into_diagnostic(dcx, level), Self::Layout(e) => e.into_diagnostic().into_diag(dcx, level),
Self::AdjustForForeignAbi(call::AdjustForForeignAbiError::Unsupported { Self::AdjustForForeignAbi(call::AdjustForForeignAbiError::Unsupported {
arch, arch,
abi, abi,
}) => UnsupportedFnAbi { arch, abi: abi.name() }.into_diagnostic(dcx, level), }) => UnsupportedFnAbi { arch, abi: abi.name() }.into_diag(dcx, level),
} }
} }
} }

View File

@ -1,8 +1,8 @@
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_errors::DiagArgValue; use rustc_errors::DiagArgValue;
use rustc_errors::{ use rustc_errors::{
codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level,
Level, MultiSpan, SubdiagMessageOp, MultiSpan, SubdiagMessageOp,
}; };
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty};
@ -461,10 +461,8 @@ pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> {
pub ty: Ty<'tcx>, pub ty: Ty<'tcx>,
} }
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> {
for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'_, G> {
{
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = let mut diag =
Diag::new(dcx, level, fluent::mir_build_non_exhaustive_patterns_type_not_empty); Diag::new(dcx, level, fluent::mir_build_non_exhaustive_patterns_type_not_empty);
diag.span(self.span); diag.span(self.span);

View File

@ -1,8 +1,8 @@
use std::borrow::Cow; use std::borrow::Cow;
use rustc_errors::{ use rustc_errors::{
codes::*, Applicability, DecorateLint, Diag, DiagArgValue, DiagCtxt, DiagMessage, codes::*, Applicability, DecorateLint, Diag, DiagArgValue, DiagCtxt, DiagMessage, Diagnostic,
EmissionGuarantee, IntoDiagnostic, Level, EmissionGuarantee, Level,
}; };
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::mir::{AssertKind, UnsafetyViolationDetails}; use rustc_middle::mir::{AssertKind, UnsafetyViolationDetails};
@ -62,9 +62,9 @@ pub(crate) struct RequiresUnsafe {
// so we need to eagerly translate the label here, which isn't supported by the derive API // so we need to eagerly translate the label here, which isn't supported by the derive API
// We could also exhaustively list out the primary messages for all unsafe violations, // We could also exhaustively list out the primary messages for all unsafe violations,
// but this would result in a lot of duplication. // but this would result in a lot of duplication.
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for RequiresUnsafe { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for RequiresUnsafe {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
let mut diag = Diag::new(dcx, level, fluent::mir_transform_requires_unsafe); let mut diag = Diag::new(dcx, level, fluent::mir_transform_requires_unsafe);
diag.code(E0133); diag.code(E0133);
diag.span(self.span); diag.span(self.span);

View File

@ -1,7 +1,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_errors::{Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, Level}; use rustc_errors::{Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::{Diagnostic, LintDiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic};
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
@ -46,9 +46,9 @@ pub struct UnusedGenericParamsHint {
pub param_names: Vec<String>, pub param_names: Vec<String>,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for UnusedGenericParamsHint { impl<G: EmissionGuarantee> Diagnostic<'_, G> for UnusedGenericParamsHint {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::monomorphize_unused_generic_params); let mut diag = Diag::new(dcx, level, fluent::monomorphize_unused_generic_params);
diag.span(self.span); diag.span(self.span);
for (span, name) in self.param_spans.into_iter().zip(self.param_names) { for (span, name) in self.param_spans.into_iter().zip(self.param_names) {

View File

@ -3,8 +3,8 @@ use std::borrow::Cow;
use rustc_ast::token::Token; use rustc_ast::token::Token;
use rustc_ast::{Path, Visibility}; use rustc_ast::{Path, Visibility};
use rustc_errors::{ use rustc_errors::{
codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level,
Level, SubdiagMessageOp, SubdiagMessageOp,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_session::errors::ExprParenthesesNeeded; use rustc_session::errors::ExprParenthesesNeeded;
@ -1065,9 +1065,9 @@ pub(crate) struct ExpectedIdentifier {
pub help_cannot_start_number: Option<HelpIdentifierStartsWithNumber>, pub help_cannot_start_number: Option<HelpIdentifierStartsWithNumber>,
} }
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedIdentifier {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
let token_descr = TokenDescription::from_token(&self.token); let token_descr = TokenDescription::from_token(&self.token);
let mut diag = Diag::new( let mut diag = Diag::new(
@ -1125,9 +1125,9 @@ pub(crate) struct ExpectedSemi {
pub sugg: ExpectedSemiSugg, pub sugg: ExpectedSemiSugg,
} }
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedSemi { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedSemi {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
let token_descr = TokenDescription::from_token(&self.token); let token_descr = TokenDescription::from_token(&self.token);
let mut diag = Diag::new( let mut diag = Diag::new(

View File

@ -6,8 +6,8 @@ use std::{
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_ast::Label; use rustc_ast::Label;
use rustc_errors::{ use rustc_errors::{
codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, DiagSymbolList, EmissionGuarantee, codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, DiagSymbolList, Diagnostic,
IntoDiagnostic, Level, MultiSpan, SubdiagMessageOp, EmissionGuarantee, Level, MultiSpan, SubdiagMessageOp,
}; };
use rustc_hir::{self as hir, ExprKind, Target}; use rustc_hir::{self as hir, ExprKind, Target};
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
@ -862,9 +862,9 @@ pub struct ItemFollowingInnerAttr {
pub kind: &'static str, pub kind: &'static str,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for InvalidAttrAtCrateLevel { impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidAttrAtCrateLevel {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::passes_invalid_attr_at_crate_level); let mut diag = Diag::new(dcx, level, fluent::passes_invalid_attr_at_crate_level);
diag.span(self.span); diag.span(self.span);
diag.arg("name", self.name); diag.arg("name", self.name);
@ -1012,9 +1012,9 @@ pub struct BreakNonLoop<'a> {
pub break_expr_span: Span, pub break_expr_span: Span,
} }
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'_, G> for BreakNonLoop<'a> { impl<'a, G: EmissionGuarantee> Diagnostic<'_, G> for BreakNonLoop<'a> {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::passes_break_non_loop); let mut diag = Diag::new(dcx, level, fluent::passes_break_non_loop);
diag.span(self.span); diag.span(self.span);
diag.code(E0571); diag.code(E0571);
@ -1156,9 +1156,9 @@ pub struct NakedFunctionsAsmBlock {
pub non_asms: Vec<Span>, pub non_asms: Vec<Span>,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for NakedFunctionsAsmBlock { impl<G: EmissionGuarantee> Diagnostic<'_, G> for NakedFunctionsAsmBlock {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::passes_naked_functions_asm_block); let mut diag = Diag::new(dcx, level, fluent::passes_naked_functions_asm_block);
diag.span(self.span); diag.span(self.span);
diag.code(E0787); diag.code(E0787);
@ -1267,9 +1267,9 @@ pub struct NoMainErr {
pub add_teach_note: bool, pub add_teach_note: bool,
} }
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for NoMainErr { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NoMainErr {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
let mut diag = Diag::new(dcx, level, fluent::passes_no_main_function); let mut diag = Diag::new(dcx, level, fluent::passes_no_main_function);
diag.span(DUMMY_SP); diag.span(DUMMY_SP);
diag.code(E0601); diag.code(E0601);
@ -1325,9 +1325,9 @@ pub struct DuplicateLangItem {
pub(crate) duplicate: Duplicate, pub(crate) duplicate: Duplicate,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for DuplicateLangItem { impl<G: EmissionGuarantee> Diagnostic<'_, G> for DuplicateLangItem {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new( let mut diag = Diag::new(
dcx, dcx,
level, level,

View File

@ -3,8 +3,8 @@ use std::num::NonZero;
use rustc_ast::token; use rustc_ast::token;
use rustc_ast::util::literal::LitError; use rustc_ast::util::literal::LitError;
use rustc_errors::{ use rustc_errors::{
codes::*, Diag, DiagCtxt, DiagMessage, EmissionGuarantee, ErrorGuaranteed, IntoDiagnostic, codes::*, Diag, DiagCtxt, DiagMessage, Diagnostic, EmissionGuarantee, ErrorGuaranteed, Level,
Level, MultiSpan, MultiSpan,
}; };
use rustc_macros::Diagnostic; use rustc_macros::Diagnostic;
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
@ -17,9 +17,9 @@ pub struct FeatureGateError {
pub explain: DiagMessage, pub explain: DiagMessage,
} }
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for FeatureGateError { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for FeatureGateError {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
Diag::new(dcx, level, self.explain).with_span(self.span).with_code(E0658) Diag::new(dcx, level, self.explain).with_span(self.span).with_code(E0658)
} }
} }

View File

@ -22,8 +22,8 @@ use rustc_errors::emitter::{stderr_destination, DynEmitter, HumanEmitter, HumanR
use rustc_errors::json::JsonEmitter; use rustc_errors::json::JsonEmitter;
use rustc_errors::registry::Registry; use rustc_errors::registry::Registry;
use rustc_errors::{ use rustc_errors::{
codes::*, fallback_fluent_bundle, Diag, DiagCtxt, DiagMessage, ErrorGuaranteed, FatalAbort, codes::*, fallback_fluent_bundle, Diag, DiagCtxt, DiagMessage, Diagnostic, ErrorGuaranteed,
FluentBundle, IntoDiagnostic, LazyFallbackBundle, TerminalUrl, FatalAbort, FluentBundle, LazyFallbackBundle, TerminalUrl,
}; };
use rustc_macros::HashStable_Generic; use rustc_macros::HashStable_Generic;
pub use rustc_span::def_id::StableCrateId; pub use rustc_span::def_id::StableCrateId;
@ -305,11 +305,7 @@ impl Session {
} }
#[track_caller] #[track_caller]
pub fn create_feature_err<'a>( pub fn create_feature_err<'a>(&'a self, err: impl Diagnostic<'a>, feature: Symbol) -> Diag<'a> {
&'a self,
err: impl IntoDiagnostic<'a>,
feature: Symbol,
) -> Diag<'a> {
let mut err = self.dcx().create_err(err); let mut err = self.dcx().create_err(err);
if err.code.is_none() { if err.code.is_none() {
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]

View File

@ -188,6 +188,7 @@ symbols! {
Default, Default,
Deref, Deref,
DiagMessage, DiagMessage,
Diagnostic,
DirBuilder, DirBuilder,
Display, Display,
DoubleEndedIterator, DoubleEndedIterator,
@ -223,7 +224,6 @@ symbols! {
Input, Input,
Instant, Instant,
Into, Into,
IntoDiagnostic,
IntoFuture, IntoFuture,
IntoIterator, IntoIterator,
IoLines, IoLines,

View File

@ -1,6 +1,6 @@
//! Errors emitted by symbol_mangling. //! Errors emitted by symbol_mangling.
use rustc_errors::{Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, Level}; use rustc_errors::{Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level};
use rustc_span::Span; use rustc_span::Span;
use std::fmt; use std::fmt;
@ -13,8 +13,8 @@ pub struct TestOutput {
// This diagnostic doesn't need translation because (a) it doesn't contain any // This diagnostic doesn't need translation because (a) it doesn't contain any
// natural language, and (b) it's only used in tests. So we construct it // natural language, and (b) it's only used in tests. So we construct it
// manually and avoid the fluent machinery. // manually and avoid the fluent machinery.
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TestOutput { impl<G: EmissionGuarantee> Diagnostic<'_, G> for TestOutput {
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> {
let TestOutput { span, kind, content } = self; let TestOutput { span, kind, content } = self;
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]

View File

@ -1,7 +1,7 @@
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_errors::{ use rustc_errors::{
codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, EmissionGuarantee, IntoDiagnostic, codes::*, AddToDiagnostic, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level,
Level, SubdiagMessageOp, SubdiagMessageOp,
}; };
use rustc_macros::Diagnostic; use rustc_macros::Diagnostic;
use rustc_middle::ty::{self, ClosureKind, PolyTraitRef, Ty}; use rustc_middle::ty::{self, ClosureKind, PolyTraitRef, Ty};
@ -57,9 +57,9 @@ pub struct NegativePositiveConflict<'tcx> {
pub positive_impl_span: Result<Span, Symbol>, pub positive_impl_span: Result<Span, Symbol>,
} }
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for NegativePositiveConflict<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for NegativePositiveConflict<'_> {
#[track_caller] #[track_caller]
fn into_diagnostic(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(dcx, level, fluent::trait_selection_negative_positive_conflict); let mut diag = Diag::new(dcx, level, fluent::trait_selection_negative_positive_conflict);
diag.arg("trait_desc", self.trait_desc.print_only_trait_path().to_string()); diag.arg("trait_desc", self.trait_desc.print_only_trait_path().to_string());
diag.arg("self_desc", self.self_ty.map_or_else(|| "none".to_string(), |ty| ty.to_string())); diag.arg("self_desc", self.self_ty.map_or_else(|| "none".to_string(), |ty| ty.to_string()));

View File

@ -15,7 +15,7 @@ extern crate rustc_span;
use rustc_errors::{ use rustc_errors::{
AddToDiagnostic, DecorateLint, Diag, DiagCtxt, DiagInner, DiagMessage, EmissionGuarantee, AddToDiagnostic, DecorateLint, Diag, DiagCtxt, DiagInner, DiagMessage, EmissionGuarantee,
IntoDiagnostic, Level, SubdiagMessageOp, Diagnostic, Level, SubdiagMessageOp,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span; use rustc_span::Span;
@ -36,19 +36,19 @@ struct Note {
span: Span, span: Span,
} }
pub struct UntranslatableInIntoDiagnostic; pub struct UntranslatableInDiagnostic;
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UntranslatableInIntoDiagnostic { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UntranslatableInDiagnostic {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
Diag::new(dcx, level, "untranslatable diagnostic") Diag::new(dcx, level, "untranslatable diagnostic")
//~^ ERROR diagnostics should be created using translatable messages //~^ ERROR diagnostics should be created using translatable messages
} }
} }
pub struct TranslatableInIntoDiagnostic; pub struct TranslatableInDiagnostic;
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for TranslatableInIntoDiagnostic { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for TranslatableInDiagnostic {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
Diag::new(dcx, level, crate::fluent_generated::no_crate_example) Diag::new(dcx, level, crate::fluent_generated::no_crate_example)
} }
} }
@ -105,10 +105,10 @@ impl<'a> DecorateLint<'a, ()> for TranslatableInDecorateLint {
pub fn make_diagnostics<'a>(dcx: &'a DiagCtxt) { pub fn make_diagnostics<'a>(dcx: &'a DiagCtxt) {
let _diag = dcx.struct_err(crate::fluent_generated::no_crate_example); let _diag = dcx.struct_err(crate::fluent_generated::no_crate_example);
//~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls //~^ ERROR diagnostics should only be created in `Diagnostic`/`AddToDiagnostic` impls
let _diag = dcx.struct_err("untranslatable diagnostic"); let _diag = dcx.struct_err("untranslatable diagnostic");
//~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls //~^ ERROR diagnostics should only be created in `Diagnostic`/`AddToDiagnostic` impls
//~^^ ERROR diagnostics should be created using translatable messages //~^^ ERROR diagnostics should be created using translatable messages
} }

View File

@ -22,7 +22,7 @@ error: diagnostics should be created using translatable messages
LL | diag.note("untranslatable diagnostic"); LL | diag.note("untranslatable diagnostic");
| ^^^^ | ^^^^
error: diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls error: diagnostics should only be created in `Diagnostic`/`AddToDiagnostic` impls
--> $DIR/diagnostics.rs:107:21 --> $DIR/diagnostics.rs:107:21
| |
LL | let _diag = dcx.struct_err(crate::fluent_generated::no_crate_example); LL | let _diag = dcx.struct_err(crate::fluent_generated::no_crate_example);
@ -34,7 +34,7 @@ note: the lint level is defined here
LL | #![deny(rustc::diagnostic_outside_of_impl)] LL | #![deny(rustc::diagnostic_outside_of_impl)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls error: diagnostics should only be created in `Diagnostic`/`AddToDiagnostic` impls
--> $DIR/diagnostics.rs:110:21 --> $DIR/diagnostics.rs:110:21
| |
LL | let _diag = dcx.struct_err("untranslatable diagnostic"); LL | let _diag = dcx.struct_err("untranslatable diagnostic");