diff options
| author | AngelicosPhosphoros <xuzin.timur@gmail.com> | 2023-05-02 17:53:11 +0400 |
|---|---|---|
| committer | AngelicosPhosphoros <xuzin.timur@gmail.com> | 2023-05-11 18:06:31 +0400 |
| commit | 7c263adb2afc310b96422bd33317407bc5385bf9 (patch) | |
| tree | e5c2a5db3896f6832dfe64f857461c292c463f3e /tests/ui/numbers-arithmetic | |
| parent | f8d8ffa2eba53928662dc57bc3a6c5608beb26f1 (diff) | |
| download | rust-7c263adb2afc310b96422bd33317407bc5385bf9.tar.gz rust-7c263adb2afc310b96422bd33317407bc5385bf9.zip | |
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.
Diffstat (limited to 'tests/ui/numbers-arithmetic')
| -rw-r--r-- | tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs | 19 | ||||
| -rw-r--r-- | tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs | 19 |
2 files changed, 38 insertions, 0 deletions
diff --git a/tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs b/tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs new file mode 100644 index 00000000000..318be2a6401 --- /dev/null +++ b/tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs @@ -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 +} diff --git a/tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs b/tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs new file mode 100644 index 00000000000..0367d980a64 --- /dev/null +++ b/tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs @@ -0,0 +1,19 @@ +// 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 +} |
