Skip to content

Commit 84a3c4b

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 8c5605e commit 84a3c4b

13 files changed

Lines changed: 93 additions & 18 deletions

compiler/rustc_monomorphize/src/mono_checks/abi_check.rs

Lines changed: 11 additions & 6 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

@@ -157,12 +158,16 @@ fn do_check_unsized_params<'tcx>(
157158
/// - the signature requires target features that are not enabled
158159
fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
159160
let typing_env = ty::TypingEnv::fully_monomorphized();
160-
let Ok(abi) = tcx.fn_abi_of_instance(typing_env.as_query_input((instance, ty::List::empty())))
161-
else {
162-
// An error will be reported during codegen if we cannot determine the ABI of this
163-
// function.
164-
tcx.dcx().delayed_bug("ABI computation failure should lead to compilation failure");
165-
return;
161+
let abi = match tcx.fn_abi_of_instance(typing_env.as_query_input((instance, ty::List::empty())))
162+
{
163+
Ok(abi) => abi,
164+
Err(err) => {
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 });
169+
return;
170+
}
166171
};
167172
// Unlike the call-site check, we do also check "Rust" ABI functions here.
168173
// This should never trigger, *except* if we start making use of vector registers

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: 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.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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test for #152204.
2+
//@ build-fail
3+
//@ only-x86_64
4+
5+
#![feature(portable_simd)]
6+
7+
fn main() {
8+
if false {
9+
let _ = core::simd::Simd::<u8, 256>::splat(0);
10+
}
11+
}
12+
13+
//~? ERROR the SIMD type `Simd<u8, 256>` has more elements than the limit 64

0 commit comments

Comments
 (0)