Unify all validity check intrinsics

Also merges the inhabitedness check into the query to further unify the
code paths.
This commit is contained in:
Nilstrieb 2023-02-26 21:50:19 +00:00
parent 32317b5a5b
commit 1b57cb6762

View File

@ -22,7 +22,7 @@ pub(crate) use cpuid::codegen_cpuid_call;
pub(crate) use llvm::codegen_llvm_intrinsic_call; pub(crate) use llvm::codegen_llvm_intrinsic_call;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_middle::ty::layout::{HasParamEnv, InitKind}; use rustc_middle::ty::layout::{HasParamEnv, ValidityRequirement};
use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::subst::SubstsRef;
use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::symbol::{kw, sym, Symbol};
@ -628,57 +628,39 @@ fn codegen_regular_intrinsic_call<'tcx>(
intrinsic_args!(fx, args => (); intrinsic); intrinsic_args!(fx, args => (); intrinsic);
let ty = substs.type_at(0); let ty = substs.type_at(0);
let layout = fx.layout_of(ty);
if layout.abi.is_uninhabited() {
with_no_trimmed_paths!({
crate::base::codegen_panic_nounwind(
fx,
&format!("attempted to instantiate uninhabited type `{}`", layout.ty),
source_info,
)
});
return;
}
if intrinsic == sym::assert_zero_valid let requirement = ValidityRequirement::from_intrinsic(intrinsic);
&& !fx
.tcx
.check_validity_of_init((InitKind::Zero, fx.param_env().and(ty)))
.expect("expected to have layout during codegen")
{
with_no_trimmed_paths!({
crate::base::codegen_panic_nounwind(
fx,
&format!(
"attempted to zero-initialize type `{}`, which is invalid",
layout.ty
),
source_info,
);
});
return;
}
if intrinsic == sym::assert_mem_uninitialized_valid if let Some(requirement) = requirement {
&& !fx let do_panic = !fx
.tcx .tcx
.check_validity_of_init(( .check_validity_requirement((requirement, fx.param_env().and(ty)))
InitKind::UninitMitigated0x01Fill, .expect("expect to have layout during codegen");
fx.param_env().and(ty),
)) if do_panic {
.expect("expected to have layout during codegen") let layout = fx.layout_of(ty);
{
with_no_trimmed_paths!({ with_no_trimmed_paths!({
crate::base::codegen_panic_nounwind( crate::base::codegen_panic_nounwind(
fx, fx,
&format!( &if layout.abi.is_uninhabited() {
"attempted to leave type `{}` uninitialized, which is invalid", format!("attempted to instantiate uninhabited type `{}`", layout.ty)
layout.ty } else if requirement == ValidityRequirement::Zero {
), format!(
source_info, "attempted to zero-initialize type `{}`, which is invalid",
) layout.ty
}); )
return; } else {
format!(
"attempted to leave type `{}` uninitialized, which is invalid",
layout.ty
)
},
source_info,
)
});
return;
}
} }
} }