Skip to content

Commit

Permalink
Unify all validity check intrinsics
Browse files Browse the repository at this point in the history
Also merges the inhabitedness check into the query to further unify the
code paths.
  • Loading branch information
Nilstrieb committed Feb 27, 2023
1 parent 32317b5 commit 1b57cb6
Showing 1 changed file with 31 additions and 49 deletions.
80 changes: 31 additions & 49 deletions src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) use cpuid::codegen_cpuid_call;
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};
Expand Down Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit 1b57cb6

Please sign in to comment.