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