Skip to content

Commit e55400c

Browse files
authoredMar 17, 2022
Rollup merge of #94997 - michaelwoerister:fix-enum-type-name-layout-error, r=wesleywiser
debuginfo: Fix ICE when generating name for type that produces a layout error. Fixes #94961.
2 parents 0c73b25 + 243e2a6 commit e55400c

5 files changed

+67
-2
lines changed
 

Diff for: ‎compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,30 @@ fn push_debuginfo_type_name<'tcx>(
7474
ty::Float(float_ty) => output.push_str(float_ty.name_str()),
7575
ty::Foreign(def_id) => push_item_name(tcx, def_id, qualified, output),
7676
ty::Adt(def, substs) => {
77-
let ty_and_layout = tcx.layout_of(ParamEnv::reveal_all().and(t)).expect("layout error");
77+
// `layout_for_cpp_like_fallback` will be `Some` if we want to use the fallback encoding.
78+
let layout_for_cpp_like_fallback = if cpp_like_debuginfo && def.is_enum() {
79+
match tcx.layout_of(ParamEnv::reveal_all().and(t)) {
80+
Ok(layout) => {
81+
if !wants_c_like_enum_debuginfo(layout) {
82+
Some(layout)
83+
} else {
84+
// This is a C-like enum so we don't want to use the fallback encoding
85+
// for the name.
86+
None
87+
}
88+
}
89+
Err(e) => {
90+
// Computing the layout can still fail here, e.g. if the target architecture
91+
// cannot represent the type. See https://github.com/rust-lang/rust/issues/94961.
92+
tcx.sess.fatal(&format!("{}", e));
93+
}
94+
}
95+
} else {
96+
// We are not emitting cpp-like debuginfo or this isn't even an enum.
97+
None
98+
};
7899

79-
if def.is_enum() && cpp_like_debuginfo && !wants_c_like_enum_debuginfo(ty_and_layout) {
100+
if let Some(ty_and_layout) = layout_for_cpp_like_fallback {
80101
msvc_enum_fallback(
81102
tcx,
82103
ty_and_layout,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Make sure the compiler does not ICE when trying to generate the debuginfo name of a type that
2+
// causes a layout error. See https://github.com/rust-lang/rust/issues/94961.
3+
4+
// compile-flags:-C debuginfo=2
5+
// build-fail
6+
// error-pattern: too big for the current architecture
7+
// normalize-stderr-64bit "18446744073709551615" -> "SIZE"
8+
// normalize-stderr-32bit "4294967295" -> "SIZE"
9+
10+
#![crate_type = "rlib"]
11+
12+
pub struct Foo<T>([T; usize::MAX]);
13+
14+
pub fn foo() -> usize {
15+
std::mem::size_of::<Foo<u8>>()
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: values of the type `[u8; SIZE]` are too big for the current architecture
2+
3+
error: aborting due to previous error
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Make sure the compiler does not ICE when trying to generate the debuginfo name of a type that
2+
// causes a layout error.
3+
// This version of the test already ICE'd before the commit that introduce the ICE described in
4+
// https://github.com/rust-lang/rust/issues/94961.
5+
6+
// compile-flags:-C debuginfo=2
7+
// build-fail
8+
// error-pattern: too big for the current architecture
9+
// normalize-stderr-64bit "18446744073709551615" -> "SIZE"
10+
// normalize-stderr-32bit "4294967295" -> "SIZE"
11+
12+
#![crate_type = "rlib"]
13+
14+
pub enum Foo<T> {
15+
Bar([T; usize::MAX]),
16+
}
17+
18+
pub fn foo() -> usize {
19+
std::mem::size_of::<Foo<u8>>()
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: values of the type `[u8; SIZE]` are too big for the current architecture
2+
3+
error: aborting due to previous error
4+

0 commit comments

Comments
 (0)
Please sign in to comment.