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:
AngelicosPhosphoros 2023-05-02 17:53:11 +04:00
parent f8d8ffa2eb
commit 7c263adb2a
8 changed files with 65 additions and 0 deletions

View File

@ -321,6 +321,8 @@ declare_features! (
(active, c_unwind, "1.52.0", Some(74990), None),
/// Allows using C-variadics.
(active, c_variadic, "1.34.0", Some(44930), None),
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
(active, cfg_overflow_checks, "CURRENT_RUSTC_VERSION", Some(111466), None),
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
(active, cfg_sanitize, "1.41.0", Some(39699), None),
/// Allows `cfg(target_abi = "...")`.

View File

@ -24,6 +24,7 @@ pub type GatedCfg = (Symbol, Symbol, GateFn);
/// `cfg(...)`'s that are feature gated.
const GATED_CFGS: &[GatedCfg] = &[
// (name in cfg, feature, function to check if the feature is enabled)
(sym::overflow_checks, sym::cfg_overflow_checks, cfg_fn!(cfg_overflow_checks)),
(sym::target_abi, sym::cfg_target_abi, cfg_fn!(cfg_target_abi)),
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
(

View File

@ -1060,6 +1060,9 @@ fn default_configuration(sess: &Session) -> CrateConfig {
if sess.opts.debug_assertions {
ret.insert((sym::debug_assertions, None));
}
if sess.overflow_checks() {
ret.insert((sym::overflow_checks, None));
}
// JUSTIFICATION: before wrapper fn is available
#[allow(rustc::bad_opt_access)]
if sess.opts.crate_types.contains(&CrateType::ProcMacro) {
@ -1209,6 +1212,7 @@ impl CrateCheckConfig {
sym::windows,
sym::proc_macro,
sym::debug_assertions,
sym::overflow_checks,
sym::target_thread_local,
] {
self.expecteds.entry(name).or_insert_with(no_values);

View File

@ -463,6 +463,7 @@ symbols! {
cfg_doctest,
cfg_eval,
cfg_hide,
cfg_overflow_checks,
cfg_panic,
cfg_sanitize,
cfg_target_abi,
@ -1065,6 +1066,7 @@ symbols! {
or_patterns,
other,
out,
overflow_checks,
overlapping_marker_traits,
owned_box,
packed,

View File

@ -0,0 +1,6 @@
#![crate_type = "lib"]
#[cfg(overflow_checks)] //~ ERROR `cfg(overflow_checks)` is experimental
pub fn cast(v: i64)->u32{
todo!()
}

View File

@ -0,0 +1,12 @@
error[E0658]: `cfg(overflow_checks)` is experimental and subject to change
--> $DIR/feature-gate-cfg_overflow_checks.rs:3:7
|
LL | #[cfg(overflow_checks)]
| ^^^^^^^^^^^^^^^
|
= note: see issue #111466 <https://github.com/rust-lang/rust/issues/111466> for more information
= help: add `#![feature(cfg_overflow_checks)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View 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
}

View File

@ -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
}