Add support for cfg(overflow_checks)
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.
This commit is contained in:
parent
f8d8ffa2eb
commit
7c263adb2a
8 changed files with 65 additions and 0 deletions
19
tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs
Normal file
19
tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
// run-pass
|
||||
// compile-flags: -C overflow_checks=true
|
||||
|
||||
#![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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue