about summary refs log tree commit diff
path: root/compiler/rustc_passes/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_passes/src')
-rw-r--r--compiler/rustc_passes/src/abi_test.rs16
-rw-r--r--compiler/rustc_passes/src/check_attr.rs168
-rw-r--r--compiler/rustc_passes/src/check_const.rs2
-rw-r--r--compiler/rustc_passes/src/debugger_visualizer.rs10
-rw-r--r--compiler/rustc_passes/src/diagnostic_items.rs2
-rw-r--r--compiler/rustc_passes/src/entry.rs18
-rw-r--r--compiler/rustc_passes/src/hir_id_validator.rs2
-rw-r--r--compiler/rustc_passes/src/lang_items.rs8
-rw-r--r--compiler/rustc_passes/src/layout_test.rs16
-rw-r--r--compiler/rustc_passes/src/lib_features.rs11
-rw-r--r--compiler/rustc_passes/src/liveness.rs2
-rw-r--r--compiler/rustc_passes/src/loops.rs18
-rw-r--r--compiler/rustc_passes/src/naked_functions.rs14
-rw-r--r--compiler/rustc_passes/src/stability.rs28
-rw-r--r--compiler/rustc_passes/src/weak_lang_items.rs6
15 files changed, 168 insertions, 153 deletions
diff --git a/compiler/rustc_passes/src/abi_test.rs b/compiler/rustc_passes/src/abi_test.rs
index 153c39977bb..986ef69ad88 100644
--- a/compiler/rustc_passes/src/abi_test.rs
+++ b/compiler/rustc_passes/src/abi_test.rs
@@ -25,7 +25,7 @@ pub fn test_abi(tcx: TyCtxt<'_>) {
                     dump_abi_of_fn_type(tcx, id, attr);
                 }
                 _ => {
-                    tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
+                    tcx.dcx().emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
                 }
             }
         }
