Skip to content

Commit d86784c

Browse files
committed
refactor ub_checks and contract_checks to share logic
1 parent 3559e0a commit d86784c

File tree

23 files changed

+67
-91
lines changed

23 files changed

+67
-91
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10401040
ConstraintCategory::SizedBound,
10411041
);
10421042
}
1043-
&Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
1044-
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
1043+
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => {}
10451044

10461045
Rvalue::ShallowInitBox(_operand, ty) => {
10471046
let trait_ref = ty::TraitRef::new(

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -836,17 +836,8 @@ fn codegen_stmt<'tcx>(
836836
fields.iter(),
837837
)
838838
.bytes(),
839-
NullOp::UbChecks => {
840-
let val = fx.tcx.sess.ub_checks();
841-
let val = CValue::by_val(
842-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
843-
fx.layout_of(fx.tcx.types.bool),
844-
);
845-
lval.write_cvalue(fx, val);
846-
return;
847-
}
848-
NullOp::ContractChecks => {
849-
let val = fx.tcx.sess.contract_checks();
839+
NullOp::RuntimeChecks(kind) => {
840+
let val = kind.value(fx.tcx.sess);
850841
let val = CValue::by_val(
851842
fx.bcx.ins().iconst(types::I8, i64::from(val)),
852843
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
@@ -752,12 +752,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
752752
.bytes();
753753
bx.cx().const_usize(val)
754754
}
755-
mir::NullOp::UbChecks => {
756-
let val = bx.tcx().sess.ub_checks();
757-
bx.cx().const_bool(val)
758-
}
759-
mir::NullOp::ContractChecks => {
760-
let val = bx.tcx().sess.contract_checks();
755+
mir::NullOp::RuntimeChecks(kind) => {
756+
let val = kind.value(bx.tcx().sess);
761757
bx.cx().const_bool(val)
762758
}
763759
};

compiler/rustc_const_eval/src/check_consts/check.rs

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

677677
Rvalue::NullaryOp(
678-
NullOp::SizeOf
679-
| NullOp::AlignOf
680-
| NullOp::OffsetOf(_)
681-
| NullOp::UbChecks
682-
| NullOp::ContractChecks,
678+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::RuntimeChecks(_),
683679
_,
684680
) => {}
685681
Rvalue::ShallowInitBox(_, _) => {}

compiler/rustc_const_eval/src/interpret/machine.rs

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

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

305302
/// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
306303
/// You can use this to detect long or endlessly running programs.
@@ -677,14 +674,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
677674
}
678675

679676
#[inline(always)]
680-
fn ub_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
681-
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
682-
// unsound differences in evaluating the same constant at different instantiation sites.
683-
interp_ok(true)
684-
}
685-
686-
#[inline(always)]
687-
fn contract_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
677+
fn runtime_checks(_ecx: &InterpCx<$tcx, Self>, _r: mir::RuntimeChecks) -> InterpResult<$tcx, bool> {
688678
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
689679
// unsound differences in evaluating the same constant at different instantiation sites.
690680
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
@@ -692,7 +692,9 @@ impl<'tcx> Body<'tcx> {
692692
}
693693

694694
match rvalue {
695-
Rvalue::NullaryOp(NullOp::UbChecks, _) => Some((tcx.sess.ub_checks() as u128, targets)),
695+
Rvalue::NullaryOp(NullOp::RuntimeChecks(kind), _) => {
696+
Some((kind.value(tcx.sess) as u128, targets))
697+
}
696698
Rvalue::Use(Operand::Constant(constant)) => {
697699
let bits = eval_mono_const(constant)?;
698700
Some((bits, targets))

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
11341134
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
11351135
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
11361136
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1137-
NullOp::UbChecks => write!(fmt, "UbChecks()"),
1138-
NullOp::ContractChecks => write!(fmt, "ContractChecks()"),
1137+
NullOp::RuntimeChecks(RuntimeChecks::UbChecks) => write!(fmt, "UbChecks()"),
1138+
NullOp::RuntimeChecks(RuntimeChecks::ContractChecks) => {
1139+
write!(fmt, "ContractChecks()")
1140+
}
11391141
}
11401142
}
11411143
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
@@ -709,8 +709,7 @@ impl<'tcx> Rvalue<'tcx> {
709709
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
710710
tcx.types.usize
711711
}
712-
Rvalue::NullaryOp(NullOp::ContractChecks, _)
713-
| Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
712+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => tcx.types.bool,
714713
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
715714
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
716715
AggregateKind::Tuple => {
@@ -778,7 +777,7 @@ impl<'tcx> NullOp<'tcx> {
778777
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
779778
match self {
780779
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
781-
NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
780+
NullOp::RuntimeChecks(_) => tcx.types.bool,
782781
}
783782
}
784783
}

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,12 @@ pub enum NullOp<'tcx> {
15881588
AlignOf,
15891589
/// Returns the offset of a field
15901590
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1591+
/// Returns whether we should perform some checking at runtime.
1592+
RuntimeChecks(RuntimeChecks),
1593+
}
1594+
1595+
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1596+
pub enum RuntimeChecks {
15911597
/// Returns whether we should perform some UB-checking at runtime.
15921598
/// See the `ub_checks` intrinsic docs for details.
15931599
UbChecks,
@@ -1596,6 +1602,16 @@ pub enum NullOp<'tcx> {
15961602
ContractChecks,
15971603
}
15981604

1605+
impl RuntimeChecks {
1606+
pub fn value(self, sess: &rustc_session::Session) -> bool {
1607+
match self {
1608+
Self::UbChecks => sess.ub_checks(),
1609+
Self::ContractChecks => sess.contract_checks(),
1610+
Self::OverflowChecks => sess.overflow_checks(),
1611+
}
1612+
}
1613+
}
1614+
15991615
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
16001616
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)]
16011617
pub enum UnOp {

0 commit comments

Comments
 (0)