Skip to content

Commit 4d92781

Browse files
authored
Rollup merge of rust-lang#128666 - pitaj:intrinsic-overflow_checks, r=BoxyUwU
Add `overflow_checks` intrinsic This adds an intrinsic which allows code in a pre-built library to inherit the overflow checks option from a crate depending on it. This enables code in the standard library to explicitly change behavior based on whether `overflow_checks` are enabled, regardless of the setting used when standard library was compiled. This is very similar to the `ub_checks` intrinsic, and refactors the two to use a common mechanism. The primary use case for this is to allow the new `RangeFrom` iterator to yield the maximum element before overflowing, as requested [here](rust-lang#125687 (comment)). This PR includes a working `IterRangeFrom` implementation based on this new intrinsic that exhibits the desired behavior. [Prior discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Ability.20to.20select.20code.20based.20on.20.60overflow_checks.60.3F)
2 parents e1d03be + da5c22f commit 4d92781

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

compiler/rustc_public/src/mir/body.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,7 @@ impl Rvalue {
642642
.ok_or_else(|| error!("Expected a `RigidTy` but found: {place_ty:?}"))
643643
}
644644
Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => Ok(Ty::usize_ty()),
645-
Rvalue::NullaryOp(NullOp::ContractChecks, _)
646-
| Rvalue::NullaryOp(NullOp::UbChecks, _) => Ok(Ty::bool_ty()),
645+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => Ok(Ty::bool_ty()),
647646
Rvalue::Aggregate(ak, ops) => match *ak {
648647
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
649648
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -1024,10 +1023,18 @@ pub enum CastKind {
10241023
pub enum NullOp {
10251024
/// Returns the offset of a field.
10261025
OffsetOf(Vec<(VariantIdx, FieldIdx)>),
1026+
/// Codegen conditions for runtime checks.
1027+
RuntimeChecks(RuntimeChecks),
1028+
}
1029+
1030+
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)]
1031+
pub enum RuntimeChecks {
10271032
/// cfg!(ub_checks), but at codegen time
10281033
UbChecks,
10291034
/// cfg!(contract_checks), but at codegen time
10301035
ContractChecks,
1036+
/// cfg!(overflow_checks), but at codegen time
1037+
OverflowChecks,
10311038
}
10321039

10331040
impl Operand {

compiler/rustc_public/src/unstable/convert/stable/mir.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,16 @@ impl<'tcx> Stable<'tcx> for mir::NullOp<'tcx> {
322322
cx: &CompilerCtxt<'cx, BridgeTys>,
323323
) -> Self::T {
324324
use rustc_middle::mir::NullOp::*;
325+
use rustc_middle::mir::RuntimeChecks::*;
325326
match self {
326327
OffsetOf(indices) => crate::mir::NullOp::OffsetOf(
327328
indices.iter().map(|idx| idx.stable(tables, cx)).collect(),
328329
),
329-
UbChecks => crate::mir::NullOp::UbChecks,
330-
ContractChecks => crate::mir::NullOp::ContractChecks,
330+
RuntimeChecks(op) => crate::mir::NullOp::RuntimeChecks(match op {
331+
UbChecks => crate::mir::RuntimeChecks::UbChecks,
332+
ContractChecks => crate::mir::RuntimeChecks::ContractChecks,
333+
OverflowChecks => crate::mir::RuntimeChecks::OverflowChecks,
334+
}),
331335
}
332336
}
333337
}

0 commit comments

Comments
 (0)