Skip to content

Commit 78016a2

Browse files
committed
refactor ub_checks and contract_checks to share logic
1 parent bea625f commit 78016a2

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
@@ -1044,8 +1044,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10441044
ConstraintCategory::SizedBound,
10451045
);
10461046
}
1047-
&Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
1048-
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
1047+
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => {}
10491048

10501049
Rvalue::ShallowInitBox(_operand, ty) => {
10511050
let trait_ref =

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -861,17 +861,8 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
861861
fields.iter(),
862862
)
863863
.bytes(),
864-
NullOp::UbChecks => {
865-
let val = fx.tcx.sess.ub_checks();
866-
let val = CValue::by_val(
867-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
868-
fx.layout_of(fx.tcx.types.bool),
869-
);
870-
lval.write_cvalue(fx, val);
871-
return;
872-
}
873-
NullOp::ContractChecks => {
874-
let val = fx.tcx.sess.contract_checks();
864+
NullOp::RuntimeChecks(kind) => {
865+
let val = kind.value(fx.tcx.sess);
875866
let val = CValue::by_val(
876867
fx.bcx.ins().iconst(types::I8, i64::from(val)),
877868
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
@@ -629,12 +629,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
629629
.bytes();
630630
bx.cx().const_usize(val)
631631
}
632-
mir::NullOp::UbChecks => {
633-
let val = bx.tcx().sess.ub_checks();
634-
bx.cx().const_bool(val)
635-
}
636-
mir::NullOp::ContractChecks => {
637-
let val = bx.tcx().sess.contract_checks();
632+
mir::NullOp::RuntimeChecks(kind) => {
633+
let val = kind.value(bx.tcx().sess);
638634
bx.cx().const_bool(val)
639635
}
640636
};

compiler/rustc_const_eval/src/check_consts/check.rs

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

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

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ pub trait Machine<'tcx>: Sized {
295295
interp_ok(())
296296
}
297297

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

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

675675
#[inline(always)]
676-
fn ub_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
677-
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
678-
// unsound differences in evaluating the same constant at different instantiation sites.
679-
interp_ok(true)
680-
}
681-
682-
#[inline(always)]
683-
fn contract_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
676+
fn runtime_checks(
677+
_ecx: &InterpCx<$tcx, Self>,
678+
_r: mir::RuntimeChecks,
679+
) -> InterpResult<$tcx, bool> {
684680
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
685681
// unsound differences in evaluating the same constant at different instantiation sites.
686682
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
@@ -1073,8 +1073,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10731073
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
10741074
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
10751075
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1076-
NullOp::UbChecks => write!(fmt, "UbChecks()"),
1077-
NullOp::ContractChecks => write!(fmt, "ContractChecks()"),
1076+
NullOp::RuntimeChecks(RuntimeChecks::UbChecks) => write!(fmt, "UbChecks()"),
1077+
NullOp::RuntimeChecks(RuntimeChecks::ContractChecks) => {
1078+
write!(fmt, "ContractChecks()")
1079+
}
10781080
}
10791081
}
10801082
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
@@ -754,8 +754,7 @@ impl<'tcx> Rvalue<'tcx> {
754754
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
755755
tcx.types.usize
756756
}
757-
Rvalue::NullaryOp(NullOp::ContractChecks, _)
758-
| Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
757+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => tcx.types.bool,
759758
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
760759
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
761760
AggregateKind::Tuple => {
@@ -823,7 +822,7 @@ impl<'tcx> NullOp<'tcx> {
823822
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
824823
match self {
825824
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
826-
NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
825+
NullOp::RuntimeChecks(_) => tcx.types.bool,
827826
}
828827
}
829828
}

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,12 @@ pub enum NullOp<'tcx> {
15751575
AlignOf,
15761576
/// Returns the offset of a field
15771577
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1578+
/// Returns whether we should perform some checking at runtime.
1579+
RuntimeChecks(RuntimeChecks),
1580+
}
1581+
1582+
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1583+
pub enum RuntimeChecks {
15781584
/// Returns whether we should perform some UB-checking at runtime.
15791585
/// See the `ub_checks` intrinsic docs for details.
15801586
UbChecks,
@@ -1583,6 +1589,15 @@ pub enum NullOp<'tcx> {
15831589
ContractChecks,
15841590
}
15851591

1592+
impl RuntimeChecks {
1593+
pub fn value(self, sess: &rustc_session::Session) -> bool {
1594+
match self {
1595+
Self::UbChecks => sess.ub_checks(),
1596+
Self::ContractChecks => sess.contract_checks(),
1597+
}
1598+
}
1599+
}
1600+
15861601
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
15871602
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)]
15881603
pub enum UnOp {

0 commit comments

Comments
 (0)