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 /compiler | |
| 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 'compiler')
| -rw-r--r-- | compiler/rustc_feature/src/active.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_feature/src/builtin_attrs.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_session/src/config.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_span/src/symbol.rs | 2 |
4 files changed, 9 insertions, 0 deletions
diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index f4615445f8e..fe8c630666b 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -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 = "...")`. diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index fe05d4590e7..61cfbf5c5e5 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -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)), ( diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index aa3cb03bad8..e2b8d3eea2d 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -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); diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 60efcb768cb..9951d8f4fc1 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -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, |
