Use a dedicated type instead of a reference for the diagnostic context

This paves the way for tracking more state (e.g. error tainting) in the diagnostic context handle
This commit is contained in:
Oli Scherer 2024-06-18 10:35:56 +00:00
parent c91edc3888
commit 7ba82d61eb
77 changed files with 363 additions and 328 deletions

View File

@ -50,7 +50,7 @@ use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::sorted_map::SortedMap; use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::{DiagArgFromDisplay, DiagCtxt, StashKey}; use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res}; use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{self as hir}; use rustc_hir::{self as hir};
@ -188,7 +188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} }
} }
pub(crate) fn dcx(&self) -> &'hir DiagCtxt { pub(crate) fn dcx(&self) -> DiagCtxtHandle<'hir> {
self.tcx.dcx() self.tcx.dcx()
} }
} }

View File

@ -12,6 +12,7 @@ use rustc_ast::visit::{walk_list, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor}
use rustc_ast::*; use rustc_ast::*;
use rustc_ast_pretty::pprust::{self, State}; use rustc_ast_pretty::pprust::{self, State};
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::DiagCtxtHandle;
use rustc_feature::Features; use rustc_feature::Features;
use rustc_parse::validate_attr; use rustc_parse::validate_attr;
use rustc_session::lint::builtin::{ use rustc_session::lint::builtin::{
@ -269,7 +270,7 @@ impl<'a> AstValidator<'a> {
} }
} }
fn dcx(&self) -> &rustc_errors::DiagCtxt { fn dcx(&self) -> DiagCtxtHandle<'a> {
self.session.dcx() self.session.dcx()
} }
@ -809,11 +810,7 @@ impl<'a> AstValidator<'a> {
/// Checks that generic parameters are in the correct order, /// Checks that generic parameters are in the correct order,
/// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`) /// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`)
fn validate_generic_param_order( fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericParam], span: Span) {
dcx: &rustc_errors::DiagCtxt,
generics: &[GenericParam],
span: Span,
) {
let mut max_param: Option<ParamKindOrd> = None; let mut max_param: Option<ParamKindOrd> = None;
let mut out_of_order = FxIndexMap::default(); let mut out_of_order = FxIndexMap::default();
let mut param_idents = Vec::with_capacity(generics.len()); let mut param_idents = Vec::with_capacity(generics.len());

View File

@ -8,6 +8,7 @@ use std::str::FromStr;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::visit; use rustc_ast::visit;
use rustc_ast::visit::Visitor; use rustc_ast::visit::Visitor;
use rustc_errors::DiagCtxtHandle;
use crate::errors; use crate::errors;
@ -31,7 +32,7 @@ impl FromStr for Mode {
} }
struct ShowSpanVisitor<'a> { struct ShowSpanVisitor<'a> {
dcx: &'a rustc_errors::DiagCtxt, dcx: DiagCtxtHandle<'a>,
mode: Mode, mode: Mode,
} }
@ -58,7 +59,7 @@ impl<'a> Visitor<'a> for ShowSpanVisitor<'a> {
} }
} }
pub fn run(dcx: &rustc_errors::DiagCtxt, mode: &str, krate: &ast::Crate) { pub fn run(dcx: DiagCtxtHandle<'_>, mode: &str, krate: &ast::Crate) {
let Ok(mode) = mode.parse() else { let Ok(mode) = mode.parse() else {
return; return;
}; };

View File

@ -1,7 +1,8 @@
use std::num::IntErrorKind; use std::num::IntErrorKind;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_errors::{codes::*, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level}; use rustc_errors::DiagCtxtHandle;
use rustc_errors::{codes::*, Applicability, Diag, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
@ -49,7 +50,7 @@ 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> Diagnostic<'a, G> for UnknownMetaItem<'_> { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UnknownMetaItem<'_> {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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)
@ -202,7 +203,7 @@ pub(crate) struct UnsupportedLiteral {
} }
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UnsupportedLiteral { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UnsupportedLiteral {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
let mut diag = Diag::new( let mut diag = Diag::new(
dcx, dcx,
level, level,

View File

@ -1,13 +1,13 @@
#![allow(rustc::diagnostic_outside_of_impl)] #![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)] #![allow(rustc::untranslatable_diagnostic)]
use rustc_errors::{codes::*, struct_span_code_err, Diag, DiagCtxt}; use rustc_errors::{codes::*, struct_span_code_err, Diag, DiagCtxtHandle};
use rustc_middle::span_bug; use rustc_middle::span_bug;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span; use rustc_span::Span;
impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> { impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
pub fn dcx(&self) -> &'tcx DiagCtxt { pub fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.infcx.dcx() self.infcx.dcx()
} }

View File

@ -4,8 +4,8 @@ use crate::session_diagnostics::{
CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause, CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
CaptureVarKind, CaptureVarPathUseCause, OnClosureNote, CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
}; };
use rustc_errors::{Applicability, Diag}; use rustc_errors::MultiSpan;
use rustc_errors::{DiagCtxt, MultiSpan}; use rustc_errors::{Applicability, Diag, DiagCtxtHandle};
use rustc_hir::def::{CtorKind, Namespace}; use rustc_hir::def::{CtorKind, Namespace};
use rustc_hir::CoroutineKind; use rustc_hir::CoroutineKind;
use rustc_hir::{self as hir, LangItem}; use rustc_hir::{self as hir, LangItem};
@ -593,7 +593,7 @@ impl UseSpans<'_> {
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub(super) fn args_subdiag( pub(super) fn args_subdiag(
self, self,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
err: &mut Diag<'_>, err: &mut Diag<'_>,
f: impl FnOnce(Span) -> CaptureArgLabel, f: impl FnOnce(Span) -> CaptureArgLabel,
) { ) {
@ -607,7 +607,7 @@ impl UseSpans<'_> {
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub(super) fn var_path_only_subdiag( pub(super) fn var_path_only_subdiag(
self, self,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
err: &mut Diag<'_>, err: &mut Diag<'_>,
action: crate::InitializationRequiringAction, action: crate::InitializationRequiringAction,
) { ) {
@ -645,7 +645,7 @@ impl UseSpans<'_> {
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub(super) fn var_subdiag( pub(super) fn var_subdiag(
self, self,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
err: &mut Diag<'_>, err: &mut Diag<'_>,
kind: Option<rustc_middle::mir::BorrowKind>, kind: Option<rustc_middle::mir::BorrowKind>,
f: impl FnOnce(hir::ClosureKind, Span) -> CaptureVarCause, f: impl FnOnce(hir::ClosureKind, Span) -> CaptureVarCause,

View File

@ -1,5 +1,5 @@
use rustc_errors::{ use rustc_errors::{
codes::*, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level, MultiSpan, codes::*, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, MultiSpan,
SingleLabelManySpans, SubdiagMessageOp, Subdiagnostic, SingleLabelManySpans, SubdiagMessageOp, Subdiagnostic,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
@ -434,7 +434,7 @@ pub(crate) struct EnvNotDefinedWithUserMessage {
// Hand-written implementation to support custom user messages. // Hand-written implementation to support custom user messages.
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for EnvNotDefinedWithUserMessage { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for EnvNotDefinedWithUserMessage {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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"
@ -801,7 +801,7 @@ pub(crate) struct AsmClobberNoReg {
} }
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AsmClobberNoReg { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AsmClobberNoReg {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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

@ -3,6 +3,7 @@ use rustc_ast::ptr::P;
use rustc_ast::visit::{self, Visitor}; use rustc_ast::visit::{self, Visitor};
use rustc_ast::{self as ast, attr, NodeId}; use rustc_ast::{self as ast, attr, NodeId};
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_errors::DiagCtxtHandle;
use rustc_expand::base::{parse_macro_name_and_helper_attrs, ExtCtxt, ResolverExpand}; use rustc_expand::base::{parse_macro_name_and_helper_attrs, ExtCtxt, ResolverExpand};
use rustc_expand::expand::{AstFragment, ExpansionConfig}; use rustc_expand::expand::{AstFragment, ExpansionConfig};
use rustc_feature::Features; use rustc_feature::Features;
@ -38,7 +39,7 @@ enum ProcMacro {
struct CollectProcMacros<'a> { struct CollectProcMacros<'a> {
macros: Vec<ProcMacro>, macros: Vec<ProcMacro>,
in_root: bool, in_root: bool,
dcx: &'a rustc_errors::DiagCtxt, dcx: DiagCtxtHandle<'a>,
source_map: &'a SourceMap, source_map: &'a SourceMap,
is_proc_macro_crate: bool, is_proc_macro_crate: bool,
is_test_crate: bool, is_test_crate: bool,
@ -52,7 +53,7 @@ pub fn inject(
is_proc_macro_crate: bool, is_proc_macro_crate: bool,
has_proc_macro_decls: bool, has_proc_macro_decls: bool,
is_test_crate: bool, is_test_crate: bool,
dcx: &rustc_errors::DiagCtxt, dcx: DiagCtxtHandle<'_>,
) { ) {
let ecfg = ExpansionConfig::default("proc_macro".to_string(), features); let ecfg = ExpansionConfig::default("proc_macro".to_string(), features);
let mut cx = ExtCtxt::new(sess, ecfg, resolver, None); let mut cx = ExtCtxt::new(sess, ecfg, resolver, None);

View File

@ -6,6 +6,7 @@ use rustc_ast::mut_visit::*;
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::visit::{walk_item, Visitor}; use rustc_ast::visit::{walk_item, Visitor};
use rustc_ast::{attr, ModKind}; use rustc_ast::{attr, ModKind};
use rustc_errors::DiagCtxtHandle;
use rustc_expand::base::{ExtCtxt, ResolverExpand}; use rustc_expand::base::{ExtCtxt, ResolverExpand};
use rustc_expand::expand::{AstFragment, ExpansionConfig}; use rustc_expand::expand::{AstFragment, ExpansionConfig};
use rustc_feature::Features; use rustc_feature::Features;
@ -391,7 +392,7 @@ fn get_test_name(i: &ast::Item) -> Option<Symbol> {
attr::first_attr_value_str_by_name(&i.attrs, sym::rustc_test_marker) attr::first_attr_value_str_by_name(&i.attrs, sym::rustc_test_marker)
} }
fn get_test_runner(dcx: &rustc_errors::DiagCtxt, krate: &ast::Crate) -> Option<ast::Path> { fn get_test_runner(dcx: DiagCtxtHandle<'_>, krate: &ast::Crate) -> Option<ast::Path> {
let test_attr = attr::find_by_name(&krate.attrs, sym::test_runner)?; let test_attr = attr::find_by_name(&krate.attrs, sym::test_runner)?;
let meta_list = test_attr.meta_item_list()?; let meta_list = test_attr.meta_item_list()?;
let span = test_attr.span; let span = test_attr.span;

View File

@ -1,6 +1,7 @@
use std::sync::{Arc, Condvar, Mutex}; use std::sync::{Arc, Condvar, Mutex};
use jobserver::HelperThread; use jobserver::HelperThread;
use rustc_errors::DiagCtxtHandle;
use rustc_session::Session; use rustc_session::Session;
// FIXME don't panic when a worker thread panics // FIXME don't panic when a worker thread panics
@ -46,7 +47,7 @@ impl ConcurrencyLimiter {
} }
} }
pub(super) fn acquire(&self, dcx: &rustc_errors::DiagCtxt) -> ConcurrencyLimiterToken { pub(super) fn acquire(&self, dcx: DiagCtxtHandle<'_>) -> ConcurrencyLimiterToken {
let mut state = self.state.lock().unwrap(); let mut state = self.state.lock().unwrap();
loop { loop {
state.assert_invariants(); state.assert_invariants();

View File

@ -28,7 +28,7 @@ use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind}; use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind};
use rustc_data_structures::memmap::Mmap; use rustc_data_structures::memmap::Mmap;
use rustc_errors::{DiagCtxt, FatalError}; use rustc_errors::{DiagCtxtHandle, FatalError};
use rustc_hir::def_id::LOCAL_CRATE; use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::dep_graph::WorkProduct; use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel}; use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
@ -59,7 +59,7 @@ struct LtoData {
fn prepare_lto( fn prepare_lto(
cgcx: &CodegenContext<GccCodegenBackend>, cgcx: &CodegenContext<GccCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
) -> Result<LtoData, FatalError> { ) -> Result<LtoData, FatalError> {
let export_threshold = match cgcx.lto { let export_threshold = match cgcx.lto {
// We're just doing LTO for our one crate // We're just doing LTO for our one crate
@ -179,12 +179,13 @@ pub(crate) fn run_fat(
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>, cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
) -> Result<LtoModuleCodegen<GccCodegenBackend>, FatalError> { ) -> Result<LtoModuleCodegen<GccCodegenBackend>, FatalError> {
let dcx = cgcx.create_dcx(); let dcx = cgcx.create_dcx();
let lto_data = prepare_lto(cgcx, &dcx)?; let dcx = dcx.handle();
let lto_data = prepare_lto(cgcx, dcx)?;
/*let symbols_below_threshold = /*let symbols_below_threshold =
lto_data.symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();*/ lto_data.symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();*/
fat_lto( fat_lto(
cgcx, cgcx,
&dcx, dcx,
modules, modules,
cached_modules, cached_modules,
lto_data.upstream_modules, lto_data.upstream_modules,
@ -195,7 +196,7 @@ pub(crate) fn run_fat(
fn fat_lto( fn fat_lto(
cgcx: &CodegenContext<GccCodegenBackend>, cgcx: &CodegenContext<GccCodegenBackend>,
_dcx: &DiagCtxt, _dcx: DiagCtxtHandle<'_>,
modules: Vec<FatLtoInput<GccCodegenBackend>>, modules: Vec<FatLtoInput<GccCodegenBackend>>,
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>, cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>, mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,

View File

@ -4,7 +4,7 @@ use gccjit::OutputKind;
use rustc_codegen_ssa::back::link::ensure_removed; use rustc_codegen_ssa::back::link::ensure_removed;
use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig}; use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig};
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen}; use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
use rustc_errors::DiagCtxt; use rustc_errors::DiagCtxtHandle;
use rustc_fs_util::link_or_copy; use rustc_fs_util::link_or_copy;
use rustc_session::config::OutputType; use rustc_session::config::OutputType;
use rustc_span::fatal_error::FatalError; use rustc_span::fatal_error::FatalError;
@ -15,7 +15,7 @@ use crate::{GccCodegenBackend, GccContext};
pub(crate) unsafe fn codegen( pub(crate) unsafe fn codegen(
cgcx: &CodegenContext<GccCodegenBackend>, cgcx: &CodegenContext<GccCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: ModuleCodegen<GccContext>, module: ModuleCodegen<GccContext>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<CompiledModule, FatalError> { ) -> Result<CompiledModule, FatalError> {
@ -166,7 +166,7 @@ pub(crate) unsafe fn codegen(
pub(crate) fn link( pub(crate) fn link(
_cgcx: &CodegenContext<GccCodegenBackend>, _cgcx: &CodegenContext<GccCodegenBackend>,
_dcx: &DiagCtxt, _dcx: DiagCtxtHandle<'_>,
mut _modules: Vec<ModuleCodegen<GccContext>>, mut _modules: Vec<ModuleCodegen<GccContext>>,
) -> Result<ModuleCodegen<GccContext>, FatalError> { ) -> Result<ModuleCodegen<GccContext>, FatalError> {
unimplemented!(); unimplemented!();

View File

@ -1,4 +1,4 @@
use rustc_errors::{Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level}; use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span; use rustc_span::Span;
@ -90,7 +90,7 @@ pub(crate) struct TargetFeatureDisableOrEnable<'a> {
pub(crate) struct MissingFeatures; pub(crate) struct MissingFeatures;
impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> {
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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

@ -16,13 +16,7 @@
#![allow(internal_features)] #![allow(internal_features)]
#![doc(rust_logo)] #![doc(rust_logo)]
#![feature(rustdoc_internals)] #![feature(rustdoc_internals)]
#![feature( #![feature(rustc_private, decl_macro, never_type, trusted_len, hash_raw_entry)]
rustc_private,
decl_macro,
never_type,
trusted_len,
hash_raw_entry
)]
#![allow(broken_intra_doc_links)] #![allow(broken_intra_doc_links)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#![warn(rust_2018_idioms)] #![warn(rust_2018_idioms)]
@ -104,7 +98,7 @@ use rustc_codegen_ssa::traits::{
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen}; use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::IntoDynSyncSend; use rustc_data_structures::sync::IntoDynSyncSend;
use rustc_errors::{DiagCtxt, ErrorGuaranteed}; use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
use rustc_metadata::EncodedMetadata; use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
@ -386,7 +380,7 @@ impl WriteBackendMethods for GccCodegenBackend {
unsafe fn optimize( unsafe fn optimize(
_cgcx: &CodegenContext<Self>, _cgcx: &CodegenContext<Self>,
_dcx: &DiagCtxt, _dcx: DiagCtxtHandle<'_>,
module: &ModuleCodegen<Self::Module>, module: &ModuleCodegen<Self::Module>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<(), FatalError> { ) -> Result<(), FatalError> {
@ -411,14 +405,17 @@ impl WriteBackendMethods for GccCodegenBackend {
unsafe fn codegen( unsafe fn codegen(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: ModuleCodegen<Self::Module>, module: ModuleCodegen<Self::Module>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<CompiledModule, FatalError> { ) -> Result<CompiledModule, FatalError> {
back::write::codegen(cgcx, dcx, module, config) back::write::codegen(cgcx, dcx, module, config)
} }
fn prepare_thin(_module: ModuleCodegen<Self::Module>, _emit_summary: bool) -> (String, Self::ThinBuffer) { fn prepare_thin(
_module: ModuleCodegen<Self::Module>,
_emit_summary: bool,
) -> (String, Self::ThinBuffer) {
unimplemented!(); unimplemented!();
} }
@ -428,7 +425,7 @@ impl WriteBackendMethods for GccCodegenBackend {
fn run_link( fn run_link(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
modules: Vec<ModuleCodegen<Self::Module>>, modules: Vec<ModuleCodegen<Self::Module>>,
) -> Result<ModuleCodegen<Self::Module>, FatalError> { ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
back::write::link(cgcx, dcx, modules) back::write::link(cgcx, dcx, modules)

View File

@ -14,7 +14,7 @@ use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind}; use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::memmap::Mmap; use rustc_data_structures::memmap::Mmap;
use rustc_errors::{DiagCtxt, FatalError}; use rustc_errors::{DiagCtxtHandle, FatalError};
use rustc_hir::def_id::LOCAL_CRATE; use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::dep_graph::WorkProduct; use rustc_middle::dep_graph::WorkProduct;
@ -49,7 +49,7 @@ pub fn crate_type_allows_lto(crate_type: CrateType) -> bool {
fn prepare_lto( fn prepare_lto(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
) -> Result<(Vec<CString>, Vec<(SerializedModule<ModuleBuffer>, CString)>), FatalError> { ) -> Result<(Vec<CString>, Vec<(SerializedModule<ModuleBuffer>, CString)>), FatalError> {
let export_threshold = match cgcx.lto { let export_threshold = match cgcx.lto {
// We're just doing LTO for our one crate // We're just doing LTO for our one crate
@ -203,10 +203,11 @@ pub(crate) fn run_fat(
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>, cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
) -> Result<LtoModuleCodegen<LlvmCodegenBackend>, FatalError> { ) -> Result<LtoModuleCodegen<LlvmCodegenBackend>, FatalError> {
let dcx = cgcx.create_dcx(); let dcx = cgcx.create_dcx();
let (symbols_below_threshold, upstream_modules) = prepare_lto(cgcx, &dcx)?; let dcx = dcx.handle();
let (symbols_below_threshold, upstream_modules) = prepare_lto(cgcx, dcx)?;
let symbols_below_threshold = let symbols_below_threshold =
symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>(); symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();
fat_lto(cgcx, &dcx, modules, cached_modules, upstream_modules, &symbols_below_threshold) fat_lto(cgcx, dcx, modules, cached_modules, upstream_modules, &symbols_below_threshold)
} }
/// Performs thin LTO by performing necessary global analysis and returning two /// Performs thin LTO by performing necessary global analysis and returning two
@ -218,7 +219,8 @@ pub(crate) fn run_thin(
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>, cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
) -> Result<(Vec<LtoModuleCodegen<LlvmCodegenBackend>>, Vec<WorkProduct>), FatalError> { ) -> Result<(Vec<LtoModuleCodegen<LlvmCodegenBackend>>, Vec<WorkProduct>), FatalError> {
let dcx = cgcx.create_dcx(); let dcx = cgcx.create_dcx();
let (symbols_below_threshold, upstream_modules) = prepare_lto(cgcx, &dcx)?; let dcx = dcx.handle();
let (symbols_below_threshold, upstream_modules) = prepare_lto(cgcx, dcx)?;
let symbols_below_threshold = let symbols_below_threshold =
symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>(); symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();
if cgcx.opts.cg.linker_plugin_lto.enabled() { if cgcx.opts.cg.linker_plugin_lto.enabled() {
@ -227,7 +229,7 @@ pub(crate) fn run_thin(
is deferred to the linker" is deferred to the linker"
); );
} }
thin_lto(cgcx, &dcx, modules, upstream_modules, cached_modules, &symbols_below_threshold) thin_lto(cgcx, dcx, modules, upstream_modules, cached_modules, &symbols_below_threshold)
} }
pub(crate) fn prepare_thin( pub(crate) fn prepare_thin(
@ -241,7 +243,7 @@ pub(crate) fn prepare_thin(
fn fat_lto( fn fat_lto(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
modules: Vec<FatLtoInput<LlvmCodegenBackend>>, modules: Vec<FatLtoInput<LlvmCodegenBackend>>,
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>, cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>, mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
@ -436,7 +438,7 @@ impl Drop for Linker<'_> {
/// they all go out of scope. /// they all go out of scope.
fn thin_lto( fn thin_lto(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
modules: Vec<(String, ThinBuffer)>, modules: Vec<(String, ThinBuffer)>,
serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>, serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>, cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
@ -593,7 +595,7 @@ fn thin_lto(
pub(crate) fn run_pass_manager( pub(crate) fn run_pass_manager(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: &mut ModuleCodegen<ModuleLlvm>, module: &mut ModuleCodegen<ModuleLlvm>,
thin: bool, thin: bool,
) -> Result<(), FatalError> { ) -> Result<(), FatalError> {
@ -714,10 +716,11 @@ pub unsafe fn optimize_thin_module(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> { ) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
let dcx = cgcx.create_dcx(); let dcx = cgcx.create_dcx();
let dcx = dcx.handle();
let module_name = &thin_module.shared.module_names[thin_module.idx]; let module_name = &thin_module.shared.module_names[thin_module.idx];
let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, module_name.to_str().unwrap()); let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, module_name.to_str().unwrap());
let tm = (cgcx.tm_factory)(tm_factory_config).map_err(|e| write::llvm_err(&dcx, e))?; let tm = (cgcx.tm_factory)(tm_factory_config).map_err(|e| write::llvm_err(dcx, e))?;
// Right now the implementation we've got only works over serialized // Right now the implementation we've got only works over serialized
// modules, so we create a fresh new LLVM context and parse the module // modules, so we create a fresh new LLVM context and parse the module
@ -725,7 +728,7 @@ pub unsafe fn optimize_thin_module(
// crates but for locally codegened modules we may be able to reuse // crates but for locally codegened modules we may be able to reuse
// that LLVM Context and Module. // that LLVM Context and Module.
let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names); let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
let llmod_raw = parse_module(llcx, module_name, thin_module.data(), &dcx)? as *const _; let llmod_raw = parse_module(llcx, module_name, thin_module.data(), dcx)? as *const _;
let mut module = ModuleCodegen { let mut module = ModuleCodegen {
module_llvm: ModuleLlvm { llmod_raw, llcx, tm: ManuallyDrop::new(tm) }, module_llvm: ModuleLlvm { llmod_raw, llcx, tm: ManuallyDrop::new(tm) },
name: thin_module.name().to_string(), name: thin_module.name().to_string(),
@ -748,7 +751,7 @@ pub unsafe fn optimize_thin_module(
let _timer = let _timer =
cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_rename", thin_module.name()); cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_rename", thin_module.name());
if !llvm::LLVMRustPrepareThinLTORename(thin_module.shared.data.0, llmod, target) { if !llvm::LLVMRustPrepareThinLTORename(thin_module.shared.data.0, llmod, target) {
return Err(write::llvm_err(&dcx, LlvmError::PrepareThinLtoModule)); return Err(write::llvm_err(dcx, LlvmError::PrepareThinLtoModule));
} }
save_temp_bitcode(cgcx, &module, "thin-lto-after-rename"); save_temp_bitcode(cgcx, &module, "thin-lto-after-rename");
} }
@ -758,7 +761,7 @@ pub unsafe fn optimize_thin_module(
.prof .prof
.generic_activity_with_arg("LLVM_thin_lto_resolve_weak", thin_module.name()); .generic_activity_with_arg("LLVM_thin_lto_resolve_weak", thin_module.name());
if !llvm::LLVMRustPrepareThinLTOResolveWeak(thin_module.shared.data.0, llmod) { if !llvm::LLVMRustPrepareThinLTOResolveWeak(thin_module.shared.data.0, llmod) {
return Err(write::llvm_err(&dcx, LlvmError::PrepareThinLtoModule)); return Err(write::llvm_err(dcx, LlvmError::PrepareThinLtoModule));
} }
save_temp_bitcode(cgcx, &module, "thin-lto-after-resolve"); save_temp_bitcode(cgcx, &module, "thin-lto-after-resolve");
} }
@ -768,7 +771,7 @@ pub unsafe fn optimize_thin_module(
.prof .prof
.generic_activity_with_arg("LLVM_thin_lto_internalize", thin_module.name()); .generic_activity_with_arg("LLVM_thin_lto_internalize", thin_module.name());
if !llvm::LLVMRustPrepareThinLTOInternalize(thin_module.shared.data.0, llmod) { if !llvm::LLVMRustPrepareThinLTOInternalize(thin_module.shared.data.0, llmod) {
return Err(write::llvm_err(&dcx, LlvmError::PrepareThinLtoModule)); return Err(write::llvm_err(dcx, LlvmError::PrepareThinLtoModule));
} }
save_temp_bitcode(cgcx, &module, "thin-lto-after-internalize"); save_temp_bitcode(cgcx, &module, "thin-lto-after-internalize");
} }
@ -777,7 +780,7 @@ pub unsafe fn optimize_thin_module(
let _timer = let _timer =
cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_import", thin_module.name()); cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_import", thin_module.name());
if !llvm::LLVMRustPrepareThinLTOImport(thin_module.shared.data.0, llmod, target) { if !llvm::LLVMRustPrepareThinLTOImport(thin_module.shared.data.0, llmod, target) {
return Err(write::llvm_err(&dcx, LlvmError::PrepareThinLtoModule)); return Err(write::llvm_err(dcx, LlvmError::PrepareThinLtoModule));
} }
save_temp_bitcode(cgcx, &module, "thin-lto-after-import"); save_temp_bitcode(cgcx, &module, "thin-lto-after-import");
} }
@ -789,7 +792,7 @@ pub unsafe fn optimize_thin_module(
// little differently. // little differently.
{ {
info!("running thin lto passes over {}", module.name); info!("running thin lto passes over {}", module.name);
run_pass_manager(cgcx, &dcx, &mut module, true)?; run_pass_manager(cgcx, dcx, &mut module, true)?;
save_temp_bitcode(cgcx, &module, "thin-lto-after-pm"); save_temp_bitcode(cgcx, &module, "thin-lto-after-pm");
} }
} }
@ -859,7 +862,7 @@ pub fn parse_module<'a>(
cx: &'a llvm::Context, cx: &'a llvm::Context,
name: &CStr, name: &CStr,
data: &[u8], data: &[u8],
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
) -> Result<&'a llvm::Module, FatalError> { ) -> Result<&'a llvm::Module, FatalError> {
unsafe { unsafe {
llvm::LLVMRustParseBitcodeForLTO(cx, data.as_ptr(), data.len(), name.as_ptr()) llvm::LLVMRustParseBitcodeForLTO(cx, data.as_ptr(), data.len(), name.as_ptr())

View File

@ -26,7 +26,7 @@ use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen}; use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
use rustc_errors::{DiagCtxt, FatalError, Level}; use rustc_errors::{DiagCtxtHandle, FatalError, Level};
use rustc_fs_util::{link_or_copy, path_to_c_string}; use rustc_fs_util::{link_or_copy, path_to_c_string};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::config::{self, Lto, OutputType, Passes}; use rustc_session::config::{self, Lto, OutputType, Passes};
@ -47,7 +47,7 @@ use std::slice;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
pub fn llvm_err<'a>(dcx: &rustc_errors::DiagCtxt, err: LlvmError<'a>) -> FatalError { pub fn llvm_err<'a>(dcx: DiagCtxtHandle<'_>, err: LlvmError<'a>) -> FatalError {
match llvm::last_error() { match llvm::last_error() {
Some(llvm_err) => dcx.emit_almost_fatal(WithLlvmError(err, llvm_err)), Some(llvm_err) => dcx.emit_almost_fatal(WithLlvmError(err, llvm_err)),
None => dcx.emit_almost_fatal(err), None => dcx.emit_almost_fatal(err),
@ -55,7 +55,7 @@ pub fn llvm_err<'a>(dcx: &rustc_errors::DiagCtxt, err: LlvmError<'a>) -> FatalEr
} }
pub fn write_output_file<'ll>( pub fn write_output_file<'ll>(
dcx: &rustc_errors::DiagCtxt, dcx: DiagCtxtHandle<'_>,
target: &'ll llvm::TargetMachine, target: &'ll llvm::TargetMachine,
pm: &llvm::PassManager<'ll>, pm: &llvm::PassManager<'ll>,
m: &'ll llvm::Module, m: &'ll llvm::Module,
@ -331,7 +331,7 @@ pub enum CodegenDiagnosticsStage {
} }
pub struct DiagnosticHandlers<'a> { pub struct DiagnosticHandlers<'a> {
data: *mut (&'a CodegenContext<LlvmCodegenBackend>, &'a DiagCtxt), data: *mut (&'a CodegenContext<LlvmCodegenBackend>, DiagCtxtHandle<'a>),
llcx: &'a llvm::Context, llcx: &'a llvm::Context,
old_handler: Option<&'a llvm::DiagnosticHandler>, old_handler: Option<&'a llvm::DiagnosticHandler>,
} }
@ -339,7 +339,7 @@ pub struct DiagnosticHandlers<'a> {
impl<'a> DiagnosticHandlers<'a> { impl<'a> DiagnosticHandlers<'a> {
pub fn new( pub fn new(
cgcx: &'a CodegenContext<LlvmCodegenBackend>, cgcx: &'a CodegenContext<LlvmCodegenBackend>,
dcx: &'a DiagCtxt, dcx: DiagCtxtHandle<'a>,
llcx: &'a llvm::Context, llcx: &'a llvm::Context,
module: &ModuleCodegen<ModuleLlvm>, module: &ModuleCodegen<ModuleLlvm>,
stage: CodegenDiagnosticsStage, stage: CodegenDiagnosticsStage,
@ -428,7 +428,7 @@ unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void
if user.is_null() { if user.is_null() {
return; return;
} }
let (cgcx, dcx) = *(user as *const (&CodegenContext<LlvmCodegenBackend>, &DiagCtxt)); let (cgcx, dcx) = *(user as *const (&CodegenContext<LlvmCodegenBackend>, DiagCtxtHandle<'_>));
match llvm::diagnostic::Diagnostic::unpack(info) { match llvm::diagnostic::Diagnostic::unpack(info) {
llvm::diagnostic::InlineAsm(inline) => { llvm::diagnostic::InlineAsm(inline) => {
@ -506,7 +506,7 @@ fn get_instr_profile_output_path(config: &ModuleConfig) -> Option<CString> {
pub(crate) unsafe fn llvm_optimize( pub(crate) unsafe fn llvm_optimize(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: &ModuleCodegen<ModuleLlvm>, module: &ModuleCodegen<ModuleLlvm>,
config: &ModuleConfig, config: &ModuleConfig,
opt_level: config::OptLevel, opt_level: config::OptLevel,
@ -604,7 +604,7 @@ pub(crate) unsafe fn llvm_optimize(
// Unsafe due to LLVM calls. // Unsafe due to LLVM calls.
pub(crate) unsafe fn optimize( pub(crate) unsafe fn optimize(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: &ModuleCodegen<ModuleLlvm>, module: &ModuleCodegen<ModuleLlvm>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<(), FatalError> { ) -> Result<(), FatalError> {
@ -637,7 +637,7 @@ pub(crate) unsafe fn optimize(
pub(crate) fn link( pub(crate) fn link(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
mut modules: Vec<ModuleCodegen<ModuleLlvm>>, mut modules: Vec<ModuleCodegen<ModuleLlvm>>,
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> { ) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
use super::lto::{Linker, ModuleBuffer}; use super::lto::{Linker, ModuleBuffer};
@ -660,7 +660,7 @@ pub(crate) fn link(
pub(crate) unsafe fn codegen( pub(crate) unsafe fn codegen(
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: ModuleCodegen<ModuleLlvm>, module: ModuleCodegen<ModuleLlvm>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<CompiledModule, FatalError> { ) -> Result<CompiledModule, FatalError> {

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, Diagnostic, EmissionGuarantee, Level}; use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span; use rustc_span::Span;
@ -100,7 +100,7 @@ pub(crate) struct DynamicLinkingWithLTO;
pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>); pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>);
impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
let diag: Diag<'_, G> = self.0.into_diag(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());
@ -120,7 +120,7 @@ pub(crate) struct TargetFeatureDisableOrEnable<'a> {
pub(crate) struct MissingFeatures; pub(crate) struct MissingFeatures;
impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_> {
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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);
@ -180,7 +180,7 @@ 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> Diagnostic<'_, G> for WithLlvmError<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for WithLlvmError<'_> {
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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,

View File

@ -31,7 +31,7 @@ use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::ModuleCodegen; use rustc_codegen_ssa::ModuleCodegen;
use rustc_codegen_ssa::{CodegenResults, CompiledModule}; use rustc_codegen_ssa::{CodegenResults, CompiledModule};
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::{DiagCtxt, ErrorGuaranteed, FatalError}; use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, FatalError};
use rustc_metadata::EncodedMetadata; use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
@ -191,7 +191,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
} }
fn run_link( fn run_link(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
modules: Vec<ModuleCodegen<Self::Module>>, modules: Vec<ModuleCodegen<Self::Module>>,
) -> Result<ModuleCodegen<Self::Module>, FatalError> { ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
back::write::link(cgcx, dcx, modules) back::write::link(cgcx, dcx, modules)
@ -212,7 +212,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
} }
unsafe fn optimize( unsafe fn optimize(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: &ModuleCodegen<Self::Module>, module: &ModuleCodegen<Self::Module>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<(), FatalError> { ) -> Result<(), FatalError> {
@ -223,7 +223,8 @@ impl WriteBackendMethods for LlvmCodegenBackend {
module: &mut ModuleCodegen<Self::Module>, module: &mut ModuleCodegen<Self::Module>,
) -> Result<(), FatalError> { ) -> Result<(), FatalError> {
let dcx = cgcx.create_dcx(); let dcx = cgcx.create_dcx();
back::lto::run_pass_manager(cgcx, &dcx, module, false) let dcx = dcx.handle();
back::lto::run_pass_manager(cgcx, dcx, module, false)
} }
unsafe fn optimize_thin( unsafe fn optimize_thin(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
@ -233,7 +234,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
} }
unsafe fn codegen( unsafe fn codegen(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: ModuleCodegen<Self::Module>, module: ModuleCodegen<Self::Module>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<CompiledModule, FatalError> { ) -> Result<CompiledModule, FatalError> {
@ -441,7 +442,7 @@ impl ModuleLlvm {
cgcx: &CodegenContext<LlvmCodegenBackend>, cgcx: &CodegenContext<LlvmCodegenBackend>,
name: &CStr, name: &CStr,
buffer: &[u8], buffer: &[u8],
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
) -> Result<Self, FatalError> { ) -> Result<Self, FatalError> {
unsafe { unsafe {
let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names); let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);

View File

@ -3,7 +3,7 @@ use rustc_ast::CRATE_NODE_ID;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::memmap::Mmap; use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::temp_dir::MaybeTempDir; use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_errors::{DiagCtxt, ErrorGuaranteed, FatalError}; use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, FatalError};
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize}; use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc_metadata::find_native_static_library; use rustc_metadata::find_native_static_library;
@ -54,7 +54,7 @@ use std::process::{ExitStatus, Output, Stdio};
use std::{env, fmt, fs, io, mem, str}; use std::{env, fmt, fs, io, mem, str};
use tracing::{debug, info, warn}; use tracing::{debug, info, warn};
pub fn ensure_removed(dcx: &DiagCtxt, path: &Path) { pub fn ensure_removed(dcx: DiagCtxtHandle<'_>, path: &Path) {
if let Err(e) = fs::remove_file(path) { if let Err(e) = fs::remove_file(path) {
if e.kind() != io::ErrorKind::NotFound { if e.kind() != io::ErrorKind::NotFound {
dcx.err(format!("failed to remove {}: {}", path.display(), e)); dcx.err(format!("failed to remove {}: {}", path.display(), e));

View File

@ -891,9 +891,10 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
module_config: &ModuleConfig, module_config: &ModuleConfig,
) -> Result<WorkItemResult<B>, FatalError> { ) -> Result<WorkItemResult<B>, FatalError> {
let dcx = cgcx.create_dcx(); let dcx = cgcx.create_dcx();
let dcx = dcx.handle();
unsafe { unsafe {
B::optimize(cgcx, &dcx, &module, module_config)?; B::optimize(cgcx, dcx, &module, module_config)?;
} }
// After we've done the initial round of optimizations we need to // After we've done the initial round of optimizations we need to
@ -954,7 +955,11 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
match link_or_copy(&source_file, &output_path) { match link_or_copy(&source_file, &output_path) {
Ok(_) => Some(output_path), Ok(_) => Some(output_path),
Err(error) => { Err(error) => {
cgcx.create_dcx().emit_err(errors::CopyPathBuf { source_file, output_path, error }); cgcx.create_dcx().handle().emit_err(errors::CopyPathBuf {
source_file,
output_path,
error,
});
None None
} }
} }
@ -987,7 +992,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
let bytecode = load_from_incr_cache(module_config.emit_bc, OutputType::Bitcode); let bytecode = load_from_incr_cache(module_config.emit_bc, OutputType::Bitcode);
let object = load_from_incr_cache(should_emit_obj, OutputType::Object); let object = load_from_incr_cache(should_emit_obj, OutputType::Object);
if should_emit_obj && object.is_none() { if should_emit_obj && object.is_none() {
cgcx.create_dcx().emit_fatal(errors::NoSavedObjectFile { cgu_name: &module.name }) cgcx.create_dcx().handle().emit_fatal(errors::NoSavedObjectFile { cgu_name: &module.name })
} }
WorkItemResult::Finished(CompiledModule { WorkItemResult::Finished(CompiledModule {
@ -1016,12 +1021,13 @@ fn finish_intra_module_work<B: ExtraBackendMethods>(
module_config: &ModuleConfig, module_config: &ModuleConfig,
) -> Result<WorkItemResult<B>, FatalError> { ) -> Result<WorkItemResult<B>, FatalError> {
let dcx = cgcx.create_dcx(); let dcx = cgcx.create_dcx();
let dcx = dcx.handle();
if !cgcx.opts.unstable_opts.combine_cgu if !cgcx.opts.unstable_opts.combine_cgu
|| module.kind == ModuleKind::Metadata || module.kind == ModuleKind::Metadata
|| module.kind == ModuleKind::Allocator || module.kind == ModuleKind::Allocator
{ {
let module = unsafe { B::codegen(cgcx, &dcx, module, module_config)? }; let module = unsafe { B::codegen(cgcx, dcx, module, module_config)? };
Ok(WorkItemResult::Finished(module)) Ok(WorkItemResult::Finished(module))
} else { } else {
Ok(WorkItemResult::NeedsLink(module)) Ok(WorkItemResult::NeedsLink(module))
@ -1692,9 +1698,10 @@ fn start_executing_work<B: ExtraBackendMethods>(
if !needs_link.is_empty() { if !needs_link.is_empty() {
assert!(compiled_modules.is_empty()); assert!(compiled_modules.is_empty());
let dcx = cgcx.create_dcx(); let dcx = cgcx.create_dcx();
let module = B::run_link(&cgcx, &dcx, needs_link).map_err(|_| ())?; let dcx = dcx.handle();
let module = B::run_link(&cgcx, dcx, needs_link).map_err(|_| ())?;
let module = unsafe { let module = unsafe {
B::codegen(&cgcx, &dcx, module, cgcx.config(ModuleKind::Regular)).map_err(|_| ())? B::codegen(&cgcx, dcx, module, cgcx.config(ModuleKind::Regular)).map_err(|_| ())?
}; };
compiled_modules.push(module); compiled_modules.push(module);
} }

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, Diagnostic, EmissionGuarantee, IntoDiagArg, Level, codes::*, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, IntoDiagArg, Level,
}; };
use rustc_macros::Diagnostic; use rustc_macros::Diagnostic;
use rustc_middle::ty::layout::LayoutError; use rustc_middle::ty::layout::LayoutError;
@ -215,7 +215,7 @@ pub enum LinkRlibError {
pub struct ThorinErrorWrapper(pub thorin::Error); pub struct ThorinErrorWrapper(pub thorin::Error);
impl<G: EmissionGuarantee> Diagnostic<'_, G> for ThorinErrorWrapper { impl<G: EmissionGuarantee> Diagnostic<'_, G> for ThorinErrorWrapper {
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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),
@ -348,7 +348,7 @@ pub struct LinkingFailed<'a> {
} }
impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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

@ -2,7 +2,7 @@ use crate::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
use crate::back::write::{CodegenContext, FatLtoInput, ModuleConfig}; use crate::back::write::{CodegenContext, FatLtoInput, ModuleConfig};
use crate::{CompiledModule, ModuleCodegen}; use crate::{CompiledModule, ModuleCodegen};
use rustc_errors::{DiagCtxt, FatalError}; use rustc_errors::{DiagCtxtHandle, FatalError};
use rustc_middle::dep_graph::WorkProduct; use rustc_middle::dep_graph::WorkProduct;
pub trait WriteBackendMethods: 'static + Sized + Clone { pub trait WriteBackendMethods: 'static + Sized + Clone {
@ -16,7 +16,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
/// Merge all modules into main_module and returning it /// Merge all modules into main_module and returning it
fn run_link( fn run_link(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
modules: Vec<ModuleCodegen<Self::Module>>, modules: Vec<ModuleCodegen<Self::Module>>,
) -> Result<ModuleCodegen<Self::Module>, FatalError>; ) -> Result<ModuleCodegen<Self::Module>, FatalError>;
/// Performs fat LTO by merging all modules into a single one and returning it /// Performs fat LTO by merging all modules into a single one and returning it
@ -38,7 +38,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
fn print_statistics(&self); fn print_statistics(&self);
unsafe fn optimize( unsafe fn optimize(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: &ModuleCodegen<Self::Module>, module: &ModuleCodegen<Self::Module>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<(), FatalError>; ) -> Result<(), FatalError>;
@ -52,7 +52,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
) -> Result<ModuleCodegen<Self::Module>, FatalError>; ) -> Result<ModuleCodegen<Self::Module>, FatalError>;
unsafe fn codegen( unsafe fn codegen(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
module: ModuleCodegen<Self::Module>, module: ModuleCodegen<Self::Module>,
config: &ModuleConfig, config: &ModuleConfig,
) -> Result<CompiledModule, FatalError>; ) -> Result<CompiledModule, FatalError>;

View File

@ -5,7 +5,7 @@
//! it finds operations that are invalid in a certain context. //! it finds operations that are invalid in a certain context.
use rustc_attr as attr; use rustc_attr as attr;
use rustc_errors::DiagCtxt; use rustc_errors::DiagCtxtHandle;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::bug; use rustc_middle::bug;
@ -46,7 +46,7 @@ impl<'mir, 'tcx> ConstCx<'mir, 'tcx> {
ConstCx { body, tcx, param_env, const_kind } ConstCx { body, tcx, param_env, const_kind }
} }
pub(crate) fn dcx(&self) -> &'tcx DiagCtxt { pub(crate) fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.tcx.dcx() self.tcx.dcx()
} }

View File

@ -2,7 +2,7 @@ use std::borrow::Cow;
use either::Either; use either::Either;
use rustc_errors::{ use rustc_errors::{
codes::*, Diag, DiagArgValue, DiagCtxt, DiagMessage, Diagnostic, EmissionGuarantee, Level, codes::*, Diag, DiagArgValue, DiagCtxtHandle, DiagMessage, Diagnostic, EmissionGuarantee, Level,
}; };
use rustc_hir::ConstContext; use rustc_hir::ConstContext;
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
@ -453,7 +453,7 @@ pub trait ReportErrorExt {
} }
} }
fn bad_pointer_message(msg: CheckInAllocMsg, dcx: &DiagCtxt) -> String { fn bad_pointer_message(msg: CheckInAllocMsg, dcx: DiagCtxtHandle<'_>) -> String {
use crate::fluent_generated::*; use crate::fluent_generated::*;
let msg = match msg { let msg = match msg {

View File

@ -4,7 +4,7 @@ use std::{fmt, mem};
use either::{Either, Left, Right}; use either::{Either, Left, Right};
use tracing::{debug, info, info_span, instrument, trace}; use tracing::{debug, info, info_span, instrument, trace};
use rustc_errors::DiagCtxt; use rustc_errors::DiagCtxtHandle;
use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData}; use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData};
use rustc_index::IndexVec; use rustc_index::IndexVec;
use rustc_middle::mir; use rustc_middle::mir;
@ -474,7 +474,7 @@ pub(super) fn from_known_layout<'tcx>(
/// ///
/// This is NOT the preferred way to render an error; use `report` from `const_eval` instead. /// This is NOT the preferred way to render an error; use `report` from `const_eval` instead.
/// However, this is useful when error messages appear in ICEs. /// However, this is useful when error messages appear in ICEs.
pub fn format_interp_error<'tcx>(dcx: &DiagCtxt, e: InterpErrorInfo<'tcx>) -> String { pub fn format_interp_error<'tcx>(dcx: DiagCtxtHandle<'_>, e: InterpErrorInfo<'tcx>) -> String {
let (e, backtrace) = e.into_parts(); let (e, backtrace) = e.into_parts();
backtrace.print_backtrace(); backtrace.print_backtrace();
// FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the // FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the

View File

@ -1444,6 +1444,7 @@ fn report_ice(
fallback_bundle, fallback_bundle,
)); ));
let dcx = rustc_errors::DiagCtxt::new(emitter); let dcx = rustc_errors::DiagCtxt::new(emitter);
let dcx = dcx.handle();
// a .span_bug or .bug call has already printed what // a .span_bug or .bug call has already printed what
// it wants to print. // it wants to print.
@ -1509,7 +1510,7 @@ fn report_ice(
let num_frames = if backtrace { None } else { Some(2) }; let num_frames = if backtrace { None } else { Some(2) };
interface::try_print_query_stack(&dcx, num_frames, file); interface::try_print_query_stack(dcx, num_frames, file);
// We don't trust this callback not to panic itself, so run it at the end after we're sure we've // We don't trust this callback not to panic itself, so run it at the end after we're sure we've
// printed all the relevant info. // printed all the relevant info.

View File

@ -1,7 +1,7 @@
use crate::snippet::Style; use crate::snippet::Style;
use crate::{ use crate::{
CodeSuggestion, DiagCtxt, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level, MultiSpan, CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
StashKey, SubdiagMessage, Substitution, SubstitutionPart, SuggestionStyle, MultiSpan, StashKey, SubdiagMessage, Substitution, SubstitutionPart, SuggestionStyle,
}; };
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_error_messages::fluent_value_from_str_list_sep_by_and; use rustc_error_messages::fluent_value_from_str_list_sep_by_and;
@ -133,7 +133,7 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
pub trait Diagnostic<'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_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G>; fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G>;
} }
impl<'a, T, G> Diagnostic<'a, G> for Spanned<T> impl<'a, T, G> Diagnostic<'a, G> for Spanned<T>
@ -141,7 +141,7 @@ where
T: Diagnostic<'a, G>, T: Diagnostic<'a, G>,
G: EmissionGuarantee, G: EmissionGuarantee,
{ {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
self.node.into_diag(dcx, level).with_span(self.span) self.node.into_diag(dcx, level).with_span(self.span)
} }
} }
@ -490,7 +490,7 @@ pub struct Subdiag {
/// the methods of `Diag` here, consider extending `DiagCtxtFlags`. /// the methods of `Diag` here, consider extending `DiagCtxtFlags`.
#[must_use] #[must_use]
pub struct Diag<'a, G: EmissionGuarantee = ErrorGuaranteed> { pub struct Diag<'a, G: EmissionGuarantee = ErrorGuaranteed> {
pub dcx: &'a DiagCtxt, pub dcx: DiagCtxtHandle<'a>,
/// Why the `Option`? It is always `Some` until the `Diag` is consumed via /// Why the `Option`? It is always `Some` until the `Diag` is consumed via
/// `emit`, `cancel`, etc. At that point it is consumed and replaced with /// `emit`, `cancel`, etc. At that point it is consumed and replaced with
@ -578,13 +578,13 @@ macro_rules! with_fn {
impl<'a, G: EmissionGuarantee> Diag<'a, G> { impl<'a, G: EmissionGuarantee> Diag<'a, G> {
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn new(dcx: &'a DiagCtxt, level: Level, message: impl Into<DiagMessage>) -> Self { pub fn new(dcx: DiagCtxtHandle<'a>, level: Level, message: impl Into<DiagMessage>) -> Self {
Self::new_diagnostic(dcx, DiagInner::new(level, message)) Self::new_diagnostic(dcx, DiagInner::new(level, message))
} }
/// Creates a new `Diag` with an already constructed diagnostic. /// Creates a new `Diag` with an already constructed diagnostic.
#[track_caller] #[track_caller]
pub(crate) fn new_diagnostic(dcx: &'a DiagCtxt, diag: DiagInner) -> Self { pub(crate) fn new_diagnostic(dcx: DiagCtxtHandle<'a>, diag: DiagInner) -> Self {
debug!("Created new diagnostic"); debug!("Created new diagnostic");
Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData } Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData }
} }
@ -1194,7 +1194,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
pub fn subdiagnostic( pub fn subdiagnostic(
&mut self, &mut self,
dcx: &crate::DiagCtxt, dcx: DiagCtxtHandle<'_>,
subdiagnostic: impl Subdiagnostic, subdiagnostic: impl Subdiagnostic,
) -> &mut Self { ) -> &mut Self {
subdiagnostic.add_to_diag_with(self, &|diag, msg| { subdiagnostic.add_to_diag_with(self, &|diag, msg| {
@ -1341,7 +1341,8 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
/// See `DiagCtxt::stash_diagnostic` for details. /// See `DiagCtxt::stash_diagnostic` for details.
pub fn stash(mut self, span: Span, key: StashKey) -> Option<ErrorGuaranteed> { pub fn stash(mut self, span: Span, key: StashKey) -> Option<ErrorGuaranteed> {
self.dcx.stash_diagnostic(span, key, self.take_diag()) let diag = self.take_diag();
self.dcx.stash_diagnostic(span, key, diag)
} }
/// Delay emission of this diagnostic as a bug. /// Delay emission of this diagnostic as a bug.

View File

@ -1,7 +1,7 @@
use crate::diagnostic::DiagLocation; use crate::diagnostic::DiagLocation;
use crate::{fluent_generated as fluent, Subdiagnostic}; use crate::{fluent_generated as fluent, DiagCtxtHandle, Subdiagnostic};
use crate::{ use crate::{
Diag, DiagArgValue, DiagCtxt, Diagnostic, EmissionGuarantee, ErrCode, IntoDiagArg, Level, Diag, DiagArgValue, Diagnostic, EmissionGuarantee, ErrCode, IntoDiagArg, Level,
SubdiagMessageOp, SubdiagMessageOp,
}; };
use rustc_ast as ast; use rustc_ast as ast;
@ -315,7 +315,7 @@ impl IntoDiagArg for DiagSymbolList {
} }
impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetDataLayoutErrors<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetDataLayoutErrors<'_> {
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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

@ -567,7 +567,7 @@ impl Emitter for SilentEmitter {
if let Some(fatal_note) = &self.fatal_note { if let Some(fatal_note) = &self.fatal_note {
diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new()); diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new());
} }
self.fatal_dcx.emit_diagnostic(diag); self.fatal_dcx.handle().emit_diagnostic(diag);
} }
} }
} }

View File

@ -55,8 +55,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
); );
let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1)); let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));
let dcx = DiagCtxt::new(Box::new(je)); DiagCtxt::new(Box::new(je)).handle().span_err(span, "foo");
dcx.span_err(span, "foo");
let bytes = output.lock().unwrap(); let bytes = output.lock().unwrap();
let actual_output = str::from_utf8(&bytes).unwrap(); let actual_output = str::from_utf8(&bytes).unwrap();

View File

@ -414,6 +414,19 @@ pub struct DiagCtxt {
inner: Lock<DiagCtxtInner>, inner: Lock<DiagCtxtInner>,
} }
#[derive(Copy, Clone)]
pub struct DiagCtxtHandle<'a> {
dcx: &'a DiagCtxt,
}
impl<'a> std::ops::Deref for DiagCtxtHandle<'a> {
type Target = &'a DiagCtxt;
fn deref(&self) -> &Self::Target {
&self.dcx
}
}
/// This inner struct exists to keep it all behind a single lock; /// This inner struct exists to keep it all behind a single lock;
/// this is done to prevent possible deadlocks in a multi-threaded compiler, /// this is done to prevent possible deadlocks in a multi-threaded compiler,
/// as well as inconsistent state observation. /// as well as inconsistent state observation.
@ -608,7 +621,7 @@ impl DiagCtxt {
} }
pub fn make_silent( pub fn make_silent(
&mut self, &self,
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
fatal_note: Option<String>, fatal_note: Option<String>,
emit_fatal_diagnostic: bool, emit_fatal_diagnostic: bool,
@ -623,7 +636,7 @@ impl DiagCtxt {
}); });
} }
fn wrap_emitter<F>(&mut self, f: F) fn wrap_emitter<F>(&self, f: F)
where where
F: FnOnce(DiagCtxtInner) -> Box<DynEmitter>, F: FnOnce(DiagCtxtInner) -> Box<DynEmitter>,
{ {
@ -738,6 +751,12 @@ impl DiagCtxt {
*fulfilled_expectations = Default::default(); *fulfilled_expectations = Default::default();
} }
pub fn handle<'a>(&'a self) -> DiagCtxtHandle<'a> {
DiagCtxtHandle { dcx: self }
}
}
impl<'a> DiagCtxtHandle<'a> {
/// Stashes a diagnostic for possible later improvement in a different, /// Stashes a diagnostic for possible later improvement in a different,
/// later stage of the compiler. Possible actions depend on the diagnostic /// later stage of the compiler. Possible actions depend on the diagnostic
/// level: /// level:
@ -745,8 +764,8 @@ impl DiagCtxt {
/// - Level::Error: immediately counted as an error that has occurred, because it /// - Level::Error: immediately counted as an error that has occurred, because it
/// is guaranteed to be emitted eventually. Can be later accessed with the /// is guaranteed to be emitted eventually. Can be later accessed with the
/// provided `span` and `key` through /// provided `span` and `key` through
/// [`DiagCtxt::try_steal_modify_and_emit_err`] or /// [`DiagCtxtHandle::try_steal_modify_and_emit_err`] or
/// [`DiagCtxt::try_steal_replace_and_emit_err`]. These do not allow /// [`DiagCtxtHandle::try_steal_replace_and_emit_err`]. These do not allow
/// cancellation or downgrading of the error. Returns /// cancellation or downgrading of the error. Returns
/// `Some(ErrorGuaranteed)`. /// `Some(ErrorGuaranteed)`.
/// - Level::DelayedBug: this does happen occasionally with errors that are /// - Level::DelayedBug: this does happen occasionally with errors that are
@ -757,7 +776,7 @@ impl DiagCtxt {
/// user-facing error. Returns `Some(ErrorGuaranteed)` as is normal for /// user-facing error. Returns `Some(ErrorGuaranteed)` as is normal for
/// delayed bugs. /// delayed bugs.
/// - Level::Warning and lower (i.e. !is_error()): can be accessed with the /// - Level::Warning and lower (i.e. !is_error()): can be accessed with the
/// provided `span` and `key` through [`DiagCtxt::steal_non_err()`]. This /// provided `span` and `key` through [`DiagCtxtHandle::steal_non_err()`]. This
/// allows cancelling and downgrading of the diagnostic. Returns `None`. /// allows cancelling and downgrading of the diagnostic. Returns `None`.
pub fn stash_diagnostic( pub fn stash_diagnostic(
&self, &self,
@ -793,7 +812,7 @@ impl DiagCtxt {
/// Steal a previously stashed non-error diagnostic with the given `Span` /// Steal a previously stashed non-error diagnostic with the given `Span`
/// and [`StashKey`] as the key. Panics if the found diagnostic is an /// and [`StashKey`] as the key. Panics if the found diagnostic is an
/// error. /// error.
pub fn steal_non_err(&self, span: Span, key: StashKey) -> Option<Diag<'_, ()>> { pub fn steal_non_err(self, span: Span, key: StashKey) -> Option<Diag<'a, ()>> {
let key = (span.with_parent(None), key); let key = (span.with_parent(None), key);
// FIXME(#120456) - is `swap_remove` correct? // FIXME(#120456) - is `swap_remove` correct?
let (diag, guar) = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key)?; let (diag, guar) = self.inner.borrow_mut().stashed_diagnostics.swap_remove(&key)?;
@ -807,7 +826,7 @@ impl DiagCtxt {
/// no matching diagnostic is found. Panics if the found diagnostic's level /// no matching diagnostic is found. Panics if the found diagnostic's level
/// isn't `Level::Error`. /// isn't `Level::Error`.
pub fn try_steal_modify_and_emit_err<F>( pub fn try_steal_modify_and_emit_err<F>(
&self, self,
span: Span, span: Span,
key: StashKey, key: StashKey,
mut modify_err: F, mut modify_err: F,
@ -833,7 +852,7 @@ impl DiagCtxt {
/// [`StashKey`] as the key, cancels it if found, and emits `new_err`. /// [`StashKey`] as the key, cancels it if found, and emits `new_err`.
/// Panics if the found diagnostic's level isn't `Level::Error`. /// Panics if the found diagnostic's level isn't `Level::Error`.
pub fn try_steal_replace_and_emit_err( pub fn try_steal_replace_and_emit_err(
&self, self,
span: Span, span: Span,
key: StashKey, key: StashKey,
new_err: Diag<'_>, new_err: Diag<'_>,
@ -1106,18 +1125,18 @@ impl DiagCtxt {
// //
// Functions beginning with `struct_`/`create_` create a diagnostic. Other // Functions beginning with `struct_`/`create_` create a diagnostic. Other
// functions create and emit a diagnostic all in one go. // functions create and emit a diagnostic all in one go.
impl DiagCtxt { impl<'a> DiagCtxtHandle<'a> {
// No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing. // user-facing.
#[track_caller] #[track_caller]
pub fn struct_bug(&self, msg: impl Into<Cow<'static, str>>) -> Diag<'_, BugAbort> { pub fn struct_bug(self, msg: impl Into<Cow<'static, str>>) -> Diag<'a, BugAbort> {
Diag::new(self, Bug, msg.into()) Diag::new(self, Bug, msg.into())
} }
// No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing. // user-facing.
#[track_caller] #[track_caller]
pub fn bug(&self, msg: impl Into<Cow<'static, str>>) -> ! { pub fn bug(self, msg: impl Into<Cow<'static, str>>) -> ! {
self.struct_bug(msg).emit() self.struct_bug(msg).emit()
} }
@ -1125,111 +1144,108 @@ impl DiagCtxt {
// user-facing. // user-facing.
#[track_caller] #[track_caller]
pub fn struct_span_bug( pub fn struct_span_bug(
&self, self,
span: impl Into<MultiSpan>, span: impl Into<MultiSpan>,
msg: impl Into<Cow<'static, str>>, msg: impl Into<Cow<'static, str>>,
) -> Diag<'_, BugAbort> { ) -> Diag<'a, BugAbort> {
self.struct_bug(msg).with_span(span) self.struct_bug(msg).with_span(span)
} }
// No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing. // user-facing.
#[track_caller] #[track_caller]
pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: impl Into<Cow<'static, str>>) -> ! { pub fn span_bug(self, span: impl Into<MultiSpan>, msg: impl Into<Cow<'static, str>>) -> ! {
self.struct_span_bug(span, msg.into()).emit() self.struct_span_bug(span, msg.into()).emit()
} }
#[track_caller] #[track_caller]
pub fn create_bug<'a>(&'a self, bug: impl Diagnostic<'a, BugAbort>) -> Diag<'a, BugAbort> { pub fn create_bug(self, bug: impl Diagnostic<'a, BugAbort>) -> Diag<'a, BugAbort> {
bug.into_diag(self, Bug) bug.into_diag(self, Bug)
} }
#[track_caller] #[track_caller]
pub fn emit_bug<'a>(&'a self, bug: impl Diagnostic<'a, BugAbort>) -> ! { pub fn emit_bug(self, bug: impl Diagnostic<'a, BugAbort>) -> ! {
self.create_bug(bug).emit() self.create_bug(bug).emit()
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_fatal(&self, msg: impl Into<DiagMessage>) -> Diag<'_, FatalAbort> { pub fn struct_fatal(self, msg: impl Into<DiagMessage>) -> Diag<'a, FatalAbort> {
Diag::new(self, Fatal, msg) Diag::new(self, Fatal, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn fatal(&self, msg: impl Into<DiagMessage>) -> ! { pub fn fatal(self, msg: impl Into<DiagMessage>) -> ! {
self.struct_fatal(msg).emit() self.struct_fatal(msg).emit()
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_span_fatal( pub fn struct_span_fatal(
&self, self,
span: impl Into<MultiSpan>, span: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>, msg: impl Into<DiagMessage>,
) -> Diag<'_, FatalAbort> { ) -> Diag<'a, FatalAbort> {
self.struct_fatal(msg).with_span(span) self.struct_fatal(msg).with_span(span)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) -> ! { pub fn span_fatal(self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) -> ! {
self.struct_span_fatal(span, msg).emit() self.struct_span_fatal(span, msg).emit()
} }
#[track_caller] #[track_caller]
pub fn create_fatal<'a>( pub fn create_fatal(self, fatal: impl Diagnostic<'a, FatalAbort>) -> Diag<'a, FatalAbort> {
&'a self,
fatal: impl Diagnostic<'a, FatalAbort>,
) -> Diag<'a, FatalAbort> {
fatal.into_diag(self, Fatal) fatal.into_diag(self, Fatal)
} }
#[track_caller] #[track_caller]
pub fn emit_fatal<'a>(&'a self, fatal: impl Diagnostic<'a, FatalAbort>) -> ! { pub fn emit_fatal(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 self, self,
fatal: impl Diagnostic<'a, FatalError>, fatal: impl Diagnostic<'a, FatalError>,
) -> Diag<'a, FatalError> { ) -> Diag<'a, FatalError> {
fatal.into_diag(self, Fatal) fatal.into_diag(self, Fatal)
} }
#[track_caller] #[track_caller]
pub fn emit_almost_fatal<'a>(&'a self, fatal: impl Diagnostic<'a, FatalError>) -> FatalError { pub fn emit_almost_fatal(self, fatal: impl Diagnostic<'a, FatalError>) -> FatalError {
self.create_almost_fatal(fatal).emit() self.create_almost_fatal(fatal).emit()
} }
// FIXME: This method should be removed (every error should have an associated error code). // FIXME: This method should be removed (every error should have an associated error code).
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_err(&self, msg: impl Into<DiagMessage>) -> Diag<'_> { pub fn struct_err(self, msg: impl Into<DiagMessage>) -> Diag<'a> {
Diag::new(self, Error, msg) Diag::new(self, Error, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn err(&self, msg: impl Into<DiagMessage>) -> ErrorGuaranteed { pub fn err(self, msg: impl Into<DiagMessage>) -> ErrorGuaranteed {
self.struct_err(msg).emit() self.struct_err(msg).emit()
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_span_err( pub fn struct_span_err(
&self, self,
span: impl Into<MultiSpan>, span: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>, msg: impl Into<DiagMessage>,
) -> Diag<'_> { ) -> Diag<'a> {
self.struct_err(msg).with_span(span) self.struct_err(msg).with_span(span)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn span_err( pub fn span_err(
&self, self,
span: impl Into<MultiSpan>, span: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>, msg: impl Into<DiagMessage>,
) -> ErrorGuaranteed { ) -> ErrorGuaranteed {
@ -1237,12 +1253,12 @@ impl DiagCtxt {
} }
#[track_caller] #[track_caller]
pub fn create_err<'a>(&'a self, err: impl Diagnostic<'a>) -> Diag<'a> { pub fn create_err(self, err: impl Diagnostic<'a>) -> Diag<'a> {
err.into_diag(self, Error) err.into_diag(self, Error)
} }
#[track_caller] #[track_caller]
pub fn emit_err<'a>(&'a self, err: impl Diagnostic<'a>) -> ErrorGuaranteed { pub fn emit_err(self, err: impl Diagnostic<'a>) -> ErrorGuaranteed {
self.create_err(err).emit() self.create_err(err).emit()
} }
@ -1251,7 +1267,7 @@ impl DiagCtxt {
// No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't // No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
// user-facing. // user-facing.
#[track_caller] #[track_caller]
pub fn delayed_bug(&self, msg: impl Into<Cow<'static, str>>) -> ErrorGuaranteed { pub fn delayed_bug(self, msg: impl Into<Cow<'static, str>>) -> ErrorGuaranteed {
Diag::<ErrorGuaranteed>::new(self, DelayedBug, msg.into()).emit() Diag::<ErrorGuaranteed>::new(self, DelayedBug, msg.into()).emit()
} }
@ -1264,7 +1280,7 @@ impl DiagCtxt {
// user-facing. // user-facing.
#[track_caller] #[track_caller]
pub fn span_delayed_bug( pub fn span_delayed_bug(
&self, self,
sp: impl Into<MultiSpan>, sp: impl Into<MultiSpan>,
msg: impl Into<Cow<'static, str>>, msg: impl Into<Cow<'static, str>>,
) -> ErrorGuaranteed { ) -> ErrorGuaranteed {
@ -1273,45 +1289,45 @@ impl DiagCtxt {
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_warn(&self, msg: impl Into<DiagMessage>) -> Diag<'_, ()> { pub fn struct_warn(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
Diag::new(self, Warning, msg) Diag::new(self, Warning, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn warn(&self, msg: impl Into<DiagMessage>) { pub fn warn(self, msg: impl Into<DiagMessage>) {
self.struct_warn(msg).emit() self.struct_warn(msg).emit()
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_span_warn( pub fn struct_span_warn(
&self, self,
span: impl Into<MultiSpan>, span: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>, msg: impl Into<DiagMessage>,
) -> Diag<'_, ()> { ) -> Diag<'a, ()> {
self.struct_warn(msg).with_span(span) self.struct_warn(msg).with_span(span)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) { pub fn span_warn(self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
self.struct_span_warn(span, msg).emit() self.struct_span_warn(span, msg).emit()
} }
#[track_caller] #[track_caller]
pub fn create_warn<'a>(&'a self, warning: impl Diagnostic<'a, ()>) -> Diag<'a, ()> { pub fn create_warn(self, warning: impl Diagnostic<'a, ()>) -> Diag<'a, ()> {
warning.into_diag(self, Warning) warning.into_diag(self, Warning)
} }
#[track_caller] #[track_caller]
pub fn emit_warn<'a>(&'a self, warning: impl Diagnostic<'a, ()>) { pub fn emit_warn(self, warning: impl Diagnostic<'a, ()>) {
self.create_warn(warning).emit() self.create_warn(warning).emit()
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_note(&self, msg: impl Into<DiagMessage>) -> Diag<'_, ()> { pub fn struct_note(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
Diag::new(self, Note, msg) Diag::new(self, Note, msg)
} }
@ -1324,54 +1340,50 @@ impl DiagCtxt {
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_span_note( pub fn struct_span_note(
&self, self,
span: impl Into<MultiSpan>, span: impl Into<MultiSpan>,
msg: impl Into<DiagMessage>, msg: impl Into<DiagMessage>,
) -> Diag<'_, ()> { ) -> Diag<'a, ()> {
self.struct_note(msg).with_span(span) self.struct_note(msg).with_span(span)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn span_note(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) { pub fn span_note(self, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
self.struct_span_note(span, msg).emit() self.struct_span_note(span, msg).emit()
} }
#[track_caller] #[track_caller]
pub fn create_note<'a>(&'a self, note: impl Diagnostic<'a, ()>) -> Diag<'a, ()> { pub fn create_note(self, note: impl Diagnostic<'a, ()>) -> Diag<'a, ()> {
note.into_diag(self, Note) note.into_diag(self, Note)
} }
#[track_caller] #[track_caller]
pub fn emit_note<'a>(&'a self, note: impl Diagnostic<'a, ()>) { pub fn emit_note(self, note: impl Diagnostic<'a, ()>) {
self.create_note(note).emit() self.create_note(note).emit()
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_help(&self, msg: impl Into<DiagMessage>) -> Diag<'_, ()> { pub fn struct_help(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
Diag::new(self, Help, msg) Diag::new(self, Help, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_failure_note(&self, msg: impl Into<DiagMessage>) -> Diag<'_, ()> { pub fn struct_failure_note(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
Diag::new(self, FailureNote, msg) Diag::new(self, FailureNote, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_allow(&self, msg: impl Into<DiagMessage>) -> Diag<'_, ()> { pub fn struct_allow(self, msg: impl Into<DiagMessage>) -> Diag<'a, ()> {
Diag::new(self, Allow, msg) Diag::new(self, Allow, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn struct_expect( pub fn struct_expect(self, msg: impl Into<DiagMessage>, id: LintExpectationId) -> Diag<'a, ()> {
&self,
msg: impl Into<DiagMessage>,
id: LintExpectationId,
) -> Diag<'_, ()> {
Diag::new(self, Expect(id), msg) Diag::new(self, Expect(id), msg)
} }
} }

View File

@ -12,7 +12,7 @@ use rustc_ast::{self as ast, AttrVec, Attribute, HasAttrs, Item, NodeId, PatKind
use rustc_attr::{self as attr, Deprecation, Stability}; use rustc_attr::{self as attr, Deprecation, Stability};
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::{self, Lrc}; use rustc_data_structures::sync::{self, Lrc};
use rustc_errors::{DiagCtxt, ErrorGuaranteed, PResult}; use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, PResult};
use rustc_feature::Features; use rustc_feature::Features;
use rustc_lint_defs::{BufferedEarlyLint, RegisteredTools}; use rustc_lint_defs::{BufferedEarlyLint, RegisteredTools};
use rustc_parse::{parser::Parser, MACRO_ARGUMENTS}; use rustc_parse::{parser::Parser, MACRO_ARGUMENTS};
@ -1135,7 +1135,7 @@ impl<'a> ExtCtxt<'a> {
} }
} }
pub fn dcx(&self) -> &'a DiagCtxt { pub fn dcx(&self) -> DiagCtxtHandle<'a> {
self.sess.dcx() self.sess.dcx()
} }
@ -1256,7 +1256,7 @@ pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PRe
} }
pub fn parse_macro_name_and_helper_attrs( pub fn parse_macro_name_and_helper_attrs(
dcx: &rustc_errors::DiagCtxt, dcx: DiagCtxtHandle<'_>,
attr: &Attribute, attr: &Attribute,
macro_type: &str, macro_type: &str,
) -> Option<(Symbol, Vec<Symbol>)> { ) -> Option<(Symbol, Vec<Symbol>)> {

View File

@ -7,7 +7,7 @@ use crate::mbe::{
use rustc_ast::token::{self, Token, TokenKind}; use rustc_ast::token::{self, Token, TokenKind};
use rustc_ast::tokenstream::TokenStream; use rustc_ast::tokenstream::TokenStream;
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_errors::{Applicability, Diag, DiagCtxt, DiagMessage}; use rustc_errors::{Applicability, Diag, DiagCtxtHandle, DiagMessage};
use rustc_macros::Subdiagnostic; use rustc_macros::Subdiagnostic;
use rustc_parse::parser::{Parser, Recovery}; use rustc_parse::parser::{Parser, Recovery};
use rustc_span::source_map::SourceMap; use rustc_span::source_map::SourceMap;
@ -324,7 +324,12 @@ enum ExplainDocComment {
}, },
} }
pub(super) fn annotate_doc_comment(dcx: &DiagCtxt, err: &mut Diag<'_>, sm: &SourceMap, span: Span) { pub(super) fn annotate_doc_comment(
dcx: DiagCtxtHandle<'_>,
err: &mut Diag<'_>,
sm: &SourceMap,
span: Span,
) {
if let Ok(src) = sm.span_to_snippet(span) { if let Ok(src) = sm.span_to_snippet(span) {
if src.starts_with("///") || src.starts_with("/**") { if src.starts_with("///") || src.starts_with("/**") {
err.subdiagnostic(dcx, ExplainDocComment::Outer { span }); err.subdiagnostic(dcx, ExplainDocComment::Outer { span });

View File

@ -10,7 +10,7 @@ use rustc_ast::token::IdentIsRaw;
use rustc_ast::token::{self, Delimiter, Token, TokenKind}; use rustc_ast::token::{self, Delimiter, Token, TokenKind};
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree}; use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{pluralize, Diag, DiagCtxt, PResult}; use rustc_errors::{pluralize, Diag, DiagCtxtHandle, PResult};
use rustc_parse::parser::ParseNtResult; use rustc_parse::parser::ParseNtResult;
use rustc_session::parse::ParseSess; use rustc_session::parse::ParseSess;
use rustc_span::hygiene::{LocalExpnId, Transparency}; use rustc_span::hygiene::{LocalExpnId, Transparency};
@ -571,7 +571,7 @@ fn lockstep_iter_size(
/// * `[ $( ${count(foo, 1)} ),* ]` will return an error because `${count(foo, 1)}` is /// * `[ $( ${count(foo, 1)} ),* ]` will return an error because `${count(foo, 1)}` is
/// declared inside a single repetition and the index `1` implies two nested repetitions. /// declared inside a single repetition and the index `1` implies two nested repetitions.
fn count_repetitions<'a>( fn count_repetitions<'a>(
dcx: &'a DiagCtxt, dcx: DiagCtxtHandle<'a>,
depth_user: usize, depth_user: usize,
mut matched: &NamedMatch, mut matched: &NamedMatch,
repeats: &[(usize, usize)], repeats: &[(usize, usize)],
@ -632,7 +632,7 @@ fn count_repetitions<'a>(
/// Returns a `NamedMatch` item declared on the LHS given an arbitrary [Ident] /// Returns a `NamedMatch` item declared on the LHS given an arbitrary [Ident]
fn matched_from_ident<'ctx, 'interp, 'rslt>( fn matched_from_ident<'ctx, 'interp, 'rslt>(
dcx: &'ctx DiagCtxt, dcx: DiagCtxtHandle<'ctx>,
ident: Ident, ident: Ident,
interp: &'interp FxHashMap<MacroRulesNormalizedIdent, NamedMatch>, interp: &'interp FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
) -> PResult<'ctx, &'rslt NamedMatch> ) -> PResult<'ctx, &'rslt NamedMatch>
@ -646,7 +646,7 @@ where
/// Used by meta-variable expressions when an user input is out of the actual declared bounds. For /// Used by meta-variable expressions when an user input is out of the actual declared bounds. For
/// example, index(999999) in an repetition of only three elements. /// example, index(999999) in an repetition of only three elements.
fn out_of_bounds_err<'a>(dcx: &'a DiagCtxt, max: usize, span: Span, ty: &str) -> Diag<'a> { fn out_of_bounds_err<'a>(dcx: DiagCtxtHandle<'a>, max: usize, span: Span, ty: &str) -> Diag<'a> {
let msg = if max == 0 { let msg = if max == 0 {
format!( format!(
"meta-variable expression `{ty}` with depth parameter \ "meta-variable expression `{ty}` with depth parameter \
@ -662,7 +662,7 @@ fn out_of_bounds_err<'a>(dcx: &'a DiagCtxt, max: usize, span: Span, ty: &str) ->
} }
fn transcribe_metavar_expr<'a>( fn transcribe_metavar_expr<'a>(
dcx: &'a DiagCtxt, dcx: DiagCtxtHandle<'a>,
expr: &MetaVarExpr, expr: &MetaVarExpr,
interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>, interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
marker: &mut Marker, marker: &mut Marker,
@ -730,7 +730,7 @@ fn transcribe_metavar_expr<'a>(
/// Extracts an identifier that can be originated from a `$var:ident` variable or from a token tree. /// Extracts an identifier that can be originated from a `$var:ident` variable or from a token tree.
fn extract_ident<'a>( fn extract_ident<'a>(
dcx: &'a DiagCtxt, dcx: DiagCtxtHandle<'a>,
ident: Ident, ident: Ident,
interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>, interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
) -> PResult<'a, String> { ) -> PResult<'a, String> {

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, Diagnostic, EmissionGuarantee, Level, MultiSpan, codes::*, Applicability, Diag, DiagCtxtHandle, 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;
@ -424,7 +424,7 @@ pub struct MissingTypeParams {
// Manual implementation of `Diagnostic` to be able to call `span_to_snippet`. // Manual implementation of `Diagnostic` to be able to call `span_to_snippet`.
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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

@ -5,14 +5,13 @@ mod checks;
mod inspect_obligations; mod inspect_obligations;
mod suggestions; mod suggestions;
use rustc_errors::ErrorGuaranteed; use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
use crate::coercion::DynamicCoerceMany; use crate::coercion::DynamicCoerceMany;
use crate::fallback::DivergingFallbackBehavior; use crate::fallback::DivergingFallbackBehavior;
use crate::fn_ctxt::checks::DivergingBlockBehavior; use crate::fn_ctxt::checks::DivergingBlockBehavior;
use crate::{CoroutineTypes, Diverges, EnclosingBreakables, TypeckRootCtxt}; use crate::{CoroutineTypes, Diverges, EnclosingBreakables, TypeckRootCtxt};
use hir::def_id::CRATE_DEF_ID; use hir::def_id::CRATE_DEF_ID;
use rustc_errors::DiagCtxt;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, RegionInferReason}; use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
@ -145,7 +144,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
} }
pub(crate) fn dcx(&self) -> &'tcx DiagCtxt { pub(crate) fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.infcx.dcx() self.infcx.dcx()
} }

View File

@ -61,8 +61,8 @@ use crate::traits::{
use crate::infer::relate::{self, RelateResult, TypeRelation}; use crate::infer::relate::{self, RelateResult, TypeRelation};
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::{ use rustc_errors::{
codes::*, pluralize, struct_span_code_err, Applicability, Diag, DiagCtxt, DiagStyledString, codes::*, pluralize, struct_span_code_err, Applicability, Diag, DiagCtxtHandle,
ErrorGuaranteed, IntoDiagArg, StringPart, DiagStyledString, ErrorGuaranteed, IntoDiagArg, StringPart,
}; };
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
@ -139,7 +139,7 @@ pub struct TypeErrCtxt<'a, 'tcx> {
} }
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
pub fn dcx(&self) -> &'tcx DiagCtxt { pub fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.infcx.dcx() self.infcx.dcx()
} }

View File

@ -4,6 +4,7 @@ pub use lexical_region_resolve::RegionResolutionError;
pub use relate::combine::CombineFields; pub use relate::combine::CombineFields;
pub use relate::combine::PredicateEmittingRelation; pub use relate::combine::PredicateEmittingRelation;
pub use relate::StructurallyRelateAliases; pub use relate::StructurallyRelateAliases;
use rustc_errors::DiagCtxtHandle;
pub use rustc_macros::{TypeFoldable, TypeVisitable}; pub use rustc_macros::{TypeFoldable, TypeVisitable};
pub use rustc_middle::ty::IntVarValue; pub use rustc_middle::ty::IntVarValue;
pub use BoundRegionConversionTime::*; pub use BoundRegionConversionTime::*;
@ -23,7 +24,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_data_structures::undo_log::Rollback; use rustc_data_structures::undo_log::Rollback;
use rustc_data_structures::unify as ut; use rustc_data_structures::unify as ut;
use rustc_errors::{Diag, DiagCtxt, ErrorGuaranteed}; use rustc_errors::{Diag, ErrorGuaranteed};
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_macros::extension; use rustc_macros::extension;
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues}; use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
@ -826,7 +827,7 @@ impl<'tcx> InferOk<'tcx, ()> {
} }
impl<'tcx> InferCtxt<'tcx> { impl<'tcx> InferCtxt<'tcx> {
pub fn dcx(&self) -> &'tcx DiagCtxt { pub fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.tcx.dcx() self.tcx.dcx()
} }

View File

@ -9,7 +9,7 @@ use rustc_data_structures::jobserver;
use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::registry::Registry; use rustc_errors::registry::Registry;
use rustc_errors::{DiagCtxt, ErrorGuaranteed}; use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
use rustc_lint::LintStore; use rustc_lint::LintStore;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_middle::ty::CurrentGcx; use rustc_middle::ty::CurrentGcx;
@ -46,7 +46,7 @@ pub struct Compiler {
} }
/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`. /// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.
pub(crate) fn parse_cfg(dcx: &DiagCtxt, cfgs: Vec<String>) -> Cfg { pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
cfgs.into_iter() cfgs.into_iter()
.map(|s| { .map(|s| {
let psess = ParseSess::with_silent_emitter( let psess = ParseSess::with_silent_emitter(
@ -105,7 +105,7 @@ pub(crate) fn parse_cfg(dcx: &DiagCtxt, cfgs: Vec<String>) -> Cfg {
} }
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`. /// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg { pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> CheckCfg {
// If any --check-cfg is passed then exhaustive_values and exhaustive_names // If any --check-cfg is passed then exhaustive_values and exhaustive_names
// are enabled by default. // are enabled by default.
let exhaustive_names = !specs.is_empty(); let exhaustive_names = !specs.is_empty();
@ -451,12 +451,12 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
codegen_backend.init(&sess); codegen_backend.init(&sess);
let cfg = parse_cfg(&sess.dcx(), config.crate_cfg); let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
let mut cfg = config::build_configuration(&sess, cfg); let mut cfg = config::build_configuration(&sess, cfg);
util::add_configuration(&mut cfg, &mut sess, &*codegen_backend); util::add_configuration(&mut cfg, &mut sess, &*codegen_backend);
sess.psess.config = cfg; sess.psess.config = cfg;
let mut check_cfg = parse_check_cfg(&sess.dcx(), config.crate_check_cfg); let mut check_cfg = parse_check_cfg(sess.dcx(), config.crate_check_cfg);
check_cfg.fill_well_known(&sess.target); check_cfg.fill_well_known(&sess.target);
sess.psess.check_config = check_cfg; sess.psess.check_config = check_cfg;
@ -529,7 +529,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
} }
pub fn try_print_query_stack( pub fn try_print_query_stack(
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
num_frames: Option<usize>, num_frames: Option<usize>,
file: Option<std::fs::File>, file: Option<std::fs::File>,
) { ) {

View File

@ -70,7 +70,7 @@ where
Arc::default(), Arc::default(),
Default::default(), Default::default(),
); );
let cfg = parse_cfg(&sess.dcx(), matches.opt_strs("cfg")); let cfg = parse_cfg(sess.dcx(), matches.opt_strs("cfg"));
let cfg = build_configuration(&sess, cfg); let cfg = build_configuration(&sess, cfg);
f(sess, cfg) f(sess, cfg)
}); });

View File

@ -1405,7 +1405,7 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
diag.note(fluent::lint_macro_to_change); diag.note(fluent::lint_macro_to_change);
} }
if let Some(cargo_update) = cargo_update { if let Some(cargo_update) = cargo_update {
diag.subdiagnostic(&diag.dcx, cargo_update); diag.subdiagnostic(diag.dcx, cargo_update);
} }
if has_trait { if has_trait {
@ -1471,7 +1471,7 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
diag.note(fluent::lint_non_local_definitions_deprecation); diag.note(fluent::lint_non_local_definitions_deprecation);
if let Some(cargo_update) = cargo_update { if let Some(cargo_update) = cargo_update {
diag.subdiagnostic(&diag.dcx, cargo_update); diag.subdiagnostic(diag.dcx, cargo_update);
} }
} }
} }

View File

@ -78,7 +78,7 @@ impl<'a> DiagnosticDerive<'a> {
#[track_caller] #[track_caller]
fn into_diag( fn into_diag(
self, self,
dcx: &'_sess rustc_errors::DiagCtxt, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
level: rustc_errors::Level level: rustc_errors::Level
) -> rustc_errors::Diag<'_sess, G> { ) -> rustc_errors::Diag<'_sess, G> {
#implementation #implementation

View File

@ -10,7 +10,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::owned_slice::OwnedSlice; use rustc_data_structures::owned_slice::OwnedSlice;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard}; use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard};
use rustc_errors::DiagCtxt; use rustc_errors::DiagCtxtHandle;
use rustc_expand::base::SyntaxExtension; use rustc_expand::base::SyntaxExtension;
use rustc_fs_util::try_canonicalize; use rustc_fs_util::try_canonicalize;
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
@ -91,7 +91,7 @@ impl<'a, 'tcx> std::ops::Deref for CrateLoader<'a, 'tcx> {
} }
impl<'a, 'tcx> CrateLoader<'a, 'tcx> { impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
fn dcx(&self) -> &'tcx DiagCtxt { fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.tcx.dcx() self.tcx.dcx()
} }
} }

View File

@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use rustc_errors::{codes::*, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level}; use rustc_errors::{codes::*, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{sym, Span, Symbol}; use rustc_span::{sym, Span, Symbol};
use rustc_target::spec::{PanicStrategy, TargetTriple}; use rustc_target::spec::{PanicStrategy, TargetTriple};
@ -503,7 +503,7 @@ pub(crate) struct MultipleCandidates {
} }
impl<G: EmissionGuarantee> Diagnostic<'_, G> for MultipleCandidates { impl<G: EmissionGuarantee> Diagnostic<'_, G> for MultipleCandidates {
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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);
@ -602,7 +602,7 @@ pub struct InvalidMetadataFiles {
impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidMetadataFiles { impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidMetadataFiles {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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);
@ -631,7 +631,7 @@ pub struct CannotFindCrate {
impl<G: EmissionGuarantee> Diagnostic<'_, G> for CannotFindCrate { impl<G: EmissionGuarantee> Diagnostic<'_, G> for CannotFindCrate {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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

@ -4,10 +4,10 @@
/// ///
/// If you have a span available, you should use [`span_bug`] instead. /// If you have a span available, you should use [`span_bug`] instead.
/// ///
/// If the bug should only be emitted when compilation didn't fail, [`DiagCtxt::span_delayed_bug`] /// If the bug should only be emitted when compilation didn't fail, [`DiagCtxtHandle::span_delayed_bug`]
/// may be useful. /// may be useful.
/// ///
/// [`DiagCtxt::span_delayed_bug`]: rustc_errors::DiagCtxt::span_delayed_bug /// [`DiagCtxtHandle::span_delayed_bug`]: rustc_errors::DiagCtxtHandle::span_delayed_bug
/// [`span_bug`]: crate::span_bug /// [`span_bug`]: crate::span_bug
#[macro_export] #[macro_export]
macro_rules! bug { macro_rules! bug {
@ -30,10 +30,10 @@ macro_rules! bug {
/// at the code the compiler was compiling when it ICEd. This is the preferred way to trigger /// at the code the compiler was compiling when it ICEd. This is the preferred way to trigger
/// ICEs. /// ICEs.
/// ///
/// If the bug should only be emitted when compilation didn't fail, [`DiagCtxt::span_delayed_bug`] /// If the bug should only be emitted when compilation didn't fail, [`DiagCtxtHandle::span_delayed_bug`]
/// may be useful. /// may be useful.
/// ///
/// [`DiagCtxt::span_delayed_bug`]: rustc_errors::DiagCtxt::span_delayed_bug /// [`DiagCtxtHandle::span_delayed_bug`]: rustc_errors::DiagCtxtHandle::span_delayed_bug
#[macro_export] #[macro_export]
macro_rules! span_bug { macro_rules! span_bug {
($span:expr, $msg:expr) => ( ($span:expr, $msg:expr) => (

View File

@ -47,7 +47,9 @@ use rustc_data_structures::sync::{self, FreezeReadGuard, Lock, Lrc, RwLock, Work
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
use rustc_data_structures::sync::{DynSend, DynSync}; use rustc_data_structures::sync::{DynSend, DynSync};
use rustc_data_structures::unord::UnordSet; use rustc_data_structures::unord::UnordSet;
use rustc_errors::{Applicability, Diag, DiagCtxt, ErrorGuaranteed, LintDiagnostic, MultiSpan}; use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, LintDiagnostic, MultiSpan,
};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
@ -1415,7 +1417,7 @@ impl<'tcx> TyCtxt<'tcx> {
) )
} }
pub fn dcx(self) -> &'tcx DiagCtxt { pub fn dcx(self) -> DiagCtxtHandle<'tcx> {
self.sess.dcx() self.sess.dcx()
} }
} }

View File

@ -5,7 +5,7 @@ use crate::ty::normalize_erasing_regions::NormalizationError;
use crate::ty::{self, CoroutineArgsExt, Ty, TyCtxt, TypeVisitableExt}; use crate::ty::{self, CoroutineArgsExt, Ty, TyCtxt, TypeVisitableExt};
use rustc_error_messages::DiagMessage; use rustc_error_messages::DiagMessage;
use rustc_errors::{ use rustc_errors::{
Diag, DiagArgValue, DiagCtxt, Diagnostic, EmissionGuarantee, IntoDiagArg, Level, Diag, DiagArgValue, DiagCtxtHandle, 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;
@ -1256,7 +1256,7 @@ pub enum FnAbiError<'tcx> {
} }
impl<'a, 'b, G: EmissionGuarantee> Diagnostic<'a, G> for FnAbiError<'b> { impl<'a, 'b, G: EmissionGuarantee> Diagnostic<'a, G> for FnAbiError<'b> {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
match self { match self {
Self::Layout(e) => e.into_diagnostic().into_diag(dcx, level), Self::Layout(e) => e.into_diagnostic().into_diag(dcx, level),
Self::AdjustForForeignAbi(call::AdjustForForeignAbiError::Unsupported { Self::AdjustForForeignAbi(call::AdjustForForeignAbiError::Unsupported {

View File

@ -1,9 +1,9 @@
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_errors::DiagArgValue;
use rustc_errors::{ use rustc_errors::{
codes::*, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level, MultiSpan, codes::*, Applicability, Diag, Diagnostic, EmissionGuarantee, Level, MultiSpan,
SubdiagMessageOp, Subdiagnostic, SubdiagMessageOp, Subdiagnostic,
}; };
use rustc_errors::{DiagArgValue, DiagCtxtHandle};
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty};
use rustc_pattern_analysis::{errors::Uncovered, rustc::RustcPatCtxt}; use rustc_pattern_analysis::{errors::Uncovered, rustc::RustcPatCtxt};
@ -492,7 +492,7 @@ pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> {
} }
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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.scrut_span); diag.span(self.scrut_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, Diagnostic, EmissionGuarantee, Level}; use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
use rustc_macros::{Diagnostic, LintDiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic};
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
@ -48,7 +48,7 @@ pub struct UnusedGenericParamsHint {
impl<G: EmissionGuarantee> Diagnostic<'_, G> for UnusedGenericParamsHint { impl<G: EmissionGuarantee> Diagnostic<'_, G> for UnusedGenericParamsHint {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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,7 +3,7 @@ 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::*, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level, codes::*, Applicability, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level,
SubdiagMessageOp, Subdiagnostic, SubdiagMessageOp, Subdiagnostic,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
@ -1052,7 +1052,7 @@ pub(crate) struct ExpectedIdentifier {
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedIdentifier { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedIdentifier {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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(
@ -1112,7 +1112,7 @@ pub(crate) struct ExpectedSemi {
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedSemi { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedSemi {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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

@ -7,7 +7,7 @@ use rustc_ast::ast::{self, AttrStyle};
use rustc_ast::token::{self, CommentKind, Delimiter, IdentIsRaw, Token, TokenKind}; use rustc_ast::token::{self, CommentKind, Delimiter, IdentIsRaw, Token, TokenKind};
use rustc_ast::tokenstream::TokenStream; use rustc_ast::tokenstream::TokenStream;
use rustc_ast::util::unicode::contains_text_flow_control_chars; use rustc_ast::util::unicode::contains_text_flow_control_chars;
use rustc_errors::{codes::*, Applicability, Diag, DiagCtxt, StashKey}; use rustc_errors::{codes::*, Applicability, Diag, DiagCtxtHandle, StashKey};
use rustc_lexer::unescape::{self, EscapeError, Mode}; use rustc_lexer::unescape::{self, EscapeError, Mode};
use rustc_lexer::{Base, DocStyle, RawStrError}; use rustc_lexer::{Base, DocStyle, RawStrError};
use rustc_lexer::{Cursor, LiteralKind}; use rustc_lexer::{Cursor, LiteralKind};
@ -113,7 +113,7 @@ struct StringReader<'psess, 'src> {
} }
impl<'psess, 'src> StringReader<'psess, 'src> { impl<'psess, 'src> StringReader<'psess, 'src> {
fn dcx(&self) -> &'psess DiagCtxt { fn dcx(&self) -> DiagCtxtHandle<'psess> {
self.psess.dcx() self.psess.dcx()
} }

View File

@ -3,7 +3,7 @@
use std::iter::once; use std::iter::once;
use std::ops::Range; use std::ops::Range;
use rustc_errors::{Applicability, DiagCtxt, ErrorGuaranteed}; use rustc_errors::{Applicability, DiagCtxtHandle, ErrorGuaranteed};
use rustc_lexer::unescape::{EscapeError, Mode}; use rustc_lexer::unescape::{EscapeError, Mode};
use rustc_span::{BytePos, Span}; use rustc_span::{BytePos, Span};
use tracing::debug; use tracing::debug;
@ -11,7 +11,7 @@ use tracing::debug;
use crate::errors::{MoreThanOneCharNote, MoreThanOneCharSugg, NoBraceUnicodeSub, UnescapeError}; use crate::errors::{MoreThanOneCharNote, MoreThanOneCharSugg, NoBraceUnicodeSub, UnescapeError};
pub(crate) fn emit_unescape_error( pub(crate) fn emit_unescape_error(
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
// interior part of the literal, between quotes // interior part of the literal, between quotes
lit: &str, lit: &str,
// full span of the literal, including quotes and any prefix // full span of the literal, including quotes and any prefix

View File

@ -34,7 +34,7 @@ use rustc_ast::{
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{ use rustc_errors::{
pluralize, Applicability, Diag, DiagCtxt, ErrorGuaranteed, FatalError, PErr, PResult, pluralize, Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, PErr, PResult,
Subdiagnostic, Subdiagnostic,
}; };
use rustc_session::errors::ExprParenthesesNeeded; use rustc_session::errors::ExprParenthesesNeeded;
@ -240,7 +240,7 @@ impl<'a> DerefMut for SnapshotParser<'a> {
} }
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
pub fn dcx(&self) -> &'a DiagCtxt { pub fn dcx(&self) -> DiagCtxtHandle<'a> {
self.psess.dcx() self.psess.dcx()
} }

View File

@ -61,7 +61,7 @@ where
{ {
let mut p = string_to_parser(&psess, s); let mut p = string_to_parser(&psess, s);
let x = f(&mut p).unwrap(); let x = f(&mut p).unwrap();
p.psess.dcx.abort_if_errors(); p.dcx().abort_if_errors();
x x
} }
@ -193,7 +193,7 @@ impl<T: Write> Write for Shared<T> {
#[allow(rustc::untranslatable_diagnostic)] // no translation needed for tests #[allow(rustc::untranslatable_diagnostic)] // no translation needed for tests
fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) { fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
create_default_session_globals_then(|| { create_default_session_globals_then(|| {
let (handler, source_map, output) = create_test_handler(); let (dcx, source_map, output) = create_test_handler();
source_map.new_source_file(Path::new("test.rs").to_owned().into(), file_text.to_owned()); source_map.new_source_file(Path::new("test.rs").to_owned().into(), file_text.to_owned());
let primary_span = make_span(&file_text, &span_labels[0].start, &span_labels[0].end); let primary_span = make_span(&file_text, &span_labels[0].start, &span_labels[0].end);
@ -205,7 +205,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
println!("text: {:?}", source_map.span_to_snippet(span)); println!("text: {:?}", source_map.span_to_snippet(span));
} }
handler.span_err(msp, "foo"); dcx.handle().span_err(msp, "foo");
assert!( assert!(
expected_output.chars().next() == Some('\n'), expected_output.chars().next() == Some('\n'),

View File

@ -8,8 +8,8 @@ use crate::{errors, fluent_generated as fluent};
use rustc_ast::{ast, AttrKind, AttrStyle, Attribute, LitKind}; use rustc_ast::{ast, AttrKind, AttrStyle, Attribute, LitKind};
use rustc_ast::{MetaItemKind, MetaItemLit, NestedMetaItem}; use rustc_ast::{MetaItemKind, MetaItemLit, NestedMetaItem};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::StashKey; use rustc_errors::{Applicability, IntoDiagArg, MultiSpan};
use rustc_errors::{Applicability, DiagCtxt, IntoDiagArg, MultiSpan}; use rustc_errors::{DiagCtxtHandle, StashKey};
use rustc_feature::{ use rustc_feature::{
is_unsafe_attr, AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP, is_unsafe_attr, AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP,
}; };
@ -99,7 +99,7 @@ struct CheckAttrVisitor<'tcx> {
} }
impl<'tcx> CheckAttrVisitor<'tcx> { impl<'tcx> CheckAttrVisitor<'tcx> {
fn dcx(&self) -> &'tcx DiagCtxt { fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.tcx.dcx() self.tcx.dcx()
} }

View File

@ -6,8 +6,8 @@ use std::{
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_ast::{ast, Label}; use rustc_ast::{ast, Label};
use rustc_errors::{ use rustc_errors::{
codes::*, Applicability, Diag, DiagCtxt, DiagSymbolList, Diagnostic, EmissionGuarantee, Level, codes::*, Applicability, Diag, DiagCtxtHandle, DiagSymbolList, Diagnostic, EmissionGuarantee,
MultiSpan, SubdiagMessageOp, Subdiagnostic, Level, MultiSpan, SubdiagMessageOp, Subdiagnostic,
}; };
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};
@ -880,7 +880,7 @@ pub struct ItemFollowingInnerAttr {
impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidAttrAtCrateLevel { impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidAttrAtCrateLevel {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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);
@ -1030,7 +1030,7 @@ pub struct BreakNonLoop<'a> {
impl<'a, G: EmissionGuarantee> Diagnostic<'_, G> for BreakNonLoop<'a> { impl<'a, G: EmissionGuarantee> Diagnostic<'_, G> for BreakNonLoop<'a> {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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);
@ -1176,7 +1176,7 @@ pub struct NakedFunctionsAsmBlock {
impl<G: EmissionGuarantee> Diagnostic<'_, G> for NakedFunctionsAsmBlock { impl<G: EmissionGuarantee> Diagnostic<'_, G> for NakedFunctionsAsmBlock {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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);
@ -1264,7 +1264,7 @@ pub struct NoMainErr {
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NoMainErr { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NoMainErr {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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);
@ -1322,7 +1322,7 @@ pub struct DuplicateLangItem {
impl<G: EmissionGuarantee> Diagnostic<'_, G> for DuplicateLangItem { impl<G: EmissionGuarantee> Diagnostic<'_, G> for DuplicateLangItem {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new( let mut diag = Diag::new(
dcx, dcx,
level, level,

View File

@ -4,7 +4,7 @@ use crate::query::plumbing::CycleError;
use crate::query::DepKind; use crate::query::DepKind;
use crate::query::{QueryContext, QueryStackFrame}; use crate::query::{QueryContext, QueryStackFrame};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Diag, DiagCtxt}; use rustc_errors::{Diag, DiagCtxtHandle};
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_session::Session; use rustc_session::Session;
use rustc_span::Span; use rustc_span::Span;
@ -600,7 +600,7 @@ pub fn report_cycle<'a>(
pub fn print_query_stack<Qcx: QueryContext>( pub fn print_query_stack<Qcx: QueryContext>(
qcx: Qcx, qcx: Qcx,
mut current_query: Option<QueryJobId>, mut current_query: Option<QueryJobId>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
num_frames: Option<usize>, num_frames: Option<usize>,
mut file: Option<std::fs::File>, mut file: Option<std::fs::File>,
) -> usize { ) -> usize {

View File

@ -6,7 +6,7 @@ use rustc_ast::{MetaItemKind, NestedMetaItem};
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{ use rustc_errors::{
codes::*, report_ambiguity_error, struct_span_code_err, Applicability, Diag, DiagCtxt, codes::*, report_ambiguity_error, struct_span_code_err, Applicability, Diag, DiagCtxtHandle,
ErrorGuaranteed, MultiSpan, SuggestionStyle, ErrorGuaranteed, MultiSpan, SuggestionStyle,
}; };
use rustc_feature::BUILTIN_ATTRIBUTES; use rustc_feature::BUILTIN_ATTRIBUTES;
@ -120,7 +120,7 @@ fn reduce_impl_span_to_impl_keyword(sm: &SourceMap, impl_span: Span) -> Span {
} }
impl<'a, 'tcx> Resolver<'a, 'tcx> { impl<'a, 'tcx> Resolver<'a, 'tcx> {
pub(crate) fn dcx(&self) -> &'tcx DiagCtxt { pub(crate) fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.tcx.dcx() self.tcx.dcx()
} }

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, Diagnostic, EmissionGuarantee, ErrorGuaranteed, Level, codes::*, Diag, DiagCtxtHandle, DiagMessage, Diagnostic, EmissionGuarantee, ErrorGuaranteed,
MultiSpan, Level, MultiSpan,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
@ -19,7 +19,7 @@ pub(crate) struct FeatureGateError {
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for FeatureGateError { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for FeatureGateError {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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

@ -15,8 +15,8 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::sync::{AppendOnlyVec, Lock, Lrc}; use rustc_data_structures::sync::{AppendOnlyVec, Lock, Lrc};
use rustc_errors::emitter::{stderr_destination, HumanEmitter, SilentEmitter}; use rustc_errors::emitter::{stderr_destination, HumanEmitter, SilentEmitter};
use rustc_errors::{ use rustc_errors::{
fallback_fluent_bundle, ColorConfig, Diag, DiagCtxt, DiagMessage, EmissionGuarantee, MultiSpan, fallback_fluent_bundle, ColorConfig, Diag, DiagCtxt, DiagCtxtHandle, DiagMessage,
StashKey, EmissionGuarantee, MultiSpan, StashKey,
}; };
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures}; use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
@ -327,7 +327,7 @@ impl ParseSess {
self.proc_macro_quoted_spans.iter_enumerated() self.proc_macro_quoted_spans.iter_enumerated()
} }
pub fn dcx(&self) -> &DiagCtxt { pub fn dcx(&self) -> DiagCtxtHandle<'_> {
&self.dcx self.dcx.handle()
} }
} }

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, Diagnostic, ErrorGuaranteed, codes::*, fallback_fluent_bundle, Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic,
FatalAbort, FluentBundle, LazyFallbackBundle, TerminalUrl, ErrorGuaranteed, 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;
@ -328,7 +328,7 @@ impl Session {
} }
#[inline] #[inline]
pub fn dcx(&self) -> &DiagCtxt { pub fn dcx(&self) -> DiagCtxtHandle<'_> {
self.psess.dcx() self.psess.dcx()
} }
@ -1065,7 +1065,7 @@ pub fn build_session(
match profiler { match profiler {
Ok(profiler) => Some(Arc::new(profiler)), Ok(profiler) => Some(Arc::new(profiler)),
Err(e) => { Err(e) => {
dcx.emit_warn(errors::FailedToCreateProfiler { err: e.to_string() }); dcx.handle().emit_warn(errors::FailedToCreateProfiler { err: e.to_string() });
None None
} }
} }
@ -1366,7 +1366,7 @@ impl EarlyDiagCtxt {
/// format. Any errors prior to that will cause an abort and all stashed diagnostics of the /// format. Any errors prior to that will cause an abort and all stashed diagnostics of the
/// previous dcx will be emitted. /// previous dcx will be emitted.
pub fn abort_if_error_and_set_error_format(&mut self, output: ErrorOutputType) { pub fn abort_if_error_and_set_error_format(&mut self, output: ErrorOutputType) {
self.dcx.abort_if_errors(); self.dcx.handle().abort_if_errors();
let emitter = mk_emitter(output); let emitter = mk_emitter(output);
self.dcx = DiagCtxt::new(emitter); self.dcx = DiagCtxt::new(emitter);
@ -1375,44 +1375,44 @@ impl EarlyDiagCtxt {
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_note(&self, msg: impl Into<DiagMessage>) { pub fn early_note(&self, msg: impl Into<DiagMessage>) {
self.dcx.note(msg) self.dcx.handle().note(msg)
} }
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_help(&self, msg: impl Into<DiagMessage>) { pub fn early_help(&self, msg: impl Into<DiagMessage>) {
self.dcx.struct_help(msg).emit() self.dcx.handle().struct_help(msg).emit()
} }
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
#[must_use = "ErrorGuaranteed must be returned from `run_compiler` in order to exit with a non-zero status code"] #[must_use = "ErrorGuaranteed must be returned from `run_compiler` in order to exit with a non-zero status code"]
pub fn early_err(&self, msg: impl Into<DiagMessage>) -> ErrorGuaranteed { pub fn early_err(&self, msg: impl Into<DiagMessage>) -> ErrorGuaranteed {
self.dcx.err(msg) self.dcx.handle().err(msg)
} }
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_fatal(&self, msg: impl Into<DiagMessage>) -> ! { pub fn early_fatal(&self, msg: impl Into<DiagMessage>) -> ! {
self.dcx.fatal(msg) self.dcx.handle().fatal(msg)
} }
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_struct_fatal(&self, msg: impl Into<DiagMessage>) -> Diag<'_, FatalAbort> { pub fn early_struct_fatal(&self, msg: impl Into<DiagMessage>) -> Diag<'_, FatalAbort> {
self.dcx.struct_fatal(msg) self.dcx.handle().struct_fatal(msg)
} }
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_warn(&self, msg: impl Into<DiagMessage>) { pub fn early_warn(&self, msg: impl Into<DiagMessage>) {
self.dcx.warn(msg) self.dcx.handle().warn(msg)
} }
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_struct_warn(&self, msg: impl Into<DiagMessage>) -> Diag<'_, ()> { pub fn early_struct_warn(&self, msg: impl Into<DiagMessage>) -> Diag<'_, ()> {
self.dcx.struct_warn(msg) self.dcx.handle().struct_warn(msg)
} }
} }

View File

@ -1,6 +1,6 @@
//! Errors emitted by symbol_mangling. //! Errors emitted by symbol_mangling.
use rustc_errors::{Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level}; use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
use rustc_span::Span; use rustc_span::Span;
use std::fmt; use std::fmt;
@ -14,7 +14,7 @@ pub struct TestOutput {
// 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> Diagnostic<'_, G> for TestOutput { impl<G: EmissionGuarantee> Diagnostic<'_, G> for TestOutput {
fn into_diag(self, dcx: &'_ DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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,6 +1,6 @@
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use rustc_errors::{ use rustc_errors::{
codes::*, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level, codes::*, Applicability, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level,
SubdiagMessageOp, Subdiagnostic, SubdiagMessageOp, Subdiagnostic,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
@ -59,7 +59,7 @@ pub struct NegativePositiveConflict<'tcx> {
impl<G: EmissionGuarantee> Diagnostic<'_, G> for NegativePositiveConflict<'_> { impl<G: EmissionGuarantee> Diagnostic<'_, G> for NegativePositiveConflict<'_> {
#[track_caller] #[track_caller]
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, 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

@ -8,6 +8,7 @@ use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::DiagCtxtHandle;
use rustc_session::config::{ use rustc_session::config::{
self, parse_crate_types_from_list, parse_externs, parse_target_triple, CrateType, self, parse_crate_types_from_list, parse_externs, parse_target_triple, CrateType,
}; };
@ -383,9 +384,10 @@ impl Options {
}; };
let dcx = new_dcx(error_format, None, diagnostic_width, &unstable_opts); let dcx = new_dcx(error_format, None, diagnostic_width, &unstable_opts);
let dcx = dcx.handle();
// check for deprecated options // check for deprecated options
check_deprecated_options(matches, &dcx); check_deprecated_options(matches, dcx);
if matches.opt_strs("passes") == ["list"] { if matches.opt_strs("passes") == ["list"] {
println!("Available passes for running rustdoc:"); println!("Available passes for running rustdoc:");
@ -458,7 +460,7 @@ impl Options {
println!("rustdoc: [check-theme] Starting tests! (Ignoring all other arguments)"); println!("rustdoc: [check-theme] Starting tests! (Ignoring all other arguments)");
for theme_file in to_check.iter() { for theme_file in to_check.iter() {
print!(" - Checking \"{theme_file}\"..."); print!(" - Checking \"{theme_file}\"...");
let (success, differences) = theme::test_theme_against(theme_file, &paths, &dcx); let (success, differences) = theme::test_theme_against(theme_file, &paths, dcx);
if !differences.is_empty() || !success { if !differences.is_empty() || !success {
println!(" FAILED"); println!(" FAILED");
errors += 1; errors += 1;
@ -603,7 +605,7 @@ impl Options {
.with_help("arguments to --theme must have a .css extension") .with_help("arguments to --theme must have a .css extension")
.emit(); .emit();
} }
let (success, ret) = theme::test_theme_against(&theme_file, &paths, &dcx); let (success, ret) = theme::test_theme_against(&theme_file, &paths, dcx);
if !success { if !success {
dcx.fatal(format!("error loading theme file: \"{theme_s}\"")); dcx.fatal(format!("error loading theme file: \"{theme_s}\""));
} else if !ret.is_empty() { } else if !ret.is_empty() {
@ -630,7 +632,7 @@ impl Options {
&matches.opt_strs("markdown-before-content"), &matches.opt_strs("markdown-before-content"),
&matches.opt_strs("markdown-after-content"), &matches.opt_strs("markdown-after-content"),
nightly_options::match_is_nightly_build(matches), nightly_options::match_is_nightly_build(matches),
&dcx, dcx,
&mut id_map, &mut id_map,
edition, edition,
&None, &None,
@ -741,9 +743,9 @@ impl Options {
); );
} }
let scrape_examples_options = ScrapeExamplesOptions::new(matches, &dcx); let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
let with_examples = matches.opt_strs("with-examples"); let with_examples = matches.opt_strs("with-examples");
let call_locations = crate::scrape_examples::load_call_locations(with_examples, &dcx); let call_locations = crate::scrape_examples::load_call_locations(with_examples, dcx);
let unstable_features = let unstable_features =
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref()); rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
@ -847,7 +849,7 @@ fn parse_remap_path_prefix(
} }
/// Prints deprecation warnings for deprecated options /// Prints deprecation warnings for deprecated options
fn check_deprecated_options(matches: &getopts::Matches, dcx: &rustc_errors::DiagCtxt) { fn check_deprecated_options(matches: &getopts::Matches, dcx: DiagCtxtHandle<'_>) {
let deprecated_flags = []; let deprecated_flags = [];
for &flag in deprecated_flags.iter() { for &flag in deprecated_flags.iter() {

View File

@ -3,7 +3,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_data_structures::unord::UnordSet; use rustc_data_structures::unord::UnordSet;
use rustc_errors::emitter::{stderr_destination, DynEmitter, HumanEmitter}; use rustc_errors::emitter::{stderr_destination, DynEmitter, HumanEmitter};
use rustc_errors::json::JsonEmitter; use rustc_errors::json::JsonEmitter;
use rustc_errors::{codes::*, ErrorGuaranteed, TerminalUrl}; use rustc_errors::{codes::*, DiagCtxtHandle, ErrorGuaranteed, TerminalUrl};
use rustc_feature::UnstableFeatures; use rustc_feature::UnstableFeatures;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId}; use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId};
@ -379,7 +379,7 @@ pub(crate) fn run_global_ctxt(
); );
} }
fn report_deprecated_attr(name: &str, dcx: &rustc_errors::DiagCtxt, sp: Span) { fn report_deprecated_attr(name: &str, dcx: DiagCtxtHandle<'_>, sp: Span) {
let mut msg = let mut msg =
dcx.struct_span_warn(sp, format!("the `#![doc({name})]` attribute is deprecated")); dcx.struct_span_warn(sp, format!("the `#![doc({name})]` attribute is deprecated"));
msg.note( msg.note(

View File

@ -7,7 +7,7 @@ pub(crate) use markdown::test as test_markdown;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{ColorConfig, ErrorGuaranteed, FatalError}; use rustc_errors::{ColorConfig, DiagCtxtHandle, ErrorGuaranteed, FatalError};
use rustc_hir::def_id::LOCAL_CRATE; use rustc_hir::def_id::LOCAL_CRATE;
use rustc_hir::CRATE_HIR_ID; use rustc_hir::CRATE_HIR_ID;
use rustc_interface::interface; use rustc_interface::interface;
@ -90,10 +90,7 @@ fn get_doctest_dir() -> io::Result<TempDir> {
TempFileBuilder::new().prefix("rustdoctest").tempdir() TempFileBuilder::new().prefix("rustdoctest").tempdir()
} }
pub(crate) fn run( pub(crate) fn run(dcx: DiagCtxtHandle<'_>, options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
dcx: &rustc_errors::DiagCtxt,
options: RustdocOptions,
) -> Result<(), ErrorGuaranteed> {
let invalid_codeblock_attributes_name = crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name; let invalid_codeblock_attributes_name = crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name;
// See core::create_config for what's going on here. // See core::create_config for what's going on here.

View File

@ -229,7 +229,7 @@ fn check_for_main_and_extern_crate(
// dcx. Any errors in the tests will be reported when the test file is compiled, // dcx. Any errors in the tests will be reported when the test file is compiled,
// Note that we still need to cancel the errors above otherwise `Diag` will panic on // Note that we still need to cancel the errors above otherwise `Diag` will panic on
// drop. // drop.
psess.dcx.reset_err_count(); psess.dcx().reset_err_count();
(found_main, found_extern_crate, found_macro) (found_main, found_extern_crate, found_macro)
}) })

View File

@ -1,4 +1,5 @@
use crate::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, Playground}; use crate::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, Playground};
use rustc_errors::DiagCtxtHandle;
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
@ -27,7 +28,7 @@ impl ExternalHtml {
md_before_content: &[String], md_before_content: &[String],
md_after_content: &[String], md_after_content: &[String],
nightly_build: bool, nightly_build: bool,
dcx: &rustc_errors::DiagCtxt, dcx: DiagCtxtHandle<'_>,
id_map: &mut IdMap, id_map: &mut IdMap,
edition: Edition, edition: Edition,
playground: &Option<Playground>, playground: &Option<Playground>,
@ -75,7 +76,7 @@ pub(crate) enum LoadStringError {
pub(crate) fn load_string<P: AsRef<Path>>( pub(crate) fn load_string<P: AsRef<Path>>(
file_path: P, file_path: P,
dcx: &rustc_errors::DiagCtxt, dcx: DiagCtxtHandle<'_>,
) -> Result<String, LoadStringError> { ) -> Result<String, LoadStringError> {
let file_path = file_path.as_ref(); let file_path = file_path.as_ref();
let contents = match fs::read(file_path) { let contents = match fs::read(file_path) {
@ -98,7 +99,7 @@ pub(crate) fn load_string<P: AsRef<Path>>(
} }
} }
fn load_external_files(names: &[String], dcx: &rustc_errors::DiagCtxt) -> Option<String> { fn load_external_files(names: &[String], dcx: DiagCtxtHandle<'_>) -> Option<String> {
let mut out = String::new(); let mut out = String::new();
for name in names { for name in names {
let Ok(s) = load_string(name, dcx) else { return None }; let Ok(s) = load_string(name, dcx) else { return None };

View File

@ -77,7 +77,7 @@ use std::io::{self, IsTerminal};
use std::process; use std::process;
use std::sync::{atomic::AtomicBool, Arc}; use std::sync::{atomic::AtomicBool, Arc};
use rustc_errors::{ErrorGuaranteed, FatalError}; use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, FatalError};
use rustc_interface::interface; use rustc_interface::interface;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup}; use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
@ -670,7 +670,7 @@ fn usage(argv0: &str) {
/// A result type used by several functions under `main()`. /// A result type used by several functions under `main()`.
type MainResult = Result<(), ErrorGuaranteed>; type MainResult = Result<(), ErrorGuaranteed>;
pub(crate) fn wrap_return(dcx: &rustc_errors::DiagCtxt, res: Result<(), String>) -> MainResult { pub(crate) fn wrap_return(dcx: DiagCtxtHandle<'_>, res: Result<(), String>) -> MainResult {
match res { match res {
Ok(()) => dcx.has_errors().map_or(Ok(()), Err), Ok(()) => dcx.has_errors().map_or(Ok(()), Err),
Err(err) => Err(dcx.err(err)), Err(err) => Err(dcx.err(err)),
@ -732,12 +732,13 @@ fn main_args(
None => return Ok(()), None => return Ok(()),
}; };
let diag = let dcx =
core::new_dcx(options.error_format, None, options.diagnostic_width, &options.unstable_opts); core::new_dcx(options.error_format, None, options.diagnostic_width, &options.unstable_opts);
let dcx = dcx.handle();
match (options.should_test, options.markdown_input()) { match (options.should_test, options.markdown_input()) {
(true, Some(_)) => return wrap_return(&diag, doctest::test_markdown(options)), (true, Some(_)) => return wrap_return(dcx, doctest::test_markdown(options)),
(true, None) => return doctest::run(&diag, options), (true, None) => return doctest::run(dcx, options),
(false, Some(input)) => { (false, Some(input)) => {
let input = input.to_owned(); let input = input.to_owned();
let edition = options.edition; let edition = options.edition;
@ -747,7 +748,7 @@ fn main_args(
// requires session globals and a thread pool, so we use // requires session globals and a thread pool, so we use
// `run_compiler`. // `run_compiler`.
return wrap_return( return wrap_return(
&diag, dcx,
interface::run_compiler(config, |_compiler| { interface::run_compiler(config, |_compiler| {
markdown::render(&input, render_options, edition) markdown::render(&input, render_options, edition)
}), }),

View File

@ -7,6 +7,7 @@ use crate::formats::renderer::FormatRenderer;
use crate::html::render::Context; use crate::html::render::Context;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::DiagCtxtHandle;
use rustc_hir::{ use rustc_hir::{
self as hir, self as hir,
intravisit::{self, Visitor}, intravisit::{self, Visitor},
@ -38,7 +39,7 @@ pub(crate) struct ScrapeExamplesOptions {
} }
impl ScrapeExamplesOptions { impl ScrapeExamplesOptions {
pub(crate) fn new(matches: &getopts::Matches, dcx: &rustc_errors::DiagCtxt) -> Option<Self> { pub(crate) fn new(matches: &getopts::Matches, dcx: DiagCtxtHandle<'_>) -> Option<Self> {
let output_path = matches.opt_str("scrape-examples-output-path"); let output_path = matches.opt_str("scrape-examples-output-path");
let target_crates = matches.opt_strs("scrape-examples-target-crate"); let target_crates = matches.opt_strs("scrape-examples-target-crate");
let scrape_tests = matches.opt_present("scrape-tests"); let scrape_tests = matches.opt_present("scrape-tests");
@ -336,7 +337,7 @@ pub(crate) fn run(
// options. // options.
pub(crate) fn load_call_locations( pub(crate) fn load_call_locations(
with_examples: Vec<String>, with_examples: Vec<String>,
dcx: &rustc_errors::DiagCtxt, dcx: DiagCtxtHandle<'_>,
) -> AllCallLocations { ) -> AllCallLocations {
let mut all_calls: AllCallLocations = FxHashMap::default(); let mut all_calls: AllCallLocations = FxHashMap::default();
for path in with_examples { for path in with_examples {

View File

@ -5,7 +5,7 @@ use std::iter::Peekable;
use std::path::Path; use std::path::Path;
use std::str::Chars; use std::str::Chars;
use rustc_errors::DiagCtxt; use rustc_errors::DiagCtxtHandle;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -236,7 +236,7 @@ pub(crate) fn get_differences(
pub(crate) fn test_theme_against<P: AsRef<Path>>( pub(crate) fn test_theme_against<P: AsRef<Path>>(
f: &P, f: &P,
origin: &FxHashMap<String, CssPath>, origin: &FxHashMap<String, CssPath>,
dcx: &DiagCtxt, dcx: DiagCtxtHandle<'_>,
) -> (bool, Vec<String>) { ) -> (bool, Vec<String>) {
let against = match fs::read_to_string(f) let against = match fs::read_to_string(f)
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())

View File

@ -180,12 +180,12 @@ pub fn main() {
rustc_driver::init_rustc_env_logger(&early_dcx); rustc_driver::init_rustc_env_logger(&early_dcx);
let using_internal_features = rustc_driver::install_ice_hook(BUG_REPORT_URL, |handler| { let using_internal_features = rustc_driver::install_ice_hook(BUG_REPORT_URL, |dcx| {
// FIXME: this macro calls unwrap internally but is called in a panicking context! It's not // FIXME: this macro calls unwrap internally but is called in a panicking context! It's not
// as simple as moving the call from the hook to main, because `install_ice_hook` doesn't // as simple as moving the call from the hook to main, because `install_ice_hook` doesn't
// accept a generic closure. // accept a generic closure.
let version_info = rustc_tools_util::get_version_info!(); let version_info = rustc_tools_util::get_version_info!();
handler.note(format!("Clippy version: {version_info}")); dcx.handle().note(format!("Clippy version: {version_info}"));
}); });
exit(rustc_driver::catch_with_exit_code(move || { exit(rustc_driver::catch_with_exit_code(move || {

View File

@ -67,7 +67,7 @@ fn parse_cfg_if_inner<'a>(
Ok(None) => continue, Ok(None) => continue,
Err(err) => { Err(err) => {
err.cancel(); err.cancel();
parser.psess.dcx.reset_err_count(); parser.psess.dcx().reset_err_count();
return Err( return Err(
"Expected item inside cfg_if block, but failed to parse it as an item", "Expected item inside cfg_if block, but failed to parse it as an item",
); );

View File

@ -16,8 +16,8 @@ pub(crate) fn parse_lazy_static(
($method:ident $(,)* $($arg:expr),* $(,)*) => { ($method:ident $(,)* $($arg:expr),* $(,)*) => {
match parser.$method($($arg,)*) { match parser.$method($($arg,)*) {
Ok(val) => { Ok(val) => {
if parser.psess.dcx.has_errors().is_some() { if parser.psess.dcx().has_errors().is_some() {
parser.psess.dcx.reset_err_count(); parser.psess.dcx().reset_err_count();
return None; return None;
} else { } else {
val val
@ -25,7 +25,7 @@ pub(crate) fn parse_lazy_static(
} }
Err(err) => { Err(err) => {
err.cancel(); err.cancel();
parser.psess.dcx.reset_err_count(); parser.psess.dcx().reset_err_count();
return None; return None;
} }
} }

View File

@ -29,8 +29,8 @@ fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
if Parser::nonterminal_may_begin_with($nt_kind, &cloned_parser.token) { if Parser::nonterminal_may_begin_with($nt_kind, &cloned_parser.token) {
match $try_parse(&mut cloned_parser) { match $try_parse(&mut cloned_parser) {
Ok(x) => { Ok(x) => {
if parser.psess.dcx.has_errors().is_some() { if parser.psess.dcx().has_errors().is_some() {
parser.psess.dcx.reset_err_count(); parser.psess.dcx().reset_err_count();
} else { } else {
// Parsing succeeded. // Parsing succeeded.
*parser = cloned_parser; *parser = cloned_parser;
@ -39,7 +39,7 @@ fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
} }
Err(e) => { Err(e) => {
e.cancel(); e.cancel();
parser.psess.dcx.reset_err_count(); parser.psess.dcx().reset_err_count();
} }
} }
} }

View File

@ -210,7 +210,9 @@ impl ParseSess {
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false, false,
); );
self.raw_psess.dcx.make_silent(fallback_bundle, None, false); self.raw_psess
.dcx()
.make_silent(fallback_bundle, None, false);
} }
pub(crate) fn span_to_filename(&self, span: Span) -> FileName { pub(crate) fn span_to_filename(&self, span: Span) -> FileName {
@ -286,11 +288,11 @@ impl ParseSess {
} }
pub(super) fn has_errors(&self) -> bool { pub(super) fn has_errors(&self) -> bool {
self.raw_psess.dcx.has_errors().is_some() self.raw_psess.dcx().has_errors().is_some()
} }
pub(super) fn reset_errors(&self) { pub(super) fn reset_errors(&self) {
self.raw_psess.dcx.reset_err_count(); self.raw_psess.dcx().reset_err_count();
} }
} }

View File

@ -14,8 +14,8 @@ extern crate rustc_session;
extern crate rustc_span; extern crate rustc_span;
use rustc_errors::{ use rustc_errors::{
Diag, DiagCtxt, DiagInner, DiagMessage, Diagnostic, EmissionGuarantee, Level, LintDiagnostic, Diag, DiagCtxtHandle, DiagInner, DiagMessage, Diagnostic, EmissionGuarantee, Level,
SubdiagMessageOp, SubdiagMessage, Subdiagnostic, LintDiagnostic, SubdiagMessage, SubdiagMessageOp, Subdiagnostic,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span; use rustc_span::Span;
@ -39,7 +39,7 @@ struct Note {
pub struct UntranslatableInDiagnostic; pub struct UntranslatableInDiagnostic;
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UntranslatableInDiagnostic { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UntranslatableInDiagnostic {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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
} }
@ -48,7 +48,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UntranslatableInDiagnostic
pub struct TranslatableInDiagnostic; pub struct TranslatableInDiagnostic;
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for TranslatableInDiagnostic { impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for TranslatableInDiagnostic {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, 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)
} }
} }
@ -81,7 +81,7 @@ impl Subdiagnostic for TranslatableInAddtoDiag {
pub struct UntranslatableInLintDiagnostic; pub struct UntranslatableInLintDiagnostic;
impl<'a> LintDiagnostic<'a, ()> for UntranslatableInLintDiagnostic { impl<'a> LintDiagnostic<'a, ()> for UntranslatableInLintDiagnostic {
fn decorate_lint<'b, >(self, diag: &'b mut Diag<'a, ()>) { fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
diag.note("untranslatable diagnostic"); diag.note("untranslatable diagnostic");
//~^ ERROR diagnostics should be created using translatable messages //~^ ERROR diagnostics should be created using translatable messages
} }
@ -95,7 +95,7 @@ impl<'a> LintDiagnostic<'a, ()> for TranslatableInLintDiagnostic {
} }
} }
pub fn make_diagnostics<'a>(dcx: &'a DiagCtxt) { pub fn make_diagnostics<'a>(dcx: DiagCtxtHandle<'a>) {
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 `Diagnostic`/`Subdiagnostic`/`LintDiagnostic` impls //~^ ERROR diagnostics should only be created in `Diagnostic`/`Subdiagnostic`/`LintDiagnostic` impls
@ -107,7 +107,7 @@ pub fn make_diagnostics<'a>(dcx: &'a DiagCtxt) {
// Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted for // Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted for
// `diagnostic_outside_of_impl`. // `diagnostic_outside_of_impl`.
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
pub fn skipped_because_of_annotation<'a>(dcx: &'a DiagCtxt) { pub fn skipped_because_of_annotation<'a>(dcx: DiagCtxtHandle<'a>) {
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
let _diag = dcx.struct_err("untranslatable diagnostic"); // okay! let _diag = dcx.struct_err("untranslatable diagnostic"); // okay!
} }