Skip to content

Commit 9714d5d

Browse files
Auto merge of #148703 - pitaj:rangefrom-overflow_checks, r=<try>
Use `overflow_checks` intrinsic so `IterRangeFrom` yields MAX before panicking in debug
2 parents 87f9dcd + ac9134a commit 9714d5d

File tree

34 files changed

+330
-83
lines changed

34 files changed

+330
-83
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
@@ -624,12 +624,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
624624
.bytes();
625625
bx.cx().const_usize(val)
626626
}
627-
mir::NullOp::UbChecks => {
628-
let val = bx.tcx().sess.ub_checks();
629-
bx.cx().const_bool(val)
630-
}
631-
mir::NullOp::ContractChecks => {
632-
let val = bx.tcx().sess.contract_checks();
627+
mir::NullOp::RuntimeChecks(kind) => {
628+
let val = kind.value(bx.tcx().sess);
633629
bx.cx().const_bool(val)
634630
}
635631
};

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
645645

646646
Rvalue::Cast(_, _, _) => {}
647647

648-
Rvalue::NullaryOp(
649-
NullOp::OffsetOf(_) | NullOp::UbChecks | NullOp::ContractChecks,
650-
_,
651-
) => {}
648+
Rvalue::NullaryOp(NullOp::OffsetOf(_) | NullOp::RuntimeChecks(_), _) => {}
652649
Rvalue::ShallowInitBox(_, _) => {}
653650

654651
Rvalue::UnaryOp(op, operand) => {

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_hir_analysis/src/check/intrinsic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
163163
| sym::minnumf128
164164
| sym::mul_with_overflow
165165
| sym::needs_drop
166+
| sym::overflow_checks
166167
| sym::powf16
167168
| sym::powf32
168169
| sym::powf64
@@ -643,7 +644,7 @@ pub(crate) fn check_intrinsic_type(
643644
sym::aggregate_raw_ptr => (3, 0, vec![param(1), param(2)], param(0)),
644645
sym::ptr_metadata => (2, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(1)),
645646

646-
sym::ub_checks => (0, 0, Vec::new(), tcx.types.bool),
647+
sym::ub_checks | sym::overflow_checks => (0, 0, Vec::new(), tcx.types.bool),
647648

648649
sym::box_new => (1, 0, vec![param(0)], Ty::new_box(tcx, param(0))),
649650

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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,8 +1101,13 @@ impl<'tcx> Debug for Rvalue<'tcx> {
11011101
let t = with_no_trimmed_paths!(format!("{}", t));
11021102
match op {
11031103
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1104-
NullOp::UbChecks => write!(fmt, "UbChecks()"),
1105-
NullOp::ContractChecks => write!(fmt, "ContractChecks()"),
1104+
NullOp::RuntimeChecks(RuntimeChecks::UbChecks) => write!(fmt, "UbChecks()"),
1105+
NullOp::RuntimeChecks(RuntimeChecks::ContractChecks) => {
1106+
write!(fmt, "ContractChecks()")
1107+
}
1108+
NullOp::RuntimeChecks(RuntimeChecks::OverflowChecks) => {
1109+
write!(fmt, "OverflowChecks()")
1110+
}
11061111
}
11071112
}
11081113
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
}

0 commit comments

Comments
 (0)