Don't ICE on layout error in vtable computation

This commit is contained in:
Maja Kądziołka 2026-02-10 15:10:04 +01:00
parent bb8b30a5fc
commit e098327271
6 changed files with 39 additions and 3 deletions

View file

@ -99,9 +99,10 @@ pub(super) fn vtable_allocation_provider<'tcx>(
// This confirms that the layout computation for &dyn Trait has an accurate sizing.
assert!(vtable_entries.len() >= vtable_min_entries(tcx, poly_trait_ref));
let layout = tcx
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty))
.expect("failed to build vtable representation");
let layout = match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) {
Ok(layout) => layout,
Err(e) => tcx.dcx().emit_fatal(e.into_diagnostic()),
};
assert!(layout.is_sized(), "can't create a vtable for an unsized type");
let size = layout.size.bytes();
let align = layout.align.bytes();

View file

@ -0,0 +1,4 @@
error: values of the type `[u8; usize::MAX]` are too big for the target architecture
error: aborting due to 1 previous error

View file

@ -0,0 +1,4 @@
error: values of the type `[u8; usize::MAX]` are too big for the target architecture
error: aborting due to 1 previous error

View file

@ -0,0 +1,15 @@
// At the time of writing, vtable.rs would ICE only with debuginfo disabled, while this testcase,
// originally reported as #152030, would ICE even with debuginfo enabled.
//@ revisions: no-debuginfo full-debuginfo
//@ compile-flags: --crate-type=lib --emit=mir
//@[no-debuginfo] compile-flags: -C debuginfo=0
//@[full-debuginfo] compile-flags: -C debuginfo=2
#![feature(try_as_dyn)]
trait Trait {}
impl<T> Trait for T {}
//~? ERROR: values of the type `[u8; usize::MAX]` are too big for the target architecture
pub fn foo(x: &[u8; usize::MAX]) {
let _ = std::any::try_as_dyn::<[u8; usize::MAX], dyn Trait>(x);
}

View file

@ -0,0 +1,8 @@
//@ compile-flags: --crate-type=lib --emit=mir -C debuginfo=0
pub trait Trait {}
impl<T> Trait for T {}
//~? ERROR: values of the type `[u8; usize::MAX]` are too big for the target architecture
pub fn foo(x: &[u8; usize::MAX]) -> &dyn Trait {
x as &dyn Trait
}

View file

@ -0,0 +1,4 @@
error: values of the type `[u8; usize::MAX]` are too big for the target architecture
error: aborting due to 1 previous error