Rollup merge of #152120 - meithecatte:push-ltvwvkqrytno, r=petrochenkov

Don't ICE on layout error in vtable computation

Fixes rust-lang/rust#152030.

Note: I'm including a more general testcase that doesn't use the feature in the original report, but only reproduces with debuginfo disabled. Does it make sense to also include the original testcase?
This commit is contained in:
Jonathan Brouwer 2026-02-14 18:55:36 +01:00 committed by GitHub
commit 2c4656ac66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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