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

Tracking Issue for cfg(overflow_checks) #111466

Open
1 of 3 tasks
AngelicosPhosphoros opened this issue May 11, 2023 · 1 comment
Open
1 of 3 tasks

Tracking Issue for cfg(overflow_checks) #111466

AngelicosPhosphoros opened this issue May 11, 2023 · 1 comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC

Comments

@AngelicosPhosphoros
Copy link
Contributor

AngelicosPhosphoros commented May 11, 2023

The feature gate for the issue is #![feature(cfg_overflow_checks)].

It allows write code that detects if compiler emits code for integer overflow checks similarly to how cfg(debug_assertions) allow to detect if debug assertions enabled.

Example:

#[cfg(overflow_checks)]
#[track_caller]
fn cast(v: i64)->u32{
    v.try_into().unwrap()
}

#[cfg(not(overflow_checks))]
fn cast(v: i64)->u32{
    v as _
}

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.

Steps

Unresolved Questions

Implementation history

#111096
rust-lang/reference#1355

@AngelicosPhosphoros AngelicosPhosphoros added the C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC label May 11, 2023
AngelicosPhosphoros added a commit to AngelicosPhosphoros/rust that referenced this issue May 11, 2023
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.
Tracking issue: rust-lang#111466.
@CAD97
Copy link
Contributor

CAD97 commented Apr 23, 2024

Note: this feature is observable on nightly without #![feature] via build scripts checking $CARGO_CFG_OVERFLOW_CHECKS. The env var isn't set for beta/stable, so this is fine, but imho still warrants mentioning here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC
Projects
None yet
Development

No branches or pull requests

2 participants