Skip to content

Commit 2e5d395

Browse files
committed
refactor ub_checks and contract_checks to share logic
1 parent 04ff05c commit 2e5d395

File tree

23 files changed

+75
-67
lines changed

23 files changed

+75
-67
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10461046
}
10471047
}
10481048

1049-
&Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
1050-
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
1049+
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => {}
10511050

10521051
Rvalue::ShallowInitBox(_operand, ty) => {
10531052
let trait_ref =

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -841,17 +841,8 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
841841
fields.iter(),
842842
)
843843
.bytes(),
844-
NullOp::UbChecks => {
845-
let val = fx.tcx.sess.ub_checks();
846-
let val = CValue::by_val(
847-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
848-
fx.layout_of(fx.tcx.types.bool),
849-
);
850-
lval.write_cvalue(fx, val);
851-
return;
852-
}
853-
NullOp::ContractChecks => {
854-
let val = fx.tcx.sess.contract_checks();
844+
NullOp::RuntimeChecks(kind) => {
845+
let val = kind.value(fx.tcx.sess);
855846
let val = CValue::by_val(
856847
fx.bcx.ins().iconst(types::I8, i64::from(val)),
857848
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
@@ -618,12 +618,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
618618
.bytes();
619619
bx.cx().const_usize(val)
620620
}
621-
mir::NullOp::UbChecks => {
622-
let val = bx.tcx().sess.ub_checks();
623-
bx.cx().const_bool(val)
624-
}
625-
mir::NullOp::ContractChecks => {
626-
let val = bx.tcx().sess.contract_checks();
621+
mir::NullOp::RuntimeChecks(kind) => {
622+
let val = kind.value(bx.tcx().sess);
627623
bx.cx().const_bool(val)
628624
}
629625
};

compiler/rustc_const_eval/src/check_consts/check.rs

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

648648
Rvalue::NullaryOp(
649-
NullOp::OffsetOf(_) | NullOp::UbChecks | NullOp::ContractChecks,
649+
NullOp::OffsetOf(_) | NullOp::RuntimeChecks(_),
650650
_,
651651
) => {}
652652
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
@@ -522,8 +522,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
522522
self.tcx.offset_of_subfield(self.typing_env, layout, fields.iter()).bytes();
523523
ImmTy::from_uint(val, usize_layout())
524524
}
525-
UbChecks => ImmTy::from_bool(M::ub_checks(self)?, *self.tcx),
526-
ContractChecks => ImmTy::from_bool(M::contract_checks(self)?, *self.tcx),
525+
RuntimeChecks(r) => ImmTy::from_bool(M::runtime_checks(self, r)?, *self.tcx),
527526
})
528527
}
529528
}

compiler/rustc_middle/src/mir/mod.rs

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

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

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10931093
let t = with_no_trimmed_paths!(format!("{}", t));
10941094
match op {
10951095
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1096-
NullOp::UbChecks => write!(fmt, "UbChecks()"),
1097-
NullOp::ContractChecks => write!(fmt, "ContractChecks()"),
1096+
NullOp::RuntimeChecks(RuntimeChecks::UbChecks) => write!(fmt, "UbChecks()"),
1097+
NullOp::RuntimeChecks(RuntimeChecks::ContractChecks) => {
1098+
write!(fmt, "ContractChecks()")
1099+
}
10981100
}
10991101
}
11001102
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
@@ -795,8 +795,7 @@ impl<'tcx> Rvalue<'tcx> {
795795
}
796796
Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
797797
Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => tcx.types.usize,
798-
Rvalue::NullaryOp(NullOp::ContractChecks, _)
799-
| Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
798+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => tcx.types.bool,
800799
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
801800
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
802801
AggregateKind::Tuple => {
@@ -864,7 +863,7 @@ impl<'tcx> NullOp<'tcx> {
864863
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
865864
match self {
866865
NullOp::OffsetOf(_) => tcx.types.usize,
867-
NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
866+
NullOp::RuntimeChecks(_) => tcx.types.bool,
868867
}
869868
}
870869
}

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,12 @@ pub enum AggregateKind<'tcx> {
15651565
pub enum NullOp<'tcx> {
15661566
/// Returns the offset of a field
15671567
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1568+
/// Returns whether we should perform some checking at runtime.
1569+
RuntimeChecks(RuntimeChecks),
1570+
}
1571+
1572+
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1573+
pub enum RuntimeChecks {
15681574
/// Returns whether we should perform some UB-checking at runtime.
15691575
/// See the `ub_checks` intrinsic docs for details.
15701576
UbChecks,
@@ -1573,6 +1579,15 @@ pub enum NullOp<'tcx> {
15731579
ContractChecks,
15741580
}
15751581

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

0 commit comments

Comments
 (0)