Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ER] cfg(overflow_checks) #91130

Closed
leonardo-m opened this issue Nov 22, 2021 · 4 comments · Fixed by #111096
Closed

[ER] cfg(overflow_checks) #91130

leonardo-m opened this issue Nov 22, 2021 · 4 comments · Fixed by #111096
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

An enhancement request discussed here:
https://users.rust-lang.org/t/detecting-overflow-checks/67698/

(That thread also contains a hacky solution by Yandros).

I propose to add a config like this:

#[cfg(overflow_checks)]

I would like to use it to enable/disable lines of code that perform safe unsafe "as" casts replacing them with casts when I compile with debug builds or with release builds compiled with "-C overflow-checks".

@leonardo-m
Copy link
Author

@rustbot modify labels: C-enhancement

@rustbot rustbot added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Nov 22, 2021
@hkratz
Copy link
Contributor

hkratz commented Nov 25, 2021

@rustbot label A-attributes T-compiler

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 25, 2021
@schuelermine
Copy link
Contributor

I’d also be interested in this.

@AngelicosPhosphoros
Copy link
Contributor

Tracking issue: #111466

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue May 13, 2023
…s_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 rust-lang#91130.
@bors bors closed this as completed in 7c263ad May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants