Skip to content

Commit 56d7d93

Browse files
authored
Rollup merge of #111580 - atsuzaki:layout-ice, r=oli-obk
Don't ICE on layout computation failure Fixes #111176 regression. r? `@oli-obk`
2 parents bb90f81 + 56b7673 commit 56d7d93

File tree

8 files changed

+68
-4
lines changed

8 files changed

+68
-4
lines changed

compiler/rustc_codegen_cranelift/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
480480
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
481481
self.0.sess.span_fatal(span, err.to_string())
482482
} else {
483-
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
483+
self.0.sess.span_fatal(span, format!("failed to get layout for `{}`: {}", ty, err))
484484
}
485485
}
486486
}

compiler/rustc_codegen_gcc/src/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_codegen_ssa::traits::{
77
BaseTypeMethods,
88
MiscMethods,
99
};
10+
use rustc_codegen_ssa::errors as ssa_errors;
1011
use rustc_data_structures::base_n;
1112
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1213
use rustc_middle::span_bug;
@@ -479,7 +480,7 @@ impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
479480
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
480481
self.sess().emit_fatal(respan(span, err.into_diagnostic()))
481482
} else {
482-
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
483+
self.tcx.sess.emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
483484
}
484485
}
485486
}

compiler/rustc_codegen_llvm/src/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::value::Value;
1010

1111
use cstr::cstr;
1212
use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
13+
use rustc_codegen_ssa::errors as ssa_errors;
1314
use rustc_codegen_ssa::traits::*;
1415
use rustc_data_structures::base_n;
1516
use rustc_data_structures::fx::FxHashMap;
@@ -1000,7 +1001,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
10001001
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
10011002
self.sess().emit_fatal(Spanned { span, node: err.into_diagnostic() })
10021003
} else {
1003-
span_bug!(span, "failed to get layout for `{ty}`: {err:?}")
1004+
self.tcx.sess.emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
10041005
}
10051006
}
10061007
}

compiler/rustc_codegen_ssa/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ codegen_ssa_extract_bundled_libs_parse_archive = failed to parse archive '{$rlib
3535
codegen_ssa_extract_bundled_libs_read_entry = failed to read entry '{$rlib}': {$error}
3636
codegen_ssa_extract_bundled_libs_write_file = failed to write file '{$rlib}': {$error}
3737
38+
codegen_ssa_failed_to_get_layout = failed to get layout for {$ty}: {$err}
39+
3840
codegen_ssa_failed_to_write = failed to write {$path}: {$error}
3941
4042
codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced

compiler/rustc_codegen_ssa/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::{
77
IntoDiagnosticArg,
88
};
99
use rustc_macros::Diagnostic;
10+
use rustc_middle::ty::layout::LayoutError;
1011
use rustc_middle::ty::Ty;
1112
use rustc_span::{Span, Symbol};
1213
use rustc_type_ir::FloatTy;
@@ -1030,6 +1031,15 @@ pub struct TargetFeatureSafeTrait {
10301031
pub def: Span,
10311032
}
10321033

1034+
#[derive(Diagnostic)]
1035+
#[diag(codegen_ssa_failed_to_get_layout)]
1036+
pub struct FailedToGetLayout<'tcx> {
1037+
#[primary_span]
1038+
pub span: Span,
1039+
pub ty: Ty<'tcx>,
1040+
pub err: LayoutError<'tcx>,
1041+
}
1042+
10331043
#[derive(Diagnostic)]
10341044
#[diag(codegen_ssa_error_creating_remark_dir)]
10351045
pub struct ErrorCreatingRemarkDir {

compiler/rustc_middle/src/ty/layout.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::query::TyCtxtAt;
44
use crate::ty::normalize_erasing_regions::NormalizationError;
55
use crate::ty::{self, ConstKind, ReprOptions, Ty, TyCtxt, TypeVisitableExt};
66
use rustc_error_messages::DiagnosticMessage;
7-
use rustc_errors::{DiagnosticBuilder, Handler, IntoDiagnostic};
7+
use rustc_errors::{
8+
DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg,
9+
};
810
use rustc_hir as hir;
911
use rustc_hir::def_id::DefId;
1012
use rustc_index::IndexVec;
@@ -265,6 +267,12 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> {
265267
}
266268
}
267269

270+
impl<'tcx> IntoDiagnosticArg for LayoutError<'tcx> {
271+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
272+
self.to_string().into_diagnostic_arg()
273+
}
274+
}
275+
268276
#[derive(Clone, Copy)]
269277
pub struct LayoutCx<'tcx, C> {
270278
pub tcx: C,

tests/ui/layout/layout-cycle.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// build-fail
2+
//~^ ERROR: a cycle occurred during layout computation
3+
//~| ERROR: cycle detected when computing layout of
4+
5+
// Issue #111176 -- ensure that we do not emit ICE on layout cycles
6+
7+
use std::mem;
8+
9+
pub struct S<T: Tr> {
10+
pub f: <T as Tr>::I,
11+
}
12+
13+
pub trait Tr {
14+
type I: Tr;
15+
}
16+
17+
impl<T: Tr> Tr for S<T> {
18+
type I = S<S<T>>;
19+
}
20+
21+
impl Tr for () {
22+
type I = ();
23+
}
24+
25+
fn foo<T: Tr>() -> usize {
26+
mem::size_of::<S<T>>()
27+
}
28+
29+
fn main() {
30+
println!("{}", foo::<S<()>>());
31+
}

tests/ui/layout/layout-cycle.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0391]: cycle detected when computing layout of `S<S<()>>`
2+
|
3+
= note: ...which requires computing layout of `<S<()> as Tr>::I`...
4+
= note: ...which again requires computing layout of `S<S<()>>`, completing the cycle
5+
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
6+
7+
error: failed to get layout for S<S<()>>: a cycle occurred during layout computation
8+
9+
error: aborting due to 2 previous errors
10+
11+
For more information about this error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)