about summary refs log tree commit diff
path: root/compiler/rustc_passes/src/check_attr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_passes/src/check_attr.rs')
-rw-r--r--compiler/rustc_passes/src/check_attr.rs168
1 files changed, 88 insertions, 80 deletions
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(),