Skip to content

Commit fa2e674

Browse files
committed
de-duplicate LayoutError formatting
1 parent fe581d9 commit fa2e674

9 files changed

Lines changed: 9 additions & 70 deletions

File tree

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
539539
| LayoutError::InvalidSimd { .. }
540540
| LayoutError::ReferencesError(_) = err
541541
{
542-
self.tcx.dcx().emit_fatal(respan(span, err.into_diagnostic()))
542+
self.tcx.dcx().span_fatal(span, err.to_string())
543543
} else {
544544
self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
545545
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
11361136
| LayoutError::ReferencesError(_)
11371137
| LayoutError::InvalidSimd { .. } = err
11381138
{
1139-
self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
1139+
self.tcx.dcx().span_fatal(span, err.to_string())
11401140
} else {
11411141
self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
11421142
}

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn push_debuginfo_type_name<'tcx>(
9595
// Computing the layout can still fail here, e.g. if the target architecture
9696
// cannot represent the type. See
9797
// https://github.com/rust-lang/rust/issues/94961.
98-
tcx.dcx().emit_fatal(e.into_diagnostic());
98+
tcx.dcx().fatal(e.to_string());
9999
}
100100
}
101101
} else {

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_middle::ty::{
2424
TypeVisitable, TypeVisitableExt, fold_regions,
2525
};
2626
use rustc_session::lint::builtin::UNINHABITED_STATIC;
27-
use rustc_span::source_map::Spanned;
2827
use rustc_target::spec::{AbiMap, AbiMapping};
2928
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
3029
use rustc_trait_selection::traits;
@@ -216,7 +215,7 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
216215
// SIMD types with invalid layout (e.g., zero-length) should emit an error
217216
Err(e @ LayoutError::InvalidSimd { .. }) => {
218217
let ty_span = tcx.ty_span(def_id);
219-
tcx.dcx().emit_err(Spanned { span: ty_span, node: e.into_diagnostic() });
218+
tcx.dcx().span_err(ty_span, e.to_string());
220219
return;
221220
}
222221
// Generic statics are rejected, but we still reach this case.

compiler/rustc_middle/src/error.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,6 @@ pub(super) struct ConstNotUsedTraitAlias {
113113
pub span: Span,
114114
}
115115

116-
#[derive(Diagnostic)]
117-
pub enum LayoutError<'tcx> {
118-
#[diag("the type `{$ty}` has an unknown layout")]
119-
Unknown { ty: Ty<'tcx> },
120-
121-
#[diag("the type `{$ty}` does not have a fixed layout")]
122-
TooGeneric { ty: Ty<'tcx> },
123-
124-
#[diag("values of the type `{$ty}` are too big for the target architecture")]
125-
Overflow { ty: Ty<'tcx> },
126-
127-
#[diag("the SIMD type `{$ty}` has more elements than the limit {$max_lanes}")]
128-
SimdTooManyLanes { ty: Ty<'tcx>, max_lanes: u64 },
129-
130-
#[diag("the SIMD type `{$ty}` has zero elements")]
131-
SimdZeroLength { ty: Ty<'tcx> },
132-
133-
#[diag("unable to determine layout for `{$ty}` because `{$failure_ty}` cannot be normalized")]
134-
NormalizationFailure { ty: Ty<'tcx>, failure_ty: String },
135-
136-
#[diag("a cycle occurred during layout computation")]
137-
Cycle,
138-
139-
#[diag("the type has an unknown layout")]
140-
ReferencesError,
141-
}
142-
143116
#[derive(Diagnostic)]
144117
#[diag("erroneous constant encountered")]
145118
pub(crate) struct ErroneousConstant {

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -264,30 +264,6 @@ pub enum LayoutError<'tcx> {
264264
Cycle(ErrorGuaranteed),
265265
}
266266

267-
impl<'tcx> LayoutError<'tcx> {
268-
pub fn into_diagnostic(self) -> crate::error::LayoutError<'tcx> {
269-
use LayoutError::*;
270-
271-
use crate::error::LayoutError as E;
272-
match self {
273-
Unknown(ty) => E::Unknown { ty },
274-
SizeOverflow(ty) => E::Overflow { ty },
275-
InvalidSimd { ty, kind: SimdLayoutError::TooManyLanes(max_lanes) } => {
276-
E::SimdTooManyLanes { ty, max_lanes }
277-
}
278-
InvalidSimd { ty, kind: SimdLayoutError::ZeroLength } => E::SimdZeroLength { ty },
279-
TooGeneric(ty) => E::TooGeneric { ty },
280-
NormalizationFailure(ty, e) => {
281-
E::NormalizationFailure { ty, failure_ty: e.get_type_for_failure() }
282-
}
283-
Cycle(_) => E::Cycle,
284-
ReferencesError(_) => E::ReferencesError,
285-
}
286-
}
287-
}
288-
289-
// FIXME: Once the other errors that embed this error have been converted to translatable
290-
// diagnostics, this Display impl should be removed.
291267
impl<'tcx> fmt::Display for LayoutError<'tcx> {
292268
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
293269
match *self {
@@ -1309,7 +1285,7 @@ pub enum FnAbiError<'tcx> {
13091285
impl<'a, 'b, G: EmissionGuarantee> Diagnostic<'a, G> for FnAbiError<'b> {
13101286
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
13111287
match self {
1312-
Self::Layout(e) => e.into_diagnostic().into_diag(dcx, level),
1288+
Self::Layout(e) => Diag::new(dcx, level, e.to_string()),
13131289
}
13141290
}
13151291
}

compiler/rustc_middle/src/ty/vtable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
101101

102102
let layout = match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) {
103103
Ok(layout) => layout,
104-
Err(e) => tcx.dcx().emit_fatal(e.into_diagnostic()),
104+
Err(e) => tcx.dcx().fatal(e.to_string()),
105105
};
106106
assert!(layout.is_sized(), "can't create a vtable for an unsized type");
107107
let size = layout.size.bytes();

compiler/rustc_passes/src/abi_test.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_middle::span_bug;
66
use rustc_middle::ty::layout::{FnAbiError, LayoutError};
77
use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt};
88
use rustc_span::Span;
9-
use rustc_span::source_map::Spanned;
109
use rustc_target::callconv::FnAbi;
1110

1211
use super::layout_test::ensure_wf;
@@ -45,10 +44,7 @@ fn unwrap_fn_abi<'tcx>(
4544
match abi {
4645
Ok(abi) => abi,
4746
Err(FnAbiError::Layout(layout_error)) => {
48-
tcx.dcx().emit_fatal(Spanned {
49-
node: layout_error.into_diagnostic(),
50-
span: tcx.def_span(item_def_id),
51-
});
47+
tcx.dcx().span_fatal(tcx.def_span(item_def_id), layout_error.to_string());
5248
}
5349
}
5450
}
@@ -66,11 +62,7 @@ fn dump_abi_of_fn_item(
6662
Ok(None) => {
6763
// Not sure what to do here, but `LayoutError::Unknown` seems reasonable?
6864
let ty = tcx.type_of(item_def_id).instantiate_identity();
69-
tcx.dcx().emit_fatal(Spanned {
70-
node: LayoutError::Unknown(ty).into_diagnostic(),
71-
72-
span: tcx.def_span(item_def_id),
73-
});
65+
tcx.dcx().span_fatal(tcx.def_span(item_def_id), LayoutError::Unknown(ty).to_string());
7466
}
7567
Err(_guaranteed) => return,
7668
};

compiler/rustc_passes/src/layout_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_middle::span_bug;
77
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutError, LayoutOfHelpers};
88
use rustc_middle::ty::{self, Ty, TyCtxt};
99
use rustc_span::Span;
10-
use rustc_span::source_map::Spanned;
1110
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
1211
use rustc_trait_selection::infer::TyCtxtInferExt;
1312
use rustc_trait_selection::traits;
@@ -116,7 +115,7 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attrs: &[RustcLayout
116115
}
117116

118117
Err(layout_error) => {
119-
tcx.dcx().emit_err(Spanned { node: layout_error.into_diagnostic(), span });
118+
tcx.dcx().span_err(span, layout_error.to_string());
120119
}
121120
}
122121
}

0 commit comments

Comments
 (0)