Skip to content

move expensive layout sanity check to debug assertions #141039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions compiler/rustc_ty_utils/src/layout/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ use rustc_middle::ty::layout::{HasTyCtxt, LayoutCx, TyAndLayout};
pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) {
let tcx = cx.tcx();

// Type-level uninhabitedness should always imply ABI uninhabitedness.
if layout.ty.is_privately_uninhabited(tcx, cx.typing_env) {
assert!(
layout.is_uninhabited(),
"{:?} is type-level uninhabited but not ABI-uninhabited?",
layout.ty
);
}

if layout.size.bytes() % layout.align.abi.bytes() != 0 {
bug!("size is not a multiple of align, in the following layout:\n{layout:#?}");
}
Expand All @@ -29,6 +20,19 @@ pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayou
return;
}

// Type-level uninhabitedness should always imply ABI uninhabitedness. This can be expensive on
// big non-exhaustive types, and is [hard to
// fix](https://github.com/rust-lang/rust/issues/141006#issuecomment-2883415000) in general.
// Only doing this sanity check when debug assertions are turned on avoids the issue for the
// very specific case of #140944.
if layout.ty.is_privately_uninhabited(tcx, cx.typing_env) {
assert!(
layout.is_uninhabited(),
"{:?} is type-level uninhabited but not ABI-uninhabited?",
layout.ty
);
}

/// Yields non-ZST fields of the type
fn non_zst_fields<'tcx, 'a>(
cx: &'a LayoutCx<'tcx>,
Expand Down
Loading