about summary refs log tree commit diff
path: root/compiler/rustc_hir_analysis/src/check
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_hir_analysis/src/check')
-rw-r--r--compiler/rustc_hir_analysis/src/check/check.rs58
-rw-r--r--compiler/rustc_hir_analysis/src/check/compare_impl_item.rs30
-rw-r--r--compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs6
-rw-r--r--compiler/rustc_hir_analysis/src/check/dropck.rs12
-rw-r--r--compiler/rustc_hir_analysis/src/check/entry.rs23
-rw-r--r--compiler/rustc_hir_analysis/src/check/intrinsic.rs12
-rw-r--r--compiler/rustc_hir_analysis/src/check/intrinsicck.rs20
-rw-r--r--compiler/rustc_hir_analysis/src/check/mod.rs20
-rw-r--r--compiler/rustc_hir_analysis/src/check/wfcheck.rs29
9 files changed, 106 insertions, 104 deletions
diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs
index 8413a1cc0db..6bef57a3084 100644
--- a/compiler/rustc_hir_analysis/src/check/check.rs
+++ b/compiler/rustc_hir_analysis/src/check/check.rs
@@ -38,7 +38,7 @@ pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
         Some(true) => (),
         Some(false) => {
             struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 span,
                 E0570,
                 "`{abi}` is not a supported ABI for the current target",
@@ -59,7 +59,7 @@ pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
     // This ABI is only allowed on function pointers
     if abi == Abi::CCmseNonSecureCall {
         struct_span_err!(
-            tcx.sess,
+            tcx.dcx(),
             span,
             E0781,
             "the `\"C-cmse-nonsecure-call\"` ABI is only allowed on function pointers"
@@ -126,7 +126,7 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
         for field in &def.non_enum_variant().fields {
             let Ok(field_ty) = tcx.try_normalize_erasing_regions(param_env, field.ty(tcx, args))
             else {
-                tcx.sess.span_delayed_bug(span, "could not normalize field type");
+                tcx.dcx().span_delayed_bug(span, "could not normalize field type");
                 continue;
             };
 
@@ -136,7 +136,7 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
                     Some(Node::Field(field)) => (field.span, field.ty.span),
                     _ => unreachable!("mir field has to correspond to hir field"),
                 };
-                tcx.sess.emit_err(errors::InvalidUnionField {
+                tcx.dcx().emit_err(errors::InvalidUnionField {
                     field_span,
                     sugg: errors::InvalidUnionFieldSuggestion {
                         lo: ty_span.shrink_to_lo(),
@@ -147,7 +147,7 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
                 return false;
             } else if field_ty.needs_drop(tcx, param_env) {
                 // This should never happen. But we can get here e.g. in case of name resolution errors.
-                tcx.sess
+                tcx.dcx()
                     .span_delayed_bug(span, "we should never accept maybe-dropping union fields");
             }
         }
@@ -173,12 +173,12 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
             if matches!(tcx.def_kind(def_id), DefKind::Static(_)
                 if tcx.def_kind(tcx.local_parent(def_id)) == DefKind::ForeignMod) =>
         {
-            tcx.sess.emit_err(errors::TooLargeStatic { span });
+            tcx.dcx().emit_err(errors::TooLargeStatic { span });
             return;
         }
         // Generic statics are rejected, but we still reach this case.
         Err(e) => {
-            tcx.sess.span_delayed_bug(span, format!("{e:?}"));
+            tcx.dcx().span_delayed_bug(span, format!("{e:?}"));
             return;
         }
     };
@@ -201,7 +201,7 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
 fn check_opaque(tcx: TyCtxt<'_>, id: hir::ItemId) {
     let item = tcx.hir().item(id);
     let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item.kind else {
-        tcx.sess.span_delayed_bug(item.span, "expected opaque item");
+        tcx.dcx().span_delayed_bug(item.span, "expected opaque item");
         return;
     };
 
@@ -310,7 +310,7 @@ fn check_opaque_meets_bounds<'tcx>(
         Ok(()) => {}
         Err(ty_err) => {
             let ty_err = ty_err.to_string(tcx);
-            return Err(tcx.sess.span_delayed_bug(
+            return Err(tcx.dcx().span_delayed_bug(
                 span,
                 format!("could not unify `{hidden_ty}` with revealed type:\n{ty_err}"),
             ));
@@ -435,7 +435,7 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
             ty::Adt(adt_def, args) => !is_enum_of_nonnullable_ptr(tcx, *adt_def, *args),
             _ => true,
         } {
-            tcx.sess.emit_err(LinkageType { span: tcx.def_span(def_id) });
+            tcx.dcx().emit_err(LinkageType { span: tcx.def_span(def_id) });
         }
     }
 }
@@ -554,7 +554,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
                                 _ => ("type or const", "types or consts", None),
                             };
                             struct_span_err!(
-                                tcx.sess,
+                                tcx.dcx(),
                                 item.span,
                                 E0044,
                                 "foreign items may not have {kinds} parameters",
@@ -652,7 +652,7 @@ pub(super) fn check_specialization_validity<'tcx>(
         if !tcx.is_impl_trait_in_trait(impl_item) {
             report_forbidden_specialization(tcx, impl_item, parent_impl);
         } else {
-            tcx.sess.span_delayed_bug(
+            tcx.dcx().span_delayed_bug(
                 DUMMY_SP,
                 format!("parent item: {parent_impl:?} not marked as default"),
             );
@@ -681,7 +681,7 @@ fn check_impl_items_against_trait<'tcx>(
             if let [first_item_ref, ..] = impl_item_refs {
                 let first_item_span = tcx.def_span(first_item_ref);
                 struct_span_err!(
-                    tcx.sess,
+                    tcx.dcx(),
                     first_item_span,
                     E0749,
                     "negative impls cannot have any items"
@@ -700,7 +700,7 @@ fn check_impl_items_against_trait<'tcx>(
             tcx.associated_item(trait_item_id)
         } else {
             // Checked in `associated_item`.
-            tcx.sess.span_delayed_bug(tcx.def_span(impl_item), "missing associated item in trait");
+            tcx.dcx().span_delayed_bug(tcx.def_span(impl_item), "missing associated item in trait");
             continue;
         };
         match ty_impl_item.kind {
@@ -795,7 +795,7 @@ fn check_impl_items_against_trait<'tcx>(
                         "return position `impl Trait` in traits",
                     )
                 };
-                tcx.sess
+                tcx.dcx()
                     .struct_span_err(tcx.def_span(def_id), msg)
                     .note(format!(
                         "specialization behaves in inconsistent and \
@@ -833,12 +833,12 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
     {
         let fields = &def.non_enum_variant().fields;
         if fields.is_empty() {
-            struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
+            struct_span_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
             return;
         }
         let e = fields[FieldIdx::from_u32(0)].ty(tcx, args);
         if !fields.iter().all(|f| f.ty(tcx, args) == e) {
-            struct_span_err!(tcx.sess, sp, E0076, "SIMD vector should be homogeneous")
+            struct_span_err!(tcx.dcx(), sp, E0076, "SIMD vector should be homogeneous")
                 .span_label(sp, "SIMD elements must have the same type")
                 .emit();
             return;
@@ -851,11 +851,11 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
         };
         if let Some(len) = len {
             if len == 0 {
-                struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
+                struct_span_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
                 return;
             } else if len > MAX_SIMD_LANES {
                 struct_span_err!(
-                    tcx.sess,
+                    tcx.dcx(),
                     sp,
                     E0075,
                     "SIMD vector cannot have more than {MAX_SIMD_LANES} elements",
@@ -878,7 +878,7 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
             { /* struct([f32; 4]) is ok */ }
             _ => {
                 struct_span_err!(
-                    tcx.sess,
+                    tcx.dcx(),
                     sp,
                     E0077,
                     "SIMD vector element type should be a \
@@ -901,7 +901,7 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
                     && pack as u64 != repr_pack.bytes()
                 {
                     struct_span_err!(
-                        tcx.sess,
+                        tcx.dcx(),
                         sp,
                         E0634,
                         "type has conflicting packed representation hints"
@@ -912,7 +912,7 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
         }
         if repr.align.is_some() {
             struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 sp,
                 E0587,
                 "type has conflicting packed and align representation hints"
@@ -921,7 +921,7 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
         } else {
             if let Some(def_spans) = check_packed_inner(tcx, def.did(), &mut vec![]) {
                 let mut err = struct_span_err!(
-                    tcx.sess,
+                    tcx.dcx(),
                     sp,
                     E0588,
                     "packed type cannot transitively contain a `#[repr(align)]` type"
@@ -1111,7 +1111,7 @@ fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
     if def.variants().is_empty() {
         if let Some(attr) = tcx.get_attrs(def_id, sym::repr).next() {
             struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 attr.span,
                 E0084,
                 "unsupported representation for zero-variant enum"
@@ -1150,7 +1150,7 @@ fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
 
         if disr_non_unit || (disr_units && has_non_units) {
             let mut err = struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 tcx.def_span(def_id),
                 E0732,
                 "`#[repr(inttype)]` must be specified"
@@ -1236,7 +1236,7 @@ fn detect_discriminant_duplicate<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
             if discrs[i].1.val == discrs[o].1.val {
                 let err = error.get_or_insert_with(|| {
                     let mut ret = struct_span_err!(
-                        tcx.sess,
+                        tcx.dcx(),
                         tcx.def_span(adt.did()),
                         E0081,
                         "discriminant value `{}` assigned more than once",
@@ -1302,7 +1302,7 @@ pub(super) fn check_type_params_are_used<'tcx>(
             && let ty::GenericParamDefKind::Type { .. } = param.kind
         {
             let span = tcx.def_span(param.def_id);
-            struct_span_err!(tcx.sess, span, E0091, "type parameter `{}` is unused", param.name,)
+            struct_span_err!(tcx.dcx(), span, E0091, "type parameter `{}` is unused", param.name,)
                 .span_label(span, "unused type parameter")
                 .emit();
         }
@@ -1320,7 +1320,7 @@ pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId
 }
 
 fn async_opaque_type_cycle_error(tcx: TyCtxt<'_>, span: Span) -> ErrorGuaranteed {
-    struct_span_err!(tcx.sess, span, E0733, "recursion in an `async fn` requires boxing")
+    struct_span_err!(tcx.dcx(), span, E0733, "recursion in an `async fn` requires boxing")
         .span_label(span, "recursive `async fn`")
         .note("a recursive `async fn` must be rewritten to return a boxed `dyn Future`")
         .note(
@@ -1342,7 +1342,7 @@ fn opaque_type_cycle_error(
     opaque_def_id: LocalDefId,
     span: Span,
 ) -> ErrorGuaranteed {
-    let mut err = struct_span_err!(tcx.sess, span, E0720, "cannot resolve opaque type");
+    let mut err = struct_span_err!(tcx.dcx(), span, E0720, "cannot resolve opaque type");
 
     let mut label = false;
     if let Some((def_id, visitor)) = get_owner_return_paths(tcx, opaque_def_id) {
diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
index 264868fdfc7..dbcaa244f29 100644
--- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
+++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
@@ -429,7 +429,7 @@ fn compare_asyncness<'tcx>(
             }
             _ => {
                 return Err(tcx
-                    .sess
+                    .dcx()
                     .create_err(crate::errors::AsyncTraitImplShouldBeAsync {
                         span: tcx.def_span(impl_m.def_id),
                         method_name: trait_m.name,
@@ -626,7 +626,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
         Ok(()) => {}
         Err(terr) => {
             let mut diag = struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 cause.span(),
                 E0053,
                 "method `{}` has an incompatible return type for trait",
@@ -759,7 +759,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
                 remapped_types.insert(def_id, ty::EarlyBinder::bind(ty));
             }
             Err(err) => {
-                let reported = tcx.sess.span_delayed_bug(
+                let reported = tcx.dcx().span_delayed_bug(
                     return_span,
                     format!("could not fully resolve: {ty} => {err:?}"),
                 );
@@ -929,7 +929,7 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
                         self.return_span
                     };
                     self.tcx
-                        .sess
+                        .dcx()
                         .struct_span_err(
                             return_span,
                             "return type captures more lifetimes than trait definition",
@@ -943,7 +943,7 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
                         .emit()
                 }
                 _ => {
-                    self.tcx.sess.span_delayed_bug(DUMMY_SP, "should've been able to remap region")
+                    self.tcx.dcx().span_delayed_bug(DUMMY_SP, "should've been able to remap region")
                 }
             };
             return Err(guar);
@@ -973,7 +973,7 @@ fn report_trait_method_mismatch<'tcx>(
         extract_spans_for_error_reporting(infcx, terr, &cause, impl_m, trait_m);
 
     let mut diag = struct_span_err!(
-        tcx.sess,
+        tcx.dcx(),
         impl_err_span,
         E0053,
         "method `{}` has an incompatible type for trait",
@@ -1134,7 +1134,7 @@ fn check_region_bounds_on_impl_item<'tcx>(
             }
         }
         let reported = tcx
-            .sess
+            .dcx()
             .create_err(LifetimesOrBoundsMismatchOnTrait {
                 span,
                 item_kind: assoc_item_kind_str(&impl_m),
@@ -1218,7 +1218,7 @@ fn compare_self_type<'tcx>(
             let self_descr = self_string(impl_m);
             let impl_m_span = tcx.def_span(impl_m.def_id);
             let mut err = struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 impl_m_span,
                 E0185,
                 "method `{}` has a `{}` declaration in the impl, but not in the trait",
@@ -1238,7 +1238,7 @@ fn compare_self_type<'tcx>(
             let self_descr = self_string(trait_m);
             let impl_m_span = tcx.def_span(impl_m.def_id);
             let mut err = struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 impl_m_span,
                 E0186,
                 "method `{}` has a `{}` declaration in the trait, but not in the impl",
@@ -1303,7 +1303,7 @@ fn compare_number_of_generics<'tcx>(
     // inheriting the generics from will also have mismatched arguments, and
     // we'll report an error for that instead. Delay a bug for safety, though.
     if trait_.is_impl_trait_in_trait() {
-        return Err(tcx.sess.span_delayed_bug(
+        return Err(tcx.dcx().span_delayed_bug(
             rustc_span::DUMMY_SP,
             "errors comparing numbers of generics of trait/impl functions were not emitted",
         ));
@@ -1371,7 +1371,7 @@ fn compare_number_of_generics<'tcx>(
             let spans = arg_spans(impl_.kind, impl_item.generics);
             let span = spans.first().copied();
 
-            let mut err = tcx.sess.struct_span_err_with_code(
+            let mut err = tcx.dcx().struct_span_err_with_code(
                 spans,
                 format!(
                     "{} `{}` has {} {kind} parameter{} but its trait \
@@ -1464,7 +1464,7 @@ fn compare_number_of_method_arguments<'tcx>(
             .unwrap_or_else(|| tcx.def_span(impl_m.def_id));
 
         let mut err = struct_span_err!(
-            tcx.sess,
+            tcx.dcx(),
             impl_span,
             E0050,
             "method `{}` has {} but the declaration in trait `{}` has {}",
@@ -1531,7 +1531,7 @@ fn compare_synthetic_generics<'tcx>(
             let impl_span = tcx.def_span(impl_def_id);
             let trait_span = tcx.def_span(trait_def_id);
             let mut err = struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 impl_span,
                 E0643,
                 "method `{}` has incompatible signature for trait",
@@ -1690,7 +1690,7 @@ fn compare_generic_param_kinds<'tcx>(
             let param_trait_span = tcx.def_span(param_trait.def_id);
 
             let mut err = struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 param_impl_span,
                 E0053,
                 "{} `{}` has an incompatible generic parameter for trait `{}`",
@@ -1837,7 +1837,7 @@ fn compare_const_predicate_entailment<'tcx>(
         cause.span = ty.span;
 
         let mut diag = struct_span_err!(
-            tcx.sess,
+            tcx.dcx(),
             cause.span,
             E0326,
             "implemented const `{}` has an incompatible type for trait",
diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs
index 67796855ece..71fbd983b6a 100644
--- a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs
+++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs
@@ -153,7 +153,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
         trait_m_sig.inputs_and_output,
     ));
     if !ocx.select_all_or_error().is_empty() {
-        tcx.sess.span_delayed_bug(
+        tcx.dcx().span_delayed_bug(
             DUMMY_SP,
             "encountered errors when checking RPITIT refinement (selection)",
         );
@@ -165,7 +165,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
     );
     let errors = infcx.resolve_regions(&outlives_env);
     if !errors.is_empty() {
-        tcx.sess.span_delayed_bug(
+        tcx.dcx().span_delayed_bug(
             DUMMY_SP,
             "encountered errors when checking RPITIT refinement (regions)",
         );
@@ -173,7 +173,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
     }
     // Resolve any lifetime variables that may have been introduced during normalization.
     let Ok((trait_bounds, impl_bounds)) = infcx.fully_resolve((trait_bounds, impl_bounds)) else {
-        tcx.sess.span_delayed_bug(
+        tcx.dcx().span_delayed_bug(
             DUMMY_SP,
             "encountered errors when checking RPITIT refinement (resolution)",
         );
diff --git a/compiler/rustc_hir_analysis/src/check/dropck.rs b/compiler/rustc_hir_analysis/src/check/dropck.rs
index 58c77bb45cb..3492499db68 100644
--- a/compiler/rustc_hir_analysis/src/check/dropck.rs
+++ b/compiler/rustc_hir_analysis/src/check/dropck.rs
@@ -34,12 +34,12 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro
     match tcx.impl_polarity(drop_impl_did) {
         ty::ImplPolarity::Positive => {}
         ty::ImplPolarity::Negative => {
-            return Err(tcx.sess.emit_err(errors::DropImplPolarity::Negative {
+            return Err(tcx.dcx().emit_err(errors::DropImplPolarity::Negative {
                 span: tcx.def_span(drop_impl_did),
             }));
         }
         ty::ImplPolarity::Reservation => {
-            return Err(tcx.sess.emit_err(errors::DropImplPolarity::Reservation {
+            return Err(tcx.dcx().emit_err(errors::DropImplPolarity::Reservation {
                 span: tcx.def_span(drop_impl_did),
             }));
         }
@@ -66,7 +66,7 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro
             // already checked by coherence, but compilation may
             // not have been terminated.
             let span = tcx.def_span(drop_impl_did);
-            let reported = tcx.sess.span_delayed_bug(
+            let reported = tcx.dcx().span_delayed_bug(
                 span,
                 format!("should have been rejected by coherence check: {dtor_self_type}"),
             );
@@ -89,7 +89,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
     let item_span = tcx.def_span(self_type_did);
     let self_descr = tcx.def_descr(self_type_did);
     let mut err =
-        struct_span_err!(tcx.sess, drop_impl_span, E0366, "`Drop` impls cannot be specialized");
+        struct_span_err!(tcx.dcx(), drop_impl_span, E0366, "`Drop` impls cannot be specialized");
     match arg {
         ty::util::NotUniqueParam::DuplicateParam(arg) => {
             err.note(format!("`{arg}` is mentioned multiple times"))
@@ -155,7 +155,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
                 let self_descr = tcx.def_descr(adt_def_id.to_def_id());
                 guar = Some(
                     struct_span_err!(
-                        tcx.sess,
+                        tcx.dcx(),
                         error.root_obligation.cause.span,
                         E0367,
                         "`Drop` impl requires `{root_predicate}` \
@@ -187,7 +187,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
             };
             guar = Some(
                 struct_span_err!(
-                    tcx.sess,
+                    tcx.dcx(),
                     error.origin().span(),
                     E0367,
                     "`Drop` impl requires `{outlives}` \
diff --git a/compiler/rustc_hir_analysis/src/check/entry.rs b/compiler/rustc_hir_analysis/src/check/entry.rs
index a82853a1303..649ac6c5aeb 100644
--- a/compiler/rustc_hir_analysis/src/check/entry.rs
+++ b/compiler/rustc_hir_analysis/src/check/entry.rs
@@ -96,12 +96,13 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
     let main_asyncness = tcx.asyncness(main_def_id);
     if main_asyncness.is_async() {
         let asyncness_span = main_fn_asyncness_span(tcx, main_def_id);
-        tcx.sess.emit_err(errors::MainFunctionAsync { span: main_span, asyncness: asyncness_span });
+        tcx.dcx()
+            .emit_err(errors::MainFunctionAsync { span: main_span, asyncness: asyncness_span });
         error = true;
     }
 
     for attr in tcx.get_attrs(main_def_id, sym::track_caller) {
-        tcx.sess.emit_err(errors::TrackCallerOnMain { span: attr.span, annotated: main_span });
+        tcx.dcx().emit_err(errors::TrackCallerOnMain { span: attr.span, annotated: main_span });
         error = true;
     }
 
@@ -110,7 +111,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
         && !tcx.sess.target.is_like_wasm
         && !tcx.sess.opts.actually_rustdoc
     {
-        tcx.sess.emit_err(errors::TargetFeatureOnMain { main: main_span });
+        tcx.dcx().emit_err(errors::TargetFeatureOnMain { main: main_span });
         error = true;
     }
 
@@ -125,7 +126,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
         let return_ty = main_fnsig.output();
         let return_ty_span = main_fn_return_type_span(tcx, main_def_id).unwrap_or(main_span);
         let Some(return_ty) = return_ty.no_bound_vars() else {
-            tcx.sess.emit_err(errors::MainFunctionReturnTypeGeneric { span: return_ty_span });
+            tcx.dcx().emit_err(errors::MainFunctionReturnTypeGeneric { span: return_ty_span });
             return;
         };
         let infcx = tcx.infer_ctxt().build();
@@ -180,14 +181,14 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
     let main_fn_predicates = tcx.predicates_of(main_def_id);
     if main_fn_generics.count() != 0 || !main_fnsig.bound_vars().is_empty() {
         let generics_param_span = main_fn_generics_params_span(tcx, main_def_id);
-        tcx.sess.emit_err(errors::MainFunctionGenericParameters {
+        tcx.dcx().emit_err(errors::MainFunctionGenericParameters {
             span: generics_param_span.unwrap_or(main_span),
             label_span: generics_param_span,
         });
     } else if !main_fn_predicates.predicates.is_empty() {
         // generics may bring in implicit predicates, so we skip this check if generics is present.
         let generics_where_clauses_span = main_fn_where_clauses_span(tcx, main_def_id);
-        tcx.sess.emit_err(errors::WhereClauseOnMain {
+        tcx.dcx().emit_err(errors::WhereClauseOnMain {
             span: generics_where_clauses_span.unwrap_or(main_span),
             generics_span: generics_where_clauses_span,
         });
@@ -205,25 +206,25 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
                 if let hir::ItemKind::Fn(sig, generics, _) = &it.kind {
                     let mut error = false;
                     if !generics.params.is_empty() {
-                        tcx.sess.emit_err(errors::StartFunctionParameters { span: generics.span });
+                        tcx.dcx().emit_err(errors::StartFunctionParameters { span: generics.span });
                         error = true;
                     }
                     if generics.has_where_clause_predicates {
-                        tcx.sess.emit_err(errors::StartFunctionWhere {
+                        tcx.dcx().emit_err(errors::StartFunctionWhere {
                             span: generics.where_clause_span,
                         });
                         error = true;
                     }
                     if sig.header.asyncness.is_async() {
                         let span = tcx.def_span(it.owner_id);
-                        tcx.sess.emit_err(errors::StartAsync { span: span });
+                        tcx.dcx().emit_err(errors::StartAsync { span: span });
                         error = true;
                     }
 
                     let attrs = tcx.hir().attrs(start_id);
                     for attr in attrs {
                         if attr.has_name(sym::track_caller) {
-                            tcx.sess.emit_err(errors::StartTrackCaller {
+                            tcx.dcx().emit_err(errors::StartTrackCaller {
                                 span: attr.span,
                                 start: start_span,
                             });
@@ -235,7 +236,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
                             && !tcx.sess.target.is_like_wasm
                             && !tcx.sess.opts.actually_rustdoc
                         {
-                            tcx.sess.emit_err(errors::StartTargetFeature {
+                            tcx.dcx().emit_err(errors::StartTargetFeature {
                                 span: attr.span,
                                 start: start_span,
                             });
diff --git a/compiler/rustc_hir_analysis/src/check/intrinsic.rs b/compiler/rustc_hir_analysis/src/check/intrinsic.rs
index 126bab68ae3..632af780ed8 100644
--- a/compiler/rustc_hir_analysis/src/check/intrinsic.rs
+++ b/compiler/rustc_hir_analysis/src/check/intrinsic.rs
@@ -29,7 +29,7 @@ fn equate_intrinsic_type<'tcx>(
             (own_counts, generics.span)
         }
         _ => {
-            struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function")
+            struct_span_err!(tcx.dcx(), it.span, E0622, "intrinsic must be a function")
                 .span_label(it.span, "expected a function")
                 .emit();
             return;
@@ -38,7 +38,7 @@ fn equate_intrinsic_type<'tcx>(
 
     let gen_count_ok = |found: usize, expected: usize, descr: &str| -> bool {
         if found != expected {
-            tcx.sess.emit_err(WrongNumberOfGenericArgumentsToIntrinsic {
+            tcx.dcx().emit_err(WrongNumberOfGenericArgumentsToIntrinsic {
                 span,
                 found,
                 expected,
@@ -117,7 +117,7 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir
     };
 
     if has_safe_attr != is_in_list {
-        tcx.sess.struct_span_err(
+        tcx.dcx().struct_span_err(
             tcx.def_span(intrinsic_id),
             DiagnosticMessage::from(format!(
                 "intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `{}`",
@@ -176,7 +176,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
             | "umin" => (1, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], param(0)),
             "fence" | "singlethreadfence" => (0, Vec::new(), Ty::new_unit(tcx)),
             op => {
-                tcx.sess.emit_err(UnrecognizedAtomicOperation { span: it.span, op });
+                tcx.dcx().emit_err(UnrecognizedAtomicOperation { span: it.span, op });
                 return;
             }
         };
@@ -460,7 +460,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
             }
 
             other => {
-                tcx.sess.emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
+                tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
                 return;
             }
         };
@@ -552,7 +552,7 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
         sym::simd_shuffle_generic => (2, 1, vec![param(0), param(0)], param(1)),
         _ => {
             let msg = format!("unrecognized platform-specific intrinsic function: `{name}`");
-            tcx.sess.struct_span_err(it.span, msg).emit();
+            tcx.dcx().struct_span_err(it.span, msg).emit();
             return;
         }
     };
diff --git a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs
index d86ebc2c9c3..ac18e6de0ba 100644
--- a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs
+++ b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs
@@ -153,7 +153,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
         };
         let Some(asm_ty) = asm_ty else {
             let msg = format!("cannot use value of type `{ty}` for inline assembly");
-            let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
+            let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
             err.note(
                 "only integers, floats, SIMD vectors, pointers and function pointers \
                  can be used as arguments for inline assembly",
@@ -166,7 +166,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
         // possibly fail is for SIMD types which don't #[derive(Copy)].
         if !ty.is_copy_modulo_regions(self.tcx, self.param_env) {
             let msg = "arguments for inline assembly must be copyable";
-            let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
+            let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
             err.note(format!("`{ty}` does not implement the Copy trait"));
             err.emit();
         }
@@ -183,7 +183,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
         if let Some((in_expr, Some(in_asm_ty))) = tied_input {
             if in_asm_ty != asm_ty {
                 let msg = "incompatible types for asm inout argument";
-                let mut err = self.tcx.sess.struct_span_err(vec![in_expr.span, expr.span], msg);
+                let mut err = self.tcx.dcx().struct_span_err(vec![in_expr.span, expr.span], msg);
 
                 let in_expr_ty = (self.get_operand_ty)(in_expr);
                 err.span_label(in_expr.span, format!("type `{in_expr_ty}`"));
@@ -207,7 +207,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
         let supported_tys = reg_class.supported_types(asm_arch);
         let Some((_, feature)) = supported_tys.iter().find(|&&(t, _)| t == asm_ty) else {
             let msg = format!("type `{ty}` cannot be used with this register class");
-            let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
+            let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
             let supported_tys: Vec<_> = supported_tys.iter().map(|(t, _)| t.to_string()).collect();
             err.note(format!(
                 "register class `{}` supports these types: {}",
@@ -234,7 +234,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
         if let Some(feature) = feature {
             if !target_features.contains(feature) {
                 let msg = format!("`{feature}` target feature is not enabled");
-                let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
+                let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
                 err.note(format!(
                     "this is required to use type `{}` with register class `{}`",
                     ty,
@@ -287,7 +287,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
     pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: LocalDefId) {
         let target_features = self.tcx.asm_target_features(enclosing_id.to_def_id());
         let Some(asm_arch) = self.tcx.sess.asm_arch else {
-            self.tcx.sess.span_delayed_bug(DUMMY_SP, "target architecture does not support asm");
+            self.tcx.dcx().span_delayed_bug(DUMMY_SP, "target architecture does not support asm");
             return;
         };
         for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
@@ -318,7 +318,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
                         op.is_clobber(),
                     ) {
                         let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
-                        self.tcx.sess.struct_span_err(*op_sp, msg).emit();
+                        self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
                         continue;
                     }
                 }
@@ -357,7 +357,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
                                 reg_class.name(),
                                 feature
                             );
-                            self.tcx.sess.struct_span_err(*op_sp, msg).emit();
+                            self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
                             // register isn't enabled, don't do more checks
                             continue;
                         }
@@ -371,7 +371,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
                                     .intersperse(", ")
                                     .collect::<String>(),
                             );
-                            self.tcx.sess.struct_span_err(*op_sp, msg).emit();
+                            self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
                             // register isn't enabled, don't do more checks
                             continue;
                         }
@@ -450,7 +450,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
                         ty::FnDef(..) => {}
                         _ => {
                             let mut err =
-                                self.tcx.sess.struct_span_err(*op_sp, "invalid `sym` operand");
+                                self.tcx.dcx().struct_span_err(*op_sp, "invalid `sym` operand");
                             err.span_label(
                                 self.tcx.def_span(anon_const.def_id),
                                 format!("is {} `{}`", ty.kind().article(), ty),
diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs
index e4904a0437b..faec72cfeb6 100644
--- a/compiler/rustc_hir_analysis/src/check/mod.rs
+++ b/compiler/rustc_hir_analysis/src/check/mod.rs
@@ -144,7 +144,7 @@ fn get_owner_return_paths(
 // FIXME: Move this to a more appropriate place.
 pub fn forbid_intrinsic_abi(tcx: TyCtxt<'_>, sp: Span, abi: Abi) {
     if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = abi {
-        tcx.sess.span_err(sp, "intrinsic must be in `extern \"rust-intrinsic\" { ... }` block");
+        tcx.dcx().span_err(sp, "intrinsic must be in `extern \"rust-intrinsic\" { ... }` block");
     }
 }
 
@@ -174,7 +174,7 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
         let msg = "statics with a custom `#[link_section]` must be a \
                         simple list of bytes on the wasm target with no \
                         extra levels of indirection such as references";
-        tcx.sess.span_err(tcx.def_span(id), msg);
+        tcx.dcx().span_err(tcx.def_span(id), msg);
     }
 }
 
@@ -187,7 +187,7 @@ fn report_forbidden_specialization(tcx: TyCtxt<'_>, impl_item: DefId, parent_imp
         Err(cname) => errors::ImplNotMarkedDefault::Err { span, ident, cname },
     };
 
-    tcx.sess.emit_err(err);
+    tcx.dcx().emit_err(err);
 }
 
 fn missing_items_err(
@@ -240,7 +240,7 @@ fn missing_items_err(
         }
     }
 
-    tcx.sess.emit_err(errors::MissingTraitItem {
+    tcx.dcx().emit_err(errors::MissingTraitItem {
         span: tcx.span_of_impl(impl_def_id.to_def_id()).unwrap(),
         missing_items_msg,
         missing_trait_item_label,
@@ -258,7 +258,7 @@ fn missing_items_must_implement_one_of_err(
     let missing_items_msg =
         missing_items.iter().map(Ident::to_string).collect::<Vec<_>>().join("`, `");
 
-    tcx.sess.emit_err(errors::MissingOneOfTraitItem {
+    tcx.dcx().emit_err(errors::MissingOneOfTraitItem {
         span: impl_span,
         note: annotation_span,
         missing_items_msg,
@@ -283,7 +283,7 @@ fn default_body_is_unstable(
         None => none_note = true,
     };
 
-    let mut err = tcx.sess.create_err(errors::MissingTraitItemUnstable {
+    let mut err = tcx.dcx().create_err(errors::MissingTraitItemUnstable {
         span: impl_span,
         some_note,
         none_note,
@@ -526,7 +526,7 @@ fn bad_variant_count<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>, sp: Span, d
         spans = start.to_vec();
         many = Some(*end);
     }
-    tcx.sess.emit_err(errors::TransparentEnumVariant {
+    tcx.dcx().emit_err(errors::TransparentEnumVariant {
         span: sp,
         spans,
         many,
@@ -545,14 +545,14 @@ fn bad_non_zero_sized_fields<'tcx>(
     sp: Span,
 ) {
     if adt.is_enum() {
-        tcx.sess.emit_err(errors::TransparentNonZeroSizedEnum {
+        tcx.dcx().emit_err(errors::TransparentNonZeroSizedEnum {
             span: sp,
             spans: field_spans.collect(),
             field_count,
             desc: adt.descr(),
         });
     } else {
-        tcx.sess.emit_err(errors::TransparentNonZeroSized {
+        tcx.dcx().emit_err(errors::TransparentNonZeroSized {
             span: sp,
             spans: field_spans.collect(),
             field_count,
@@ -616,7 +616,7 @@ pub fn check_function_signature<'tcx>(
                 cause.span = extract_span_for_error_reporting(tcx, err, &cause, local_id);
             }
             let failure_code = cause.as_failure_code_diag(err, cause.span, vec![]);
-            let mut diag = tcx.sess.create_err(failure_code);
+            let mut diag = tcx.dcx().create_err(failure_code);
             err_ctxt.note_type_err(
                 &mut diag,
                 &cause,
diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs
index cc34dbfd9b9..efeb15be761 100644
--- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs
+++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs
@@ -198,7 +198,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
             if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) {
                 let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span);
                 let mut err =
-                    tcx.sess.struct_span_err(sp, "impls of auto traits cannot be default");
+                    tcx.dcx().struct_span_err(sp, "impls of auto traits cannot be default");
                 err.span_labels(impl_.defaultness_span, "default because of this");
                 err.span_label(sp, "auto trait");
                 res = Err(err.emit());
@@ -217,7 +217,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
                         let mut spans = vec![span];
                         spans.extend(impl_.defaultness_span);
                         res = Err(struct_span_err!(
-                            tcx.sess,
+                            tcx.dcx(),
                             spans,
                             E0750,
                             "negative impls cannot be default impls"
@@ -485,7 +485,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
 
         if !unsatisfied_bounds.is_empty() {
             let plural = pluralize!(unsatisfied_bounds.len());
-            let mut err = tcx.sess.struct_span_err(
+            let mut err = tcx.dcx().struct_span_err(
                 gat_item_hir.span,
                 format!("missing required bound{} on `{}`", plural, gat_item_hir.ident),
             );
@@ -829,7 +829,7 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
             return;
         }
         let sugg = trait_should_be_self.iter().map(|span| (*span, "Self".to_string())).collect();
-        tcx.sess
+        tcx.dcx()
             .struct_span_err(
                 trait_should_be_self,
                 "associated item referring to unboxed trait object for its own trait",
@@ -883,15 +883,15 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
             } else {
                 let mut diag = match ty.kind() {
                     ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Error(_) => return Ok(()),
-                    ty::FnPtr(_) => tcx.sess.struct_span_err(
+                    ty::FnPtr(_) => tcx.dcx().struct_span_err(
                         hir_ty.span,
                         "using function pointers as const generic parameters is forbidden",
                     ),
-                    ty::RawPtr(_) => tcx.sess.struct_span_err(
+                    ty::RawPtr(_) => tcx.dcx().struct_span_err(
                         hir_ty.span,
                         "using raw pointers as const generic parameters is forbidden",
                     ),
-                    _ => tcx.sess.struct_span_err(
+                    _ => tcx.dcx().struct_span_err(
                         hir_ty.span,
                         format!("`{}` is forbidden as the type of a const generic parameter", ty),
                     ),
@@ -1032,7 +1032,7 @@ fn check_type_defn<'tcx>(
                     let ty = tcx.type_of(variant.tail().did).instantiate_identity();
                     let ty = tcx.erase_regions(ty);
                     if ty.has_infer() {
-                        tcx.sess
+                        tcx.dcx()
                             .span_delayed_bug(item.span, format!("inference variables in {ty:?}"));
                         // Just treat unresolved type expression as if it needs drop.
                         true
@@ -1114,7 +1114,7 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) -> Result<(), ErrorGuarant
     {
         for associated_def_id in &*tcx.associated_item_def_ids(def_id) {
             struct_span_err!(
-                tcx.sess,
+                tcx.dcx(),
                 tcx.def_span(*associated_def_id),
                 E0714,
                 "marker traits cannot have associated items",
@@ -1532,14 +1532,14 @@ fn check_fn_or_method<'tcx>(
                 tcx.require_lang_item(hir::LangItem::Sized, Some(span)),
             );
         } else {
-            tcx.sess.span_err(
+            tcx.dcx().span_err(
                 hir_decl.inputs.last().map_or(span, |input| input.span),
                 "functions with the \"rust-call\" ABI must take a single non-self tuple argument",
             );
         }
         // No more inputs other than the `self` type and the tuple type
         if inputs.next().is_some() {
-            tcx.sess.span_err(
+            tcx.dcx().span_err(
                 hir_decl.inputs.last().map_or(span, |input| input.span),
                 "functions with the \"rust-call\" ABI must take a single non-self tuple argument",
             );
@@ -1607,7 +1607,7 @@ fn check_method_receiver<'tcx>(
 }
 
 fn e0307(tcx: TyCtxt<'_>, span: Span, receiver_ty: Ty<'_>) -> ErrorGuaranteed {
-    struct_span_err!(tcx.sess.dcx(), span, E0307, "invalid `self` parameter type: {receiver_ty}")
+    struct_span_err!(tcx.dcx(), span, E0307, "invalid `self` parameter type: {receiver_ty}")
         .note("type of `self` must be `Self` or a type that dereferences to it")
         .help(HELP_FOR_SELF_TYPE)
         .emit()
@@ -1807,7 +1807,7 @@ fn check_variances_for_type_defn<'tcx>(
             //
             // if they aren't in the same order, then the user has written invalid code, and already
             // got an error about it (or I'm wrong about this)
-            tcx.sess.span_delayed_bug(
+            tcx.dcx().span_delayed_bug(
                 hir_param.span,
                 "hir generics and ty generics in different order",
             );
@@ -1913,7 +1913,8 @@ fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), Error
 }
 
 fn error_392(tcx: TyCtxt<'_>, span: Span, param_name: Symbol) -> DiagnosticBuilder<'_> {
-    let mut err = struct_span_err!(tcx.sess, span, E0392, "parameter `{param_name}` is never used");
+    let mut err =
+        struct_span_err!(tcx.dcx(), span, E0392, "parameter `{param_name}` is never used");
     err.span_label(span, "unused parameter");
     err
 }