Skip to content

Commit 5518779

Browse files
committed
refactor ub_checks and contract_checks to share logic
1 parent 61efd19 commit 5518779

File tree

23 files changed

+75
-92
lines changed

23 files changed

+75
-92
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,8 +1057,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10571057
ConstraintCategory::SizedBound,
10581058
);
10591059
}
1060-
&Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
1061-
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
1060+
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => {}
10621061

10631062
Rvalue::ShallowInitBox(_operand, ty) => {
10641063
let trait_ref =

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -855,17 +855,8 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
855855
fields.iter(),
856856
)
857857
.bytes(),
858-
NullOp::UbChecks => {
859-
let val = fx.tcx.sess.ub_checks();
860-
let val = CValue::by_val(
861-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
862-
fx.layout_of(fx.tcx.types.bool),
863-
);
864-
lval.write_cvalue(fx, val);
865-
return;
866-
}
867-
NullOp::ContractChecks => {
868-
let val = fx.tcx.sess.contract_checks();
858+
NullOp::RuntimeChecks(kind) => {
859+
let val = kind.value(fx.tcx.sess);
869860
let val = CValue::by_val(
870861
fx.bcx.ins().iconst(types::I8, i64::from(val)),
871862
fx.layout_of(fx.tcx.types.bool),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
631631
.bytes();
632632
bx.cx().const_usize(val)
633633
}
634-
mir::NullOp::UbChecks => {
635-
let val = bx.tcx().sess.ub_checks();
636-
bx.cx().const_bool(val)
637-
}
638-
mir::NullOp::ContractChecks => {
639-
let val = bx.tcx().sess.contract_checks();
634+
mir::NullOp::RuntimeChecks(kind) => {
635+
let val = kind.value(bx.tcx().sess);
640636
bx.cx().const_bool(val)
641637
}
642638
};

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
646646
Rvalue::Cast(_, _, _) => {}
647647

648648
Rvalue::NullaryOp(
649-
NullOp::SizeOf
650-
| NullOp::AlignOf
651-
| NullOp::OffsetOf(_)
652-
| NullOp::UbChecks
653-
| NullOp::ContractChecks,
649+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::RuntimeChecks(_),
654650
_,
655651
) => {}
656652
Rvalue::ShallowInitBox(_, _) => {}

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ pub trait Machine<'tcx>: Sized {
298298
interp_ok(())
299299
}
300300

301-
/// Determines the result of a `NullaryOp::UbChecks` invocation.
302-
fn ub_checks(_ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool>;
303-
304-
/// Determines the result of a `NullaryOp::ContractChecks` invocation.
305-
fn contract_checks(_ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool>;
301+
/// Determines the result of a `NullaryOp::RuntimeChecks` invocation.
302+
fn runtime_checks(
303+
_ecx: &InterpCx<'tcx, Self>,
304+
r: mir::RuntimeChecks,
305+
) -> InterpResult<'tcx, bool>;
306306

307307
/// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
308308
/// You can use this to detect long or endlessly running programs.
@@ -681,14 +681,10 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
681681
}
682682

683683
#[inline(always)]
684-
fn ub_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
685-
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
686-
// unsound differences in evaluating the same constant at different instantiation sites.
687-
interp_ok(true)
688-
}
689-
690-
#[inline(always)]
691-
fn contract_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
684+
fn runtime_checks(
685+
_ecx: &InterpCx<$tcx, Self>,
686+
_r: mir::RuntimeChecks,
687+
) -> InterpResult<$tcx, bool> {
692688
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
693689
// unsound differences in evaluating the same constant at different instantiation sites.
694690
interp_ok(true)

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
536536
self.tcx.offset_of_subfield(self.typing_env, layout, fields.iter()).bytes();
537537
ImmTy::from_uint(val, usize_layout())
538538
}
539-
UbChecks => ImmTy::from_bool(M::ub_checks(self)?, *self.tcx),
540-
ContractChecks => ImmTy::from_bool(M::contract_checks(self)?, *self.tcx),
539+
RuntimeChecks(r) => ImmTy::from_bool(M::runtime_checks(self, r)?, *self.tcx),
541540
})
542541
}
543542
}

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,9 @@ impl<'tcx> Body<'tcx> {
648648
}
649649

650650
match rvalue {
651-
Rvalue::NullaryOp(NullOp::UbChecks, _) => Some((tcx.sess.ub_checks() as u128, targets)),
651+
Rvalue::NullaryOp(NullOp::RuntimeChecks(kind), _) => {
652+
Some((kind.value(tcx.sess) as u128, targets))
653+
}
652654
Rvalue::Use(Operand::Constant(constant)) => {
653655
let bits = eval_mono_const(constant)?;
654656
Some((bits, targets))

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10961096
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
10971097
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
10981098
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1099-
NullOp::UbChecks => write!(fmt, "UbChecks()"),
1100-
NullOp::ContractChecks => write!(fmt, "ContractChecks()"),
1099+
NullOp::RuntimeChecks(RuntimeChecks::UbChecks) => write!(fmt, "UbChecks()"),
1100+
NullOp::RuntimeChecks(RuntimeChecks::ContractChecks) => {
1101+
write!(fmt, "ContractChecks()")
1102+
}
11011103
}
11021104
}
11031105
ThreadLocalRef(did) => ty::tls::with(|tcx| {

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,7 @@ impl<'tcx> Rvalue<'tcx> {
786786
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
787787
tcx.types.usize
788788
}
789-
Rvalue::NullaryOp(NullOp::ContractChecks, _)
790-
| Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
789+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => tcx.types.bool,
791790
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
792791
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
793792
AggregateKind::Tuple => {
@@ -855,7 +854,7 @@ impl<'tcx> NullOp<'tcx> {
855854
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
856855
match self {
857856
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
858-
NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
857+
NullOp::RuntimeChecks(_) => tcx.types.bool,
859858
}
860859
}
861860
}

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,12 @@ pub enum NullOp<'tcx> {
15671567
AlignOf,
15681568
/// Returns the offset of a field
15691569
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1570+
/// Returns whether we should perform some checking at runtime.
1571+
RuntimeChecks(RuntimeChecks),
1572+
}
1573+
1574+
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1575+
pub enum RuntimeChecks {
15701576
/// Returns whether we should perform some UB-checking at runtime.
15711577
/// See the `ub_checks` intrinsic docs for details.
15721578
UbChecks,
@@ -1575,6 +1581,15 @@ pub enum NullOp<'tcx> {
15751581
ContractChecks,
15761582
}
15771583

1584+
impl RuntimeChecks {
1585+
pub fn value(self, sess: &rustc_session::Session) -> bool {
1586+
match self {
1587+
Self::UbChecks => sess.ub_checks(),
1588+
Self::ContractChecks => sess.contract_checks(),
1589+
}
1590+
}
1591+
}
1592+
15781593
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
15791594
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)]
15801595
pub enum UnOp {

0 commit comments

Comments
 (0)