@@ -40,7 +40,7 @@ fn unwrap_fn_abi<'tcx>(
     match abi {
         Ok(abi) => abi,
         Err(FnAbiError::Layout(layout_error)) => {
-            tcx.sess.emit_fatal(Spanned {
+            tcx.dcx().emit_fatal(Spanned {
                 node: layout_error.into_diagnostic(),
                 span: tcx.def_span(item_def_id),
             });
@@ -65,7 +65,7 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
         Ok(None) => {
             // Not sure what to do here, but `LayoutError::Unknown` seems reasonable?
             let ty = tcx.type_of(item_def_id).instantiate_identity();
-            tcx.sess.emit_fatal(Spanned {
+            tcx.dcx().emit_fatal(Spanned {
                 node: LayoutError::Unknown(ty).into_diagnostic(),
 
                 span: tcx.def_span(item_def_id),
@@ -86,7 +86,7 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
         match meta_item.name_or_empty() {
             sym::debug => {
                 let fn_name = tcx.item_name(item_def_id.into());
-                tcx.sess.emit_err(AbiOf {
+                tcx.dcx().emit_err(AbiOf {
                     span: tcx.def_span(item_def_id),
                     fn_name,
                     // FIXME: using the `Debug` impl here isn't ideal.
@@ -95,7 +95,7 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
             }
 
             name => {
-                tcx.sess.emit_err(UnrecognizedField { span: meta_item.span(), name });
+                tcx.dcx().emit_err(UnrecognizedField { span: meta_item.span(), name });
             }
         }
     }
@@ -139,7 +139,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
                 );
 
                 let fn_name = tcx.item_name(item_def_id.into());
-                tcx.sess.emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) });
+                tcx.dcx().emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) });
             }
             sym::assert_eq => {
                 let ty::Tuple(fields) = ty.kind() else {
@@ -182,7 +182,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
                 );
 
                 if !test_abi_eq(abi1, abi2) {
-                    tcx.sess.emit_err(AbiNe {
+                    tcx.dcx().emit_err(AbiNe {
                         span,
                         left: format!("{:#?}", abi1),
                         right: format!("{:#?}", abi2),
@@ -190,7 +190,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
                 }
             }
             name => {
-                tcx.sess.emit_err(UnrecognizedField { span: meta_item.span(), name });
+                tcx.dcx().emit_err(UnrecognizedField { span: meta_item.span(), name });
             }
         }
     }
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index c5073048be3..cea4b113bf6 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -7,7 +7,7 @@
 use crate::{errors, fluent_generated as fluent};
 use rustc_ast::{ast, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem};
 use rustc_data_structures::fx::FxHashMap;
-use rustc_errors::{Applicability, IntoDiagnosticArg, MultiSpan};
+use rustc_errors::{Applicability, DiagCtxt, IntoDiagnosticArg, MultiSpan};
 use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
 use rustc_hir as hir;
 use rustc_hir::def_id::LocalModDefId;
@@ -95,7 +95,11 @@ struct CheckAttrVisitor<'tcx> {
     abort: Cell<bool>,
 }
 
-impl CheckAttrVisitor<'_> {
+impl<'tcx> CheckAttrVisitor<'tcx> {
+    pub fn dcx(&self) -> &'tcx DiagCtxt {
+        self.tcx.dcx()
+    }
+
     /// Checks any attribute.
     fn check_attributes(
         &self,
@@ -292,7 +296,7 @@ impl CheckAttrVisitor<'_> {
         if let Target::Impl = target {
             true
         } else {
-            self.tcx.sess.emit_err(errors::IncorrectDoNotRecommendLocation { span: attr_span });
+            self.dcx().emit_err(errors::IncorrectDoNotRecommendLocation { span: attr_span });
             false
         }
     }
@@ -343,7 +347,7 @@ impl CheckAttrVisitor<'_> {
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::InlineNotFnOrClosure {
+                self.dcx().emit_err(errors::InlineNotFnOrClosure {
                     attr_span: attr.span,
                     defn_span: span,
                 });
@@ -392,7 +396,7 @@ impl CheckAttrVisitor<'_> {
             }
 
             _ => {
-                self.tcx.sess.emit_err(errors::IgnoredCoverageNotCoverable {
+                self.dcx().emit_err(errors::IgnoredCoverageNotCoverable {
                     attr_span: attr.span,
                     defn_span: span,
                 });
@@ -435,7 +439,7 @@ impl CheckAttrVisitor<'_> {
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
+                self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
                     attr_span: attr.span,
                     defn_span: span,
                     on_crate: hir_id == CRATE_HIR_ID,
@@ -457,7 +461,7 @@ impl CheckAttrVisitor<'_> {
             Target::Fn
             | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
             _ => {
-                self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
+                self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
                     attr_span: attr.span,
                     defn_span: span,
                     on_crate: hir_id == CRATE_HIR_ID,
@@ -482,7 +486,7 @@ impl CheckAttrVisitor<'_> {
                     ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
                     ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
                 };
-                tcx.sess.emit_err(errors::ObjectLifetimeErr { span: p.span, repr });
+                tcx.dcx().emit_err(errors::ObjectLifetimeErr { span: p.span, repr });
             }
         }
     }
@@ -493,7 +497,7 @@ impl CheckAttrVisitor<'_> {
             Target::MacroDef => true,
             _ => {
                 self.tcx
-                    .sess
+                    .dcx()
                     .emit_err(errors::CollapseDebuginfo { attr_span: attr.span, defn_span: span });
                 false
             }
@@ -511,7 +515,7 @@ impl CheckAttrVisitor<'_> {
     ) -> bool {
         match target {
             _ if attrs.iter().any(|attr| attr.has_name(sym::naked)) => {
-                self.tcx.sess.emit_err(errors::NakedTrackedCaller { attr_span });
+                self.dcx().emit_err(errors::NakedTrackedCaller { attr_span });
                 false
             }
             Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => true,
@@ -526,7 +530,7 @@ impl CheckAttrVisitor<'_> {
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::TrackedCallerWrongLocation {
+                self.dcx().emit_err(errors::TrackedCallerWrongLocation {
                     attr_span,
                     defn_span: span,
                     on_crate: hir_id == CRATE_HIR_ID,
@@ -555,7 +559,7 @@ impl CheckAttrVisitor<'_> {
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::NonExhaustiveWrongLocation {
+                self.dcx().emit_err(errors::NonExhaustiveWrongLocation {
                     attr_span: attr.span,
                     defn_span: span,
                 });
@@ -577,7 +581,7 @@ impl CheckAttrVisitor<'_> {
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToTrait {
+                self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait {
                     attr_span: attr.span,
                     defn_span: span,
                 });
@@ -612,7 +616,7 @@ impl CheckAttrVisitor<'_> {
                         unreachable!();
                     };
 
-                    self.tcx.sess.emit_err(errors::LangItemWithTargetFeature {
+                    self.dcx().emit_err(errors::LangItemWithTargetFeature {
                         attr_span: attr.span,
                         name: lang_item,
                         sig_span: sig.span,
@@ -643,7 +647,7 @@ impl CheckAttrVisitor<'_> {
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
+                self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
                     attr_span: attr.span,
                     defn_span: span,
                     on_crate: hir_id == CRATE_HIR_ID,
@@ -658,7 +662,7 @@ impl CheckAttrVisitor<'_> {
         match target {
             Target::ForeignStatic | Target::Static => true,
             _ => {
-                self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToStatic {
+                self.dcx().emit_err(errors::AttrShouldBeAppliedToStatic {
                     attr_span: attr.span,
                     defn_span: span,
                 });
@@ -668,7 +672,7 @@ impl CheckAttrVisitor<'_> {
     }
 
     fn doc_attr_str_error(&self, meta: &NestedMetaItem, attr_name: &str) {
-        self.tcx.sess.emit_err(errors::DocExpectStr { attr_span: meta.span(), attr_name });
+        self.dcx().emit_err(errors::DocExpectStr { attr_span: meta.span(), attr_name });
     }
 
     fn check_doc_alias_value(
@@ -685,7 +689,7 @@ impl CheckAttrVisitor<'_> {
         let attr_str =
             &format!("`#[doc(alias{})]`", if is_list { "(\"...\")" } else { " = \"...\"" });
         if doc_alias == kw::Empty {
-            tcx.sess.emit_err(errors::DocAliasEmpty { span, attr_str });
+            tcx.dcx().emit_err(errors::DocAliasEmpty { span, attr_str });
             return false;
         }
 
@@ -694,11 +698,11 @@ impl CheckAttrVisitor<'_> {
             .chars()
             .find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' '))
         {
-            tcx.sess.emit_err(errors::DocAliasBadChar { span, attr_str, char_: c });
+            tcx.dcx().emit_err(errors::DocAliasBadChar { span, attr_str, char_: c });
             return false;
         }
         if doc_alias_str.starts_with(' ') || doc_alias_str.ends_with(' ') {
-            tcx.sess.emit_err(errors::DocAliasStartEnd { span, attr_str });
+            tcx.dcx().emit_err(errors::DocAliasStartEnd { span, attr_str });
             return false;
         }
 
@@ -756,12 +760,12 @@ impl CheckAttrVisitor<'_> {
             | Target::PatField
             | Target::ExprField => None,
         } {
-            tcx.sess.emit_err(errors::DocAliasBadLocation { span, attr_str, location });
+            tcx.dcx().emit_err(errors::DocAliasBadLocation { span, attr_str, location });
             return false;
         }
         let item_name = self.tcx.hir().name(hir_id);
         if item_name == doc_alias {
-            tcx.sess.emit_err(errors::DocAliasNotAnAlias { span, attr_str });
+            tcx.dcx().emit_err(errors::DocAliasNotAnAlias { span, attr_str });
             return false;
         }
         if let Err(entry) = aliases.try_insert(doc_alias_str.to_owned(), span) {
@@ -794,13 +798,15 @@ impl CheckAttrVisitor<'_> {
                         }
                         _ => {
                             self.tcx
-                                .sess
+                                .dcx()
                                 .emit_err(errors::DocAliasNotStringLiteral { span: v.span() });
                             errors += 1;
                         }
                     },
                     None => {
-                        self.tcx.sess.emit_err(errors::DocAliasNotStringLiteral { span: v.span() });
+                        self.tcx
+                            .dcx()
+                            .emit_err(errors::DocAliasNotStringLiteral { span: v.span() });
                         errors += 1;
                     }
                 }
@@ -809,7 +815,7 @@ impl CheckAttrVisitor<'_> {
         } else if let Some(doc_alias) = meta.value_str() {
             self.check_doc_alias_value(meta, doc_alias, hir_id, target, false, aliases)
         } else {
-            self.tcx.sess.emit_err(errors::DocAliasMalformed { span: meta.span() });
+            self.dcx().emit_err(errors::DocAliasMalformed { span: meta.span() });
             false
         }
     }
@@ -826,17 +832,17 @@ impl CheckAttrVisitor<'_> {
         }) {
             Some(ItemKind::Mod(module)) => {
                 if !module.item_ids.is_empty() {
-                    self.tcx.sess.emit_err(errors::DocKeywordEmptyMod { span: meta.span() });
+                    self.dcx().emit_err(errors::DocKeywordEmptyMod { span: meta.span() });
                     return false;
                 }
             }
             _ => {
-                self.tcx.sess.emit_err(errors::DocKeywordNotMod { span: meta.span() });
+                self.dcx().emit_err(errors::DocKeywordNotMod { span: meta.span() });
                 return false;
             }
         }
         if !rustc_lexer::is_ident(doc_keyword.as_str()) {
-            self.tcx.sess.emit_err(errors::DocKeywordInvalidIdent {
+            self.dcx().emit_err(errors::DocKeywordInvalidIdent {
                 span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
                 doc_keyword,
             });
@@ -858,12 +864,12 @@ impl CheckAttrVisitor<'_> {
                         false
                     };
                 if !is_valid {
-                    self.tcx.sess.emit_err(errors::DocFakeVariadicNotValid { span: meta.span() });
+                    self.dcx().emit_err(errors::DocFakeVariadicNotValid { span: meta.span() });
                     return false;
                 }
             }
             _ => {
-                self.tcx.sess.emit_err(errors::DocKeywordOnlyImpl { span: meta.span() });
+                self.dcx().emit_err(errors::DocKeywordOnlyImpl { span: meta.span() });
                 return false;
             }
         }
@@ -898,7 +904,7 @@ impl CheckAttrVisitor<'_> {
                             meta.span(),
                             fluent::passes_doc_inline_conflict_second,
                         );
-                        self.tcx.sess.emit_err(errors::DocKeywordConflict { spans });
+                        self.dcx().emit_err(errors::DocKeywordConflict { spans });
                         return false;
                     }
                     true
@@ -969,7 +975,7 @@ impl CheckAttrVisitor<'_> {
         attr_name: &str,
     ) -> bool {
         if CRATE_HIR_ID == hir_id {
-            self.tcx.sess.emit_err(errors::DocAttrNotCrateLevel { span: meta.span(), attr_name });
+            self.dcx().emit_err(errors::DocAttrNotCrateLevel { span: meta.span(), attr_name });
             return false;
         }
         true
@@ -1243,7 +1249,7 @@ impl CheckAttrVisitor<'_> {
         match target {
             Target::Struct | Target::Enum | Target::TyAlias => true,
             _ => {
-                self.tcx.sess.emit_err(errors::PassByValue { attr_span: attr.span, span });
+                self.dcx().emit_err(errors::PassByValue { attr_span: attr.span, span });
                 false
             }
         }
@@ -1253,7 +1259,7 @@ impl CheckAttrVisitor<'_> {
         match target {
             Target::Method(MethodKind::Inherent) => true,
             _ => {
-                self.tcx.sess.emit_err(errors::AllowIncoherentImpl { attr_span: attr.span, span });
+                self.dcx().emit_err(errors::AllowIncoherentImpl { attr_span: attr.span, span });
                 false
             }
         }
@@ -1271,7 +1277,7 @@ impl CheckAttrVisitor<'_> {
             }
             _ => {
                 self.tcx
-                    .sess
+                    .dcx()
                     .emit_err(errors::HasIncoherentInherentImpl { attr_span: attr.span, span });
                 false
             }
@@ -1280,12 +1286,12 @@ impl CheckAttrVisitor<'_> {
 
     fn check_ffi_pure(&self, attr_span: Span, attrs: &[Attribute], target: Target) -> bool {
         if target != Target::ForeignFn {
-            self.tcx.sess.emit_err(errors::FfiPureInvalidTarget { attr_span });
+            self.dcx().emit_err(errors::FfiPureInvalidTarget { attr_span });
             return false;
         }
         if attrs.iter().any(|a| a.has_name(sym::ffi_const)) {
             // `#[ffi_const]` functions cannot be `#[ffi_pure]`
-            self.tcx.sess.emit_err(errors::BothFfiConstAndPure { attr_span });
+            self.dcx().emit_err(errors::BothFfiConstAndPure { attr_span });
             false
         } else {
             true
@@ -1296,7 +1302,7 @@ impl CheckAttrVisitor<'_> {
         if target == Target::ForeignFn {
             true
         } else {
-            self.tcx.sess.emit_err(errors::FfiConstInvalidTarget { attr_span });
+            self.dcx().emit_err(errors::FfiConstInvalidTarget { attr_span });
             false
         }
     }
@@ -1305,7 +1311,7 @@ impl CheckAttrVisitor<'_> {
         if target == Target::ForeignFn {
             true
         } else {
-            self.tcx.sess.emit_err(errors::FfiReturnsTwiceInvalidTarget { attr_span });
+            self.dcx().emit_err(errors::FfiReturnsTwiceInvalidTarget { attr_span });
             false
         }
     }
@@ -1354,7 +1360,7 @@ impl CheckAttrVisitor<'_> {
         match target {
             Target::Struct | Target::Enum | Target::Union | Target::Trait => true,
             _ => {
-                self.tcx.sess.emit_err(errors::MustNotSuspend { attr_span: attr.span, span });
+                self.dcx().emit_err(errors::MustNotSuspend { attr_span: attr.span, span });
                 false
             }
         }
@@ -1449,7 +1455,7 @@ impl CheckAttrVisitor<'_> {
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::NoLink { attr_span: attr.span, span });
+                self.dcx().emit_err(errors::NoLink { attr_span: attr.span, span });
                 false
             }
         }
@@ -1479,7 +1485,7 @@ impl CheckAttrVisitor<'_> {
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::ExportName { attr_span: attr.span, span });
+                self.dcx().emit_err(errors::ExportName { attr_span: attr.span, span });
                 false
             }
         }
