mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-14 01:25:54 +00:00
7c263adb2a
This PR adds support for detecting if overflow checks are enabled in similar fashion as debug_assertions are detected. Possible use-case of this, for example, if we want to use checked integer casts in builds with overflow checks, e.g. ```rust pub fn cast(val: usize)->u16 { if cfg!(overflow_checks) { val.try_into().unwrap() } else{ vas as _ } } ``` Resolves #91130. Tracking issue: #111466.
20 lines
325 B
Rust
20 lines
325 B
Rust
// run-pass
|
|
// compile-flags: -C overflow_checks=false
|
|
|
|
#![feature(cfg_overflow_checks)]
|
|
|
|
fn main() {
|
|
assert!(!cfg!(overflow_checks));
|
|
assert!(!compiles_differently());
|
|
}
|
|
|
|
#[cfg(overflow_checks)]
|
|
fn compiles_differently()->bool {
|
|
true
|
|
}
|
|
|
|
#[cfg(not(overflow_checks))]
|
|
fn compiles_differently()->bool {
|
|
false
|
|
}
|