1
Fork 0

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.
This commit is contained in:
Dylan DPC 2023-05-13 11:05:33 +05:30 committed by GitHub
commit 36125c43da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 65 additions and 0 deletions

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);