@@ -1492,7 +1498,7 @@ impl CheckAttrVisitor<'_> {
         target: Target,
     ) -> bool {
         if target != Target::Struct {
-            self.tcx.sess.emit_err(errors::RustcLayoutScalarValidRangeNotStruct {
+            self.dcx().emit_err(errors::RustcLayoutScalarValidRangeNotStruct {
                 attr_span: attr.span,
                 span,
             });
@@ -1506,7 +1512,9 @@ impl CheckAttrVisitor<'_> {
         if matches!(&list[..], &[NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Int(..), .. })]) {
             true
         } else {
-            self.tcx.sess.emit_err(errors::RustcLayoutScalarValidRangeArg { attr_span: attr.span });
+            self.tcx
+                .dcx()
+                .emit_err(errors::RustcLayoutScalarValidRangeArg { attr_span: attr.span });
             false
         }
     }
@@ -1522,7 +1530,7 @@ impl CheckAttrVisitor<'_> {
     ) -> bool {
         let is_function = matches!(target, Target::Fn);
         if !is_function {
-            self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
+            self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
                 attr_span: attr.span,
                 defn_span: span,
                 on_crate: hir_id == CRATE_HIR_ID,
@@ -1546,7 +1554,7 @@ impl CheckAttrVisitor<'_> {
             match param.kind {
                 hir::GenericParamKind::Const { .. } => {}
                 _ => {
-                    self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsOnly {
+                    self.dcx().emit_err(errors::RustcLegacyConstGenericsOnly {
                         attr_span: attr.span,
                         param_span: param.span,
                     });
@@ -1556,7 +1564,7 @@ impl CheckAttrVisitor<'_> {
         }
 
         if list.len() != generics.params.len() {
-            self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsIndex {
+            self.dcx().emit_err(errors::RustcLegacyConstGenericsIndex {
                 attr_span: attr.span,
                 generics_span: generics.span,
             });
@@ -1569,7 +1577,7 @@ impl CheckAttrVisitor<'_> {
             if let Some(LitKind::Int(val, _)) = meta.lit().map(|lit| &lit.kind) {
                 if *val >= arg_count {
                     let span = meta.span();
-                    self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsIndexExceed {
+                    self.dcx().emit_err(errors::RustcLegacyConstGenericsIndexExceed {
                         span,
                         arg_count: arg_count as usize,
                     });
@@ -1581,7 +1589,7 @@ impl CheckAttrVisitor<'_> {
         }
 
         if !invalid_args.is_empty() {
-            self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsIndexNegative { invalid_args });
+            self.dcx().emit_err(errors::RustcLegacyConstGenericsIndexNegative { invalid_args });
             false
         } else {
             true
@@ -1599,7 +1607,7 @@ impl CheckAttrVisitor<'_> {
     ) -> bool {
         let is_function = matches!(target, Target::Fn | Target::Method(..));
         if !is_function {
-            self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
+            self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
                 attr_span: attr.span,
                 defn_span: span,
                 on_crate: hir_id == CRATE_HIR_ID,
@@ -1639,7 +1647,7 @@ impl CheckAttrVisitor<'_> {
         match target {
             Target::Struct => true,
             _ => {
-                self.tcx.sess.emit_err(errors::RustcLintOptTy { attr_span: attr.span, span });
+                self.dcx().emit_err(errors::RustcLintOptTy { attr_span: attr.span, span });
                 false
             }
         }
@@ -1656,7 +1664,7 @@ impl CheckAttrVisitor<'_> {
             Target::Field => true,
             _ => {
                 self.tcx
-                    .sess
+                    .dcx()
                     .emit_err(errors::RustcLintOptDenyFieldAccess { attr_span: attr.span, span });
                 false
             }
@@ -1669,7 +1677,7 @@ impl CheckAttrVisitor<'_> {
         if self.tcx.sess.opts.unstable_opts.query_dep_graph {
             true
         } else {
-            self.tcx.sess.emit_err(errors::RustcDirtyClean { span: attr.span });
+            self.dcx().emit_err(errors::RustcDirtyClean { span: attr.span });
             false
         }
     }
@@ -1679,7 +1687,7 @@ impl CheckAttrVisitor<'_> {
         match target {
             Target::Trait => true,
             _ => {
-                self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToTrait {
+                self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait {
                     attr_span: attr.span,
                     defn_span: span,
                 });
@@ -1782,7 +1790,7 @@ impl CheckAttrVisitor<'_> {
 
         for hint in &hints {
             if !hint.is_meta_item() {
-                self.tcx.sess.emit_err(errors::ReprIdent { span: hint.span() });
+                self.dcx().emit_err(errors::ReprIdent { span: hint.span() });
                 continue;
             }
 
@@ -1795,7 +1803,7 @@ impl CheckAttrVisitor<'_> {
                     match target {
                         Target::Struct | Target::Union | Target::Enum => continue,
                         _ => {
-                            self.tcx.sess.emit_err(errors::AttrApplication::StructEnumUnion {
+                            self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
                                 hint_span: hint.span(),
                                 span,
                             });
@@ -1822,7 +1830,7 @@ impl CheckAttrVisitor<'_> {
                         | Target::Fn
                         | Target::Method(_) => continue,
                         _ => {
-                            self.tcx.sess.emit_err(
+                            self.dcx().emit_err(
                                 errors::AttrApplication::StructEnumFunctionMethodUnion {
                                     hint_span: hint.span(),
                                     span,
@@ -1833,7 +1841,7 @@ impl CheckAttrVisitor<'_> {
                 }
                 sym::packed => {
                     if target != Target::Struct && target != Target::Union {
-                        self.tcx.sess.emit_err(errors::AttrApplication::StructUnion {
+                        self.dcx().emit_err(errors::AttrApplication::StructUnion {
                             hint_span: hint.span(),
                             span,
                         });
@@ -1844,7 +1852,7 @@ impl CheckAttrVisitor<'_> {
                 sym::simd => {
                     is_simd = true;
                     if target != Target::Struct {
-                        self.tcx.sess.emit_err(errors::AttrApplication::Struct {
+                        self.dcx().emit_err(errors::AttrApplication::Struct {
                             hint_span: hint.span(),
                             span,
                         });
@@ -1857,7 +1865,7 @@ impl CheckAttrVisitor<'_> {
                     match target {
                         Target::Struct | Target::Union | Target::Enum => continue,
                         _ => {
-                            self.tcx.sess.emit_err(errors::AttrApplication::StructEnumUnion {
+                            self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
                                 hint_span: hint.span(),
                                 span,
                             });
@@ -1878,7 +1886,7 @@ impl CheckAttrVisitor<'_> {
                 | sym::usize => {
                     int_reprs += 1;
                     if target != Target::Enum {
-                        self.tcx.sess.emit_err(errors::AttrApplication::Enum {
+                        self.dcx().emit_err(errors::AttrApplication::Enum {
                             hint_span: hint.span(),
                             span,
                         });
@@ -1887,7 +1895,7 @@ impl CheckAttrVisitor<'_> {
                     }
                 }
                 _ => {
-                    self.tcx.sess.emit_err(errors::UnrecognizedReprHint { span: hint.span() });
+                    self.dcx().emit_err(errors::UnrecognizedReprHint { span: hint.span() });
                     continue;
                 }
             };
@@ -1900,14 +1908,14 @@ impl CheckAttrVisitor<'_> {
         // Error on repr(transparent, <anything else>).
         if is_transparent && hints.len() > 1 {
             let hint_spans = hint_spans.clone().collect();
-            self.tcx.sess.emit_err(errors::TransparentIncompatible {
+            self.dcx().emit_err(errors::TransparentIncompatible {
                 hint_spans,
                 target: target.to_string(),
             });
         }
         if is_explicit_rust && (int_reprs > 0 || is_c || is_simd) {
             let hint_spans = hint_spans.clone().collect();
-            self.tcx.sess.emit_err(errors::ReprConflicting { hint_spans });
+            self.dcx().emit_err(errors::ReprConflicting { hint_spans });
         }
         // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
         if (int_reprs > 1)
@@ -1935,7 +1943,7 @@ impl CheckAttrVisitor<'_> {
         let mut used_compiler_span = None;
         for attr in attrs.iter().filter(|attr| attr.has_name(sym::used)) {
             if target != Target::Static {
-                self.tcx.sess.emit_err(errors::UsedStatic { span: attr.span });
+                self.dcx().emit_err(errors::UsedStatic { span: attr.span });
             }
             let inner = attr.meta_item_list();
             match inner.as_deref() {
@@ -1962,7 +1970,7 @@ impl CheckAttrVisitor<'_> {
         }
         if let (Some(linker_span), Some(compiler_span)) = (used_linker_span, used_compiler_span) {
             self.tcx
-                .sess
+                .dcx()
                 .emit_err(errors::UsedCompilerLinker { spans: vec![linker_span, compiler_span] });
         }
     }
@@ -2004,7 +2012,7 @@ impl CheckAttrVisitor<'_> {
             }
             _ => {
                 self.tcx
-                    .sess
+                    .dcx()
                     .emit_err(errors::AllowInternalUnstable { attr_span: attr.span, span });
                 false
             }
@@ -2020,7 +2028,7 @@ impl CheckAttrVisitor<'_> {
         match target {
             Target::Mod => {}
             _ => {
-                self.tcx.sess.emit_err(errors::DebugVisualizerPlacement { span: attr.span });
+                self.dcx().emit_err(errors::DebugVisualizerPlacement { span: attr.span });
                 return false;
             }
         }
@@ -2053,7 +2061,7 @@ impl CheckAttrVisitor<'_> {
             }
             _ => {
                 self.tcx
-                    .sess
+                    .dcx()
                     .emit_err(errors::RustcAllowConstFnUnstable { attr_span: attr.span, span });
                 false
             }
@@ -2079,7 +2087,7 @@ impl CheckAttrVisitor<'_> {
             return true;
         }
 
-        self.tcx.sess.emit_err(errors::RustcSafeIntrinsic { attr_span: attr.span, span });
+        self.dcx().emit_err(errors::RustcSafeIntrinsic { attr_span: attr.span, span });
         false
     }
 
@@ -2093,7 +2101,7 @@ impl CheckAttrVisitor<'_> {
             Target::Fn | Target::Static => true,
             _ => {
                 self.tcx
-                    .sess
+                    .dcx()
                     .emit_err(errors::RustcStdInternalSymbol { attr_span: attr.span, span });
                 false
             }
@@ -2103,7 +2111,7 @@ impl CheckAttrVisitor<'_> {
     fn check_stability_promotable(&self, attr: &Attribute, _span: Span, target: Target) -> bool {
         match target {
             Target::Expression => {
-                self.tcx.sess.emit_err(errors::StabilityPromotable { attr_span: attr.span });
+                self.dcx().emit_err(errors::StabilityPromotable { attr_span: attr.span });
                 false
             }
             _ => true,
@@ -2114,7 +2122,7 @@ impl CheckAttrVisitor<'_> {
         match target {
             Target::ForeignFn | Target::ForeignStatic => true,
             _ => {
-                self.tcx.sess.emit_err(errors::LinkOrdinal { attr_span: attr.span });
+                self.dcx().emit_err(errors::LinkOrdinal { attr_span: attr.span });
                 false
             }
         }
@@ -2134,7 +2142,7 @@ impl CheckAttrVisitor<'_> {
 
                 for meta in metas {
                     let NestedMetaItem::Lit(meta_lit) = meta else {
-                        self.tcx.sess.emit_err(errors::IncorrectMetaItem {
+                        self.dcx().emit_err(errors::IncorrectMetaItem {
                             span: meta.span(),
                             suggestion: errors::IncorrectMetaItemSuggestion {
                                 lo: meta.span().shrink_to_lo(),
@@ -2147,14 +2155,14 @@ impl CheckAttrVisitor<'_> {
                 }
 
                 if candidates.is_empty() {
-                    self.tcx.sess.emit_err(errors::EmptyConfusables { span: attr.span });
+                    self.dcx().emit_err(errors::EmptyConfusables { span: attr.span });
                     return false;
                 }
 
                 true
             }
             _ => {
-                self.tcx.sess.emit_err(errors::Confusables { attr_span: attr.span });
+                self.dcx().emit_err(errors::Confusables { attr_span: attr.span });
                 false
             }
         }
@@ -2326,7 +2334,7 @@ impl CheckAttrVisitor<'_> {
         );
 
         if let Err(terr) = ocx.eq(&cause, param_env, expected_sig, sig) {
-            let mut diag = tcx.sess.create_err(errors::ProcMacroBadSig { span, kind });
+            let mut diag = tcx.dcx().create_err(errors::ProcMacroBadSig { span, kind });
 
             let hir_sig = tcx.hir().fn_sig_by_hir_id(hir_id);
             if let Some(hir_sig) = hir_sig {
@@ -2531,7 +2539,7 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
                             span: item.ident.span,
                             kind: item.kind.descr(),
                         });
-                    tcx.sess.emit_err(errors::InvalidAttrAtCrateLevel {
+                    tcx.dcx().emit_err(errors::InvalidAttrAtCrateLevel {
                         span: attr.span,
                         sugg_span: tcx
                             .sess
@@ -2558,7 +2566,7 @@ fn check_non_exported_macro_for_invalid_attrs(tcx: TyCtxt<'_>, item: &Item<'_>)
 
     for attr in attrs {
         if attr.has_name(sym::inline) {
-            tcx.sess.emit_err(errors::NonExportedMacroInvalidAttrs { attr_span: attr.span });
+            tcx.dcx().emit_err(errors::NonExportedMacroInvalidAttrs { attr_span: attr.span });
         }
     }
 }
@@ -2629,7 +2637,7 @@ fn check_duplicates(
                 } else {
                     (attr.span, *entry.get())
                 };
-                tcx.sess.emit_err(errors::UnusedMultiple {
+                tcx.dcx().emit_err(errors::UnusedMultiple {
                     this,
                     other,
                     name: attr.name_or_empty(),
diff --git a/compiler/rustc_passes/src/check_const.rs b/compiler/rustc_passes/src/check_const.rs
index 76c7467346d..3e5fb1a6b47 100644
--- a/compiler/rustc_passes/src/check_const.rs
+++ b/compiler/rustc_passes/src/check_const.rs
@@ -122,7 +122,7 @@ impl<'tcx> CheckConstVisitor<'tcx> {
             // corresponding feature gate. This encourages nightly users to use feature gates when
             // possible.
             None if tcx.sess.opts.unstable_opts.unleash_the_miri_inside_of_you => {
-                tcx.sess.emit_warning(SkippingConstChecks { span });
+                tcx.dcx().emit_warning(SkippingConstChecks { span });
                 return;
             }
 
diff --git a/compiler/rustc_passes/src/debugger_visualizer.rs b/compiler/rustc_passes/src/debugger_visualizer.rs
index 3483f7da528..f4cd2fbc91f 100644
--- a/compiler/rustc_passes/src/debugger_visualizer.rs
+++ b/compiler/rustc_passes/src/debugger_visualizer.rs
@@ -17,19 +17,19 @@ impl DebuggerVisualizerCollector<'_> {
     fn check_for_debugger_visualizer(&mut self, attr: &Attribute) {
         if attr.has_name(sym::debugger_visualizer) {
             let Some(hints) = attr.meta_item_list() else {
-                self.sess.emit_err(DebugVisualizerInvalid { span: attr.span });
+                self.sess.dcx().emit_err(DebugVisualizerInvalid { span: attr.span });
                 return;
             };
 
             let hint = if hints.len() == 1 {
                 &hints[0]
             } else {
-                self.sess.emit_err(DebugVisualizerInvalid { span: attr.span });
+                self.sess.dcx().emit_err(DebugVisualizerInvalid { span: attr.span });
                 return;
             };
 
             let Some(meta_item) = hint.meta_item() else {
-                self.sess.emit_err(DebugVisualizerInvalid { span: attr.span });
+                self.sess.dcx().emit_err(DebugVisualizerInvalid { span: attr.span });
                 return;
             };
 
@@ -40,7 +40,7 @@ impl DebuggerVisualizerCollector<'_> {
                         (DebuggerVisualizerType::GdbPrettyPrinter, value)
                     }
                     (_, _) => {
-                        self.sess.emit_err(DebugVisualizerInvalid { span: meta_item.span });
+                        self.sess.dcx().emit_err(DebugVisualizerInvalid { span: meta_item.span });
                         return;
                     }
                 };
@@ -63,7 +63,7 @@ impl DebuggerVisualizerCollector<'_> {
                     ));
                 }
                 Err(error) => {
-                    self.sess.emit_err(DebugVisualizerUnreadable {
+                    self.sess.dcx().emit_err(DebugVisualizerUnreadable {
                         span: meta_item.span,
                         file: &file,
                         error,
diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs
index d8b9f4fae87..906ecdfe5ab 100644
--- a/compiler/rustc_passes/src/diagnostic_items.rs
+++ b/compiler/rustc_passes/src/diagnostic_items.rs
@@ -45,7 +45,7 @@ fn report_duplicate_item(
 ) {
     let orig_span = tcx.hir().span_if_local(original_def_id);
     let duplicate_span = tcx.hir().span_if_local(item_def_id);
-    tcx.sess.emit_err(DuplicateDiagnosticItemInCrate {
+    tcx.dcx().emit_err(DuplicateDiagnosticItemInCrate {
         duplicate_span,
         orig_span,
         crate_name: tcx.crate_name(item_def_id.krate),
diff --git a/compiler/rustc_passes/src/entry.rs b/compiler/rustc_passes/src/entry.rs
index 7667fc21eee..ed26b45a936 100644
--- a/compiler/rustc_passes/src/entry.rs
+++ b/compiler/rustc_passes/src/entry.rs
@@ -69,20 +69,20 @@ fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
     match entry_point_type {
         EntryPointType::None => {
             if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) {
-                ctxt.tcx.sess.emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe });
+                ctxt.tcx.dcx().emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe });
             }
         }
         _ if !matches!(ctxt.tcx.def_kind(id.owner_id), DefKind::Fn) => {
             for attr in [sym::start, sym::rustc_main] {
                 if let Some(span) = attr_span_by_symbol(ctxt, id, attr) {
-                    ctxt.tcx.sess.emit_err(AttrOnlyInFunctions { span, attr });
+                    ctxt.tcx.dcx().emit_err(AttrOnlyInFunctions { span, attr });
                 }
             }
         }
         EntryPointType::MainNamed => (),
         EntryPointType::OtherMain => {
             if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) {
-                ctxt.tcx.sess.emit_err(AttrOnlyOnRootMain { span, attr: sym::unix_sigpipe });
+                ctxt.tcx.dcx().emit_err(AttrOnlyOnRootMain { span, attr: sym::unix_sigpipe });
             }
             ctxt.non_main_fns.push(ctxt.tcx.def_span(id.owner_id));
         }
@@ -90,7 +90,7 @@ fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
             if ctxt.attr_main_fn.is_none() {
                 ctxt.attr_main_fn = Some((id.owner_id.def_id, ctxt.tcx.def_span(id.owner_id)));
             } else {
-                ctxt.tcx.sess.emit_err(MultipleRustcMain {
+                ctxt.tcx.dcx().emit_err(MultipleRustcMain {
                     span: ctxt.tcx.def_span(id.owner_id.to_def_id()),
                     first: ctxt.attr_main_fn.unwrap().1,
                     additional: ctxt.tcx.def_span(id.owner_id.to_def_id()),
@@ -99,12 +99,12 @@ fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
         }
         EntryPointType::Start => {
             if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) {
-                ctxt.tcx.sess.emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe });
+                ctxt.tcx.dcx().emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe });
             }
             if ctxt.start_fn.is_none() {
                 ctxt.start_fn = Some((id.owner_id.def_id, ctxt.tcx.def_span(id.owner_id)));
             } else {
-                ctxt.tcx.sess.emit_err(MultipleStartFunctions {
+                ctxt.tcx.dcx().emit_err(MultipleStartFunctions {
                     span: ctxt.tcx.def_span(id.owner_id),
                     labeled: ctxt.tcx.def_span(id.owner_id.to_def_id()),
                     previous: ctxt.start_fn.unwrap().1,
@@ -128,7 +128,7 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) -> Option<(DefId,
             if let Some(def_id) = def_id.as_local()
                 && matches!(tcx.opt_hir_node_by_def_id(def_id), Some(Node::ForeignItem(_)))
             {
-                tcx.sess.emit_err(ExternMain { span: tcx.def_span(def_id) });
+                tcx.dcx().emit_err(ExternMain { span: tcx.def_span(def_id) });
                 return None;
             }
 
@@ -161,7 +161,7 @@ fn sigpipe(tcx: TyCtxt<'_>, def_id: DefId) -> u8 {
                 sigpipe::DEFAULT
             }
             _ => {
-                tcx.sess.emit_err(UnixSigpipeValues { span: attr.span });
+                tcx.dcx().emit_err(UnixSigpipeValues { span: attr.span });
                 sigpipe::DEFAULT
             }
         }
@@ -187,7 +187,7 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) {
     // note instead.
     let file_empty = tcx.sess.source_map().lookup_line(sp.hi()).is_err();
 
-    tcx.sess.emit_err(NoMainErr {
+    tcx.dcx().emit_err(NoMainErr {
         sp,
         crate_name: tcx.crate_name(LOCAL_CRATE),
         has_filename,
diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs
index 18a80aa34f8..0727bad7c5a 100644
--- a/compiler/rustc_passes/src/hir_id_validator.rs
+++ b/compiler/rustc_passes/src/hir_id_validator.rs
@@ -31,7 +31,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
 
         if !errors.is_empty() {
             let message = errors.iter().fold(String::new(), |s1, s2| s1 + "\n" + s2);
-            tcx.sess.span_delayed_bug(rustc_span::DUMMY_SP, message);
+            tcx.dcx().span_delayed_bug(rustc_span::DUMMY_SP, message);
         }
     }
 }
diff --git a/compiler/rustc_passes/src/lang_items.rs b/compiler/rustc_passes/src/lang_items.rs
index 6d14a14096d..647f4d0e084 100644
--- a/compiler/rustc_passes/src/lang_items.rs
+++ b/compiler/rustc_passes/src/lang_items.rs
@@ -78,7 +78,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
                 }
                 // Known lang item with attribute on incorrect target.
                 Some(lang_item) => {
-                    self.tcx.sess.emit_err(LangItemOnIncorrectTarget {
+                    self.tcx.dcx().emit_err(LangItemOnIncorrectTarget {
                         span: attr_span,
                         name,
                         expected_target: lang_item.target(),
@@ -87,7 +87,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
                 }
                 // Unknown lang item.
                 _ => {
-                    self.tcx.sess.emit_err(UnknownLangItem { span: attr_span, name });
+                    self.tcx.dcx().emit_err(UnknownLangItem { span: attr_span, name });
                 }
             }
         }
@@ -149,7 +149,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
                 }
             };
 
-            self.tcx.sess.emit_err(DuplicateLangItem {
+            self.tcx.dcx().emit_err(DuplicateLangItem {
                 local_span: item_span,
                 lang_item_name,
                 crate_name,
@@ -220,7 +220,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
                 // We are issuing E0718 "incorrect target" here, because while the
                 // item kind of the target is correct, the target is still wrong
                 // because of the wrong number of generic arguments.
-                self.tcx.sess.emit_err(IncorrectTarget {
+                self.tcx.dcx().emit_err(IncorrectTarget {
                     span: attr_span,
                     generics_span: generics.span,
                     name: name.as_str(),
diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs
index 2129a98cda3..8d223c23363 100644
--- a/compiler/rustc_passes/src/layout_test.rs
+++ b/compiler/rustc_passes/src/layout_test.rs
@@ -27,7 +27,7 @@ pub fn test_layout(tcx: TyCtxt<'_>) {
                     dump_layout_of(tcx, id, attr);
                 }
                 _ => {
-                    tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id) });
+                    tcx.dcx().emit_err(LayoutInvalidAttribute { span: tcx.def_span(id) });
                 }
             }
         }
@@ -80,23 +80,23 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
             for meta_item in meta_items {
                 match meta_item.name_or_empty() {
                     sym::abi => {
-                        tcx.sess.emit_err(LayoutAbi { span, abi: format!("{:?}", ty_layout.abi) });
+                        tcx.dcx().emit_err(LayoutAbi { span, abi: format!("{:?}", ty_layout.abi) });
                     }
 
                     sym::align => {
-                        tcx.sess.emit_err(LayoutAlign {
+                        tcx.dcx().emit_err(LayoutAlign {
                             span,
                             align: format!("{:?}", ty_layout.align),
                         });
                     }
 
                     sym::size => {
-                        tcx.sess
+                        tcx.dcx()
                             .emit_err(LayoutSize { span, size: format!("{:?}", ty_layout.size) });
                     }
 
                     sym::homogeneous_aggregate => {
-                        tcx.sess.emit_err(LayoutHomogeneousAggregate {
+                        tcx.dcx().emit_err(LayoutHomogeneousAggregate {
                             span,
                             homogeneous_aggregate: format!(
                                 "{:?}",
@@ -115,18 +115,18 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
                         );
                         // FIXME: using the `Debug` impl here isn't ideal.
                         let ty_layout = format!("{:#?}", *ty_layout);
-                        tcx.sess.emit_err(LayoutOf { span, normalized_ty, ty_layout });
+                        tcx.dcx().emit_err(LayoutOf { span, normalized_ty, ty_layout });
                     }
 
                     name => {
-                        tcx.sess.emit_err(UnrecognizedField { span: meta_item.span(), name });
+                        tcx.dcx().emit_err(UnrecognizedField { span: meta_item.span(), name });
                     }
                 }
             }
         }
 
         Err(layout_error) => {
-            tcx.sess.emit_fatal(Spanned { node: layout_error.into_diagnostic(), span });
+            tcx.dcx().emit_fatal(Spanned { node: layout_error.into_diagnostic(), span });
         }
     }
 }
diff --git a/compiler/rustc_passes/src/lib_features.rs b/compiler/rustc_passes/src/lib_features.rs
index 6c0412beda2..e60985ba16f 100644
--- a/compiler/rustc_passes/src/lib_features.rs
+++ b/compiler/rustc_passes/src/lib_features.rs
@@ -97,11 +97,16 @@ impl<'tcx> LibFeatureCollector<'tcx> {
                 Some((FeatureStability::AcceptedSince(prev_since), _)),
             ) => {
                 if prev_since != since {
-                    self.tcx.sess.emit_err(FeatureStableTwice { span, feature, since, prev_since });
+                    self.tcx.dcx().emit_err(FeatureStableTwice {
+                        span,
+                        feature,
+                        since,
+                        prev_since,
+                    });
                 }
             }
             (FeatureStability::AcceptedSince(_), Some((FeatureStability::Unstable, _))) => {
-                self.tcx.sess.emit_err(FeaturePreviouslyDeclared {
+                self.tcx.dcx().emit_err(FeaturePreviouslyDeclared {
                     span,
                     feature,
                     declared: "stable",
@@ -109,7 +114,7 @@ impl<'tcx> LibFeatureCollector<'tcx> {
                 });
             }
             (FeatureStability::Unstable, Some((FeatureStability::AcceptedSince(_), _))) => {
-                self.tcx.sess.emit_err(FeaturePreviouslyDeclared {
+                self.tcx.dcx().emit_err(FeaturePreviouslyDeclared {
                     span,
                     feature,
                     declared: "unstable",
diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs
index c9e5eb50b3a..a8ba9838780 100644
--- a/compiler/rustc_passes/src/liveness.rs
+++ b/compiler/rustc_passes/src/liveness.rs
@@ -592,7 +592,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
         match self.successors[ln] {
             Some(successor) => self.assigned_on_entry(successor, var),
             None => {
-                self.ir.tcx.sess.span_delayed_bug(DUMMY_SP, "no successor");
+                self.ir.tcx.dcx().span_delayed_bug(DUMMY_SP, "no successor");
                 true
             }
         }
diff --git a/compiler/rustc_passes/src/loops.rs b/compiler/rustc_passes/src/loops.rs
index bfaf4a5a957..24db708196b 100644
--- a/compiler/rustc_passes/src/loops.rs
+++ b/compiler/rustc_passes/src/loops.rs
@@ -126,7 +126,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
                     Ok(loop_id) => Some(loop_id),
                     Err(hir::LoopIdError::OutsideLoopScope) => None,
                     Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
-                        self.sess.emit_err(UnlabeledCfInWhileCondition {
+                        self.sess.dcx().emit_err(UnlabeledCfInWhileCondition {
                             span: e.span,
                             cf_type: "break",
                         });
@@ -161,7 +161,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
                                     .label
                                     .map_or_else(String::new, |l| format!(" {}", l.ident))
                             );
-                            self.sess.emit_err(BreakNonLoop {
+                            self.sess.dcx().emit_err(BreakNonLoop {
                                 span: e.span,
                                 head,
                                 kind: kind.name(),
@@ -188,14 +188,14 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
                 match destination.target_id {
                     Ok(loop_id) => {
                         if let Node::Block(block) = self.tcx.opt_hir_node(loop_id).unwrap() {
-                            self.sess.emit_err(ContinueLabeledBlock {
+                            self.sess.dcx().emit_err(ContinueLabeledBlock {
                                 span: e.span,
                                 block_span: block.span,
                             });
                         }
                     }
                     Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
-                        self.sess.emit_err(UnlabeledCfInWhileCondition {
+                        self.sess.dcx().emit_err(UnlabeledCfInWhileCondition {
                             span: e.span,
                             cf_type: "continue",
                         });
@@ -225,17 +225,17 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
         match self.cx {
             LabeledBlock | Loop(_) => {}
             Closure(closure_span) => {
-                self.sess.emit_err(BreakInsideClosure { span, closure_span, name });
+                self.sess.dcx().emit_err(BreakInsideClosure { span, closure_span, name });
             }
             AsyncClosure(closure_span) => {
-                self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name });
+                self.sess.dcx().emit_err(BreakInsideAsyncBlock { span, closure_span, name });
             }
             UnlabeledBlock(block_span) if is_break && block_span.eq_ctxt(break_span) => {
                 let suggestion = Some(OutsideLoopSuggestion { block_span, break_span });
-                self.sess.emit_err(OutsideLoop { span, name, is_break, suggestion });
+                self.sess.dcx().emit_err(OutsideLoop { span, name, is_break, suggestion });
             }
             Normal | Constant | Fn | UnlabeledBlock(_) => {
-                self.sess.emit_err(OutsideLoop { span, name, is_break, suggestion: None });
+                self.sess.dcx().emit_err(OutsideLoop { span, name, is_break, suggestion: None });
             }
         }
     }
@@ -250,7 +250,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
             && self.cx == LabeledBlock
             && label.label.is_none()
         {
-            self.sess.emit_err(UnlabeledInLabeledBlock { span, cf_type });
+            self.sess.dcx().emit_err(UnlabeledInLabeledBlock { span, cf_type });
             return true;
         }
         false
diff --git a/compiler/rustc_passes/src/naked_functions.rs b/compiler/rustc_passes/src/naked_functions.rs
index 25637f935fb..d2cfdb7bf47 100644
--- a/compiler/rustc_passes/src/naked_functions.rs
+++ b/compiler/rustc_passes/src/naked_functions.rs
@@ -61,7 +61,7 @@ fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
 fn check_inline(tcx: TyCtxt<'_>, def_id: LocalDefId) {
     let attrs = tcx.get_attrs(def_id, sym::inline);
     for attr in attrs {
-        tcx.sess.emit_err(CannotInlineNakedFunction { span: attr.span });
+        tcx.dcx().emit_err(CannotInlineNakedFunction { span: attr.span });
     }
 }
 
@@ -86,7 +86,7 @@ fn check_no_patterns(tcx: TyCtxt<'_>, params: &[hir::Param<'_>]) {
             hir::PatKind::Wild
             | hir::PatKind::Binding(hir::BindingAnnotation::NONE, _, _, None) => {}
             _ => {
-                tcx.sess.emit_err(NoPatterns { span: param.pat.span });
+                tcx.dcx().emit_err(NoPatterns { span: param.pat.span });
             }
         }
     }
@@ -116,7 +116,7 @@ impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> {
         )) = expr.kind
         {
             if self.params.contains(var_hir_id) {
-                self.tcx.sess.emit_err(ParamsNotAllowed { span: expr.span });
+                self.tcx.dcx().emit_err(ParamsNotAllowed { span: expr.span });
                 return;
             }
         }
@@ -155,7 +155,7 @@ fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'tcx hir::Body<
         // errors, then don't show an additional error. This allows for appending/prepending
         // `compile_error!("...")` statements and reduces error noise.
         if must_show_error || !has_err {
-            tcx.sess.emit_err(NakedFunctionsAsmBlock {
+            tcx.dcx().emit_err(NakedFunctionsAsmBlock {
                 span: tcx.def_span(def_id),
                 multiple_asms,
                 non_asms,
@@ -241,7 +241,7 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
             })
             .collect();
         if !unsupported_operands.is_empty() {
-            self.tcx.sess.emit_err(NakedFunctionsOperands { unsupported_operands });
+            self.tcx.dcx().emit_err(NakedFunctionsOperands { unsupported_operands });
         }
 
         let unsupported_options: Vec<&'static str> = [
@@ -257,7 +257,7 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
         .collect();
 
         if !unsupported_options.is_empty() {
-            self.tcx.sess.emit_err(NakedFunctionsAsmOptions {
+            self.tcx.dcx().emit_err(NakedFunctionsAsmOptions {
                 span,
                 unsupported_options: unsupported_options.join(", "),
             });
@@ -270,7 +270,7 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
                 .map_or_else(|| asm.template_strs.last().unwrap().2, |op| op.1)
                 .shrink_to_hi();
 
-            self.tcx.sess.emit_err(NakedFunctionsMustUseNoreturn { span, last_span });
+            self.tcx.dcx().emit_err(NakedFunctionsMustUseNoreturn { span, last_span });
         }
     }
 }
diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs
index 676622cef45..87fdedc15ba 100644
--- a/compiler/rustc_passes/src/stability.rs
+++ b/compiler/rustc_passes/src/stability.rs
@@ -180,7 +180,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
                     || (self.in_trait_impl && !self.tcx.is_const_fn_raw(def_id.to_def_id()))
                 {
                     self.tcx
-                        .sess
+                        .dcx()
                         .emit_err(errors::MissingConstErr { fn_sig_span: fn_sig.span, const_span });
                 }
             }
@@ -201,7 +201,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
             && depr.is_since_rustc_version()
             && stab.is_none()
         {
-            self.tcx.sess.emit_err(errors::DeprecatedAttribute { span: *span });
+            self.tcx.dcx().emit_err(errors::DeprecatedAttribute { span: *span });
         }
 
         if let Some((body_stab, _span)) = body_stab {
@@ -216,7 +216,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
             if kind == AnnotationKind::Prohibited
                 || (kind == AnnotationKind::Container && stab.level.is_stable() && is_deprecated)
             {
-                self.tcx.sess.emit_err(errors::UselessStability { span, item_sp });
+                self.tcx.dcx().emit_err(errors::UselessStability { span, item_sp });
             }
 
             debug!("annotate: found {:?}", stab);
@@ -230,12 +230,14 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
             {
                 match stab_since {
                     StableSince::Current => {
-                        self.tcx.sess.emit_err(errors::CannotStabilizeDeprecated { span, item_sp });
+                        self.tcx
+                            .dcx()
+                            .emit_err(errors::CannotStabilizeDeprecated { span, item_sp });
                     }
                     StableSince::Version(stab_since) => {
                         if dep_since < stab_since {
                             self.tcx
-                                .sess
+                                .dcx()
                                 .emit_err(errors::CannotStabilizeDeprecated { span, item_sp });
                         }
                     }
@@ -520,7 +522,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
             && self.effective_visibilities.is_reachable(def_id)
         {
             let descr = self.tcx.def_descr(def_id.to_def_id());
-            self.tcx.sess.emit_err(errors::MissingStabilityAttr { span, descr });
+            self.tcx.dcx().emit_err(errors::MissingStabilityAttr { span, descr });
         }
     }
 
@@ -546,7 +548,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
 
         if is_const && is_stable && missing_const_stability_attribute && is_reachable {
             let descr = self.tcx.def_descr(def_id.to_def_id());
-            self.tcx.sess.emit_err(errors::MissingConstStabAttr { span, descr });
+            self.tcx.dcx().emit_err(errors::MissingConstStabAttr { span, descr });
         }
     }
 }
@@ -751,7 +753,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
                         && self.tcx.is_const_trait_impl_raw(item.owner_id.to_def_id())
                         && const_stab.is_some_and(|(stab, _)| stab.is_const_stable())
                     {
-                        self.tcx.sess.emit_err(errors::TraitImplConstStable { span: item.span });
+                        self.tcx.dcx().emit_err(errors::TraitImplConstStable { span: item.span });
                     }
                 }
 
@@ -929,7 +931,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
         }
         if !lang_features.insert(feature) {
             // Warn if the user enables a lang feature multiple times.
-            tcx.sess.emit_err(errors::DuplicateFeatureErr { span, feature });
+            tcx.dcx().emit_err(errors::DuplicateFeatureErr { span, feature });
         }
     }
 
@@ -937,14 +939,14 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
     let mut remaining_lib_features = FxIndexMap::default();
     for (feature, span) in declared_lib_features {
         if !tcx.sess.opts.unstable_features.is_nightly_build() {
-            tcx.sess.emit_err(errors::FeatureOnlyOnNightly {
+            tcx.dcx().emit_err(errors::FeatureOnlyOnNightly {
                 span: *span,
                 release_channel: env!("CFG_RELEASE_CHANNEL"),
             });
         }
         if remaining_lib_features.contains_key(&feature) {
             // Warn if the user enables a lib feature multiple times.
-            tcx.sess.emit_err(errors::DuplicateFeatureErr { span: *span, feature: *feature });
+            tcx.dcx().emit_err(errors::DuplicateFeatureErr { span: *span, feature: *feature });
         }
         remaining_lib_features.insert(feature, *span);
     }
@@ -1045,7 +1047,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
     }
 
     for (feature, span) in remaining_lib_features {
-        tcx.sess.emit_err(errors::UnknownFeature { span, feature: *feature });
+        tcx.dcx().emit_err(errors::UnknownFeature { span, feature: *feature });
     }
 
     // We only use the hash map contents to emit errors, and the order of
@@ -1058,7 +1060,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
             .get(&feature)
             .expect("feature that implied another does not exist")
             .1;
-        tcx.sess.emit_err(errors::ImpliedFeatureNotExist { span, feature, implied_by });
+        tcx.dcx().emit_err(errors::ImpliedFeatureNotExist { span, feature, implied_by });
     }
 
     // FIXME(#44232): the `used_features` table no longer exists, so we
diff --git a/compiler/rustc_passes/src/weak_lang_items.rs b/compiler/rustc_passes/src/weak_lang_items.rs
index b226c65e96c..db3d442676e 100644
--- a/compiler/rustc_passes/src/weak_lang_items.rs
+++ b/compiler/rustc_passes/src/weak_lang_items.rs
@@ -44,7 +44,7 @@ impl<'ast> visit::Visitor<'ast> for WeakLangItemVisitor<'_, '_> {
                     self.items.missing.push(item);
                 }
             } else {
-                self.tcx.sess.emit_err(UnknownExternLangItem { span: i.span, lang_item });
+                self.tcx.dcx().emit_err(UnknownExternLangItem { span: i.span, lang_item });
             }
         }
     }
@@ -75,9 +75,9 @@ fn verify(tcx: TyCtxt<'_>, items: &lang_items::LanguageItems) {
     for &item in WEAK_LANG_ITEMS.iter() {
         if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() {
             if item == LangItem::PanicImpl {
-                tcx.sess.emit_err(MissingPanicHandler);
+                tcx.dcx().emit_err(MissingPanicHandler);
             } else {
-                tcx.sess.emit_err(MissingLangItem { name: item.name() });
+                tcx.dcx().emit_err(MissingLangItem { name: item.name() });
             }
         }
     }