Skip to content

Commit 4e2469c

Browse files
committed
Fix ICE when ABI computation fails for unreachable/no-codegen instances
Replace delayed_bug with direct error emission in check_instance_abi, since codegen may never process the instance (dead code or -Zno-codegen).
1 parent 31ee3d9 commit 4e2469c

13 files changed

Lines changed: 74 additions & 23 deletions

compiler/rustc_monomorphize/src/mono_checks/abi_check.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_hir::{CRATE_HIR_ID, HirId};
55
use rustc_middle::mir::{self, Location, traversal};
66
use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt};
77
use rustc_span::def_id::DefId;
8+
use rustc_span::source_map::Spanned;
89
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
910
use rustc_target::callconv::{FnAbi, PassMode};
1011

@@ -161,11 +162,10 @@ fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
161162
{
162163
Ok(abi) => abi,
163164
Err(err) => {
164-
// Emit the error directly rather than using `delayed_bug`, because codegen
165-
// may never process this instance (e.g., unreachable code or `-Zno-codegen`),
166-
// which would leave the error unreported and trigger an ICE.
167-
let ty::layout::FnAbiError::Layout(layout_err) = err;
168-
tcx.dcx().emit_err((*layout_err).into_diagnostic());
165+
// Emit directly: codegen may never see this instance (dead code, `-Zno-codegen`).
166+
let ty::layout::FnAbiError::Layout(layout_err) = *err;
167+
let span = tcx.def_span(instance.def_id());
168+
tcx.dcx().emit_err(Spanned { node: layout_err.into_diagnostic(), span });
169169
return;
170170
}
171171
};

tests/crashes/138008.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/ui/layout/post-mono-layout-cycle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Wrapper<T: Trait> {
1313
_x: <T as Trait>::Assoc,
1414
}
1515

16-
fn abi<T: Trait>(_: Option<Wrapper<T>>) {}
16+
fn abi<T: Trait>(_: Option<Wrapper<T>>) {} //~ ERROR a cycle occurred during layout computation
1717

1818
fn indirect<T: Trait>() {
1919
abi::<T>(None);

tests/ui/layout/post-mono-layout-cycle.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ error[E0391]: cycle detected when computing layout of `Wrapper<()>`
55
= note: cycle used when computing layout of `core::option::Option<Wrapper<()>>`
66
= 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
77

8+
error: a cycle occurred during layout computation
9+
--> $DIR/post-mono-layout-cycle.rs:16:1
10+
|
11+
LL | fn abi<T: Trait>(_: Option<Wrapper<T>>) {}
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
814
note: the above error was encountered while instantiating `fn abi::<()>`
915
--> $DIR/post-mono-layout-cycle.rs:19:5
1016
|
1117
LL | abi::<T>(None);
1218
| ^^^^^^^^^^^^^^
1319

14-
error: aborting due to 1 previous error
20+
error: aborting due to 2 previous errors
1521

1622
For more information about this error, try `rustc --explain E0391`.

tests/ui/limits/issue-17913.32bit.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
error: values of the type `[&usize; usize::MAX]` are too big for the target architecture
2+
13
error[E0080]: values of the type `[&usize; usize::MAX]` are too big for the target architecture
24
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
35
|
@@ -14,6 +16,6 @@ note: the above error was encountered while instantiating `fn Box::<[&usize; usi
1416
LL | let a: Box<_> = Box::new([&n; SIZE]);
1517
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1618

17-
error: aborting due to 2 previous errors
19+
error: aborting due to 3 previous errors
1820

1921
For more information about this error, try `rustc --explain E0080`.

tests/ui/limits/issue-17913.64bit.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
error: values of the type `[&usize; usize::MAX]` are too big for the target architecture
2+
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
3+
14
error[E0080]: values of the type `[&usize; usize::MAX]` are too big for the target architecture
25
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
36
|
@@ -14,6 +17,6 @@ note: the above error was encountered while instantiating `fn Box::<[&usize; usi
1417
LL | let a: Box<_> = Box::new([&n; SIZE]);
1518
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1619

17-
error: aborting due to 2 previous errors
20+
error: aborting due to 3 previous errors
1821

1922
For more information about this error, try `rustc --explain E0080`.

tests/ui/limits/issue-17913.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ fn main() {
1919

2020
//~? ERROR are too big for the target architecture
2121
//~? ERROR are too big for the target architecture
22+
//~? ERROR are too big for the target architecture
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test for #149156.
2+
//@ build-fail
3+
//@ compile-flags: -Zno-codegen
4+
//@ only-x86_64
5+
6+
#![crate_type = "lib"]
7+
#![allow(private_interfaces)]
8+
9+
struct Struct([u8; 0xffff_ffff_ffff_ffff]);
10+
11+
pub fn function(value: Struct) -> u8 { //~ ERROR are too big for the target architecture
12+
value.0[0]
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: values of the type `[u8; usize::MAX]` are too big for the target architecture
2+
--> $DIR/monomorphize-oversized-no-codegen.rs:11:1
3+
|
4+
LL | pub fn function(value: Struct) -> u8 {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
// Regression test for #152204: ICE when oversized SIMD type appears in unreachable code
2-
//
3-
// Previously, `check_instance_abi` used `delayed_bug` for ABI computation
4-
// failures, assuming codegen would report the error. But unreachable code
5-
// is monomorphized without reaching codegen, leaving the error unreported.
1+
// Regression test for #152204.
2+
//@ build-fail
3+
//@ only-x86_64
64

75
#![feature(portable_simd)]
86

97
fn main() {
108
if false {
119
let _ = core::simd::Simd::<u8, 256>::splat(0);
12-
//~^ ERROR the SIMD type `Simd<u8, 256>` has more elements than the limit 64
1310
}
1411
}
12+
13+
//~? ERROR the SIMD type `Simd<u8, 256>` has more elements than the limit 64

0 commit comments

Comments
 (0)