Skip to content

Commit 0c98a31

Browse files
committed
add overflow_checks intrinsic
1 parent 741831c commit 0c98a31

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

core/src/intrinsics/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,24 @@ pub const fn ub_checks() -> bool {
25852585
cfg!(ub_checks)
25862586
}
25872587

2588+
/// Returns whether we should perform some overflow-checking at runtime. This eventually evaluates to
2589+
/// `cfg!(overflow_checks)`, but behaves different from `cfg!` when mixing crates built with different
2590+
/// flags: if the crate has overflow checks enabled or carries the `#[rustc_inherit_overflow_checks]`
2591+
/// attribute, evaluation is delayed until monomorphization (or until the call gets inlined into
2592+
/// a crate that does not delay evaluation further); otherwise it can happen any time.
2593+
///
2594+
/// The common case here is a user program built with overflow_checks linked against the distributed
2595+
/// sysroot which is built without overflow_checks but with `#[rustc_inherit_overflow_checks]`.
2596+
/// For code that gets monomorphized in the user crate (i.e., generic functions and functions with
2597+
/// `#[inline]`), gating assertions on `overflow_checks()` rather than `cfg!(overflow_checks)` means that
2598+
/// assertions are enabled whenever the *user crate* has overflow checks enabled. However if the
2599+
/// user has overflow checks disabled, the checks will still get optimized out.
2600+
#[inline(always)]
2601+
#[rustc_intrinsic]
2602+
pub const fn overflow_checks() -> bool {
2603+
cfg!(debug_assertions)
2604+
}
2605+
25882606
/// Allocates a block of memory at compile time.
25892607
/// At runtime, just returns a null pointer.
25902608
///

0 commit comments

Comments
 (0)