Add support for cfg(overflow_checks)#111096
Conversation
|
r? @b-naber (rustbot has picked a reviewer for you, use r? to override) |
|
Also, I cannot find where this I tested this manually and it works: Test descriptionCode: #[no_mangle]
#[cfg(overflow_checks)]
pub fn cast(v: i64)->u32{
v.try_into().unwrap()
}
#[no_mangle]
#[cfg(not(overflow_checks))]
pub fn cast(v: i64)->u32{
v as _
}Compiled with Compiled with |
|
3e7512c to
e9aa9be
Compare
jyn514
left a comment
There was a problem hiding this comment.
this seems good to me but i'm not sure what the procedure is for adding a new built-in cfg
This comment has been minimized.
This comment has been minimized.
e9aa9be to
32395b0
Compare
This comment was marked as resolved.
This comment was marked as resolved.
|
r? rust-lang/compiler |
32395b0 to
5ac9b22
Compare
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.
5ac9b22 to
7c263ad
Compare
|
@petrochenkov I added feature gate and tracking issue. @rustbot label -S-waiting-on-author +S-waiting-on-review |
|
@bors r+ |
|
🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened. |
Rollup of 6 pull requests Successful merges: - rust-lang#110454 (Require impl Trait in associated types to appear in method signatures) - rust-lang#111096 (Add support for `cfg(overflow_checks)`) - rust-lang#111451 (Note user-facing types of coercion failure) - rust-lang#111469 (Fix data race in llvm source code coverage) - rust-lang#111494 (Encode `VariantIdx` so we can decode ADT variants in the right order) - rust-lang#111499 (asm: loongarch64: Drop efiapi) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
This PR adds support for detecting if overflow checks are enabled in similar fashion as
debug_assertionsare detected. Possible use-case of this, for example, if we want to use checked integer casts in builds with overflow checks, e.g.Resolves #91130.