about summary refs log tree commit diff
path: root/compiler/rustc_session
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2023-05-13 11:05:33 +0530
committerGitHub <noreply@github.com>2023-05-13 11:05:33 +0530
commit36125c43da00a2c57452e72c3cf81b71b13de208 (patch)
treee5e827d0c93f013603b704607f326606a53f4fd1 /compiler/rustc_session
parent6cb13585d050ed9c708b3d03ab2797ab16a78218 (diff)
parent7c263adb2afc310b96422bd33317407bc5385bf9 (diff)
downloadrust-36125c43da00a2c57452e72c3cf81b71b13de208.tar.gz
rust-36125c43da00a2c57452e72c3cf81b71b13de208.zip
Rollup merge of #111096 - AngelicosPhosphoros:overflow_checks_issue_91130, 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.
Diffstat (limited to 'compiler/rustc_session')
-rw-r--r--compiler/rustc_session/src/config.rs4
1 files changed, 4 insertions, 0 deletions
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);