Skip to content

Commit

Permalink
Rollup merge of #111096 - AngelicosPhosphoros:overflow_checks_issue_9…
Browse files Browse the repository at this point in the history
…1130, r=petrochenkov

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.
  • Loading branch information
Dylan-DPC authored May 13, 2023
2 parents 6cb1358 + 7c263ad commit 36125c4
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "...")`.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
(
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ symbols! {
cfg_doctest,
cfg_eval,
cfg_hide,
cfg_overflow_checks,
cfg_panic,
cfg_sanitize,
cfg_target_abi,
Expand Down Expand Up @@ -1065,6 +1066,7 @@ symbols! {
or_patterns,
other,
out,
overflow_checks,
overlapping_marker_traits,
owned_box,
packed,
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/feature-gates/feature-gate-cfg_overflow_checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![crate_type = "lib"]

#[cfg(overflow_checks)] //~ ERROR `cfg(overflow_checks)` is experimental
pub fn cast(v: i64)->u32{
todo!()
}
12 changes: 12 additions & 0 deletions tests/ui/feature-gates/feature-gate-cfg_overflow_checks.stderr
Original file line number Diff line number Diff line change
@@ -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`.
19 changes: 19 additions & 0 deletions tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 36125c4

Please sign in to comment.