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

Add support for cfg(overflow_checks) #111096

Merged

Conversation

AngelicosPhosphoros
Copy link
Contributor

@AngelicosPhosphoros AngelicosPhosphoros commented May 2, 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.

pub fn cast(val: usize)->u16 {
    if cfg!(overflow_checks) {
        val.try_into().unwrap()
    }
    else{
        vas as _
    }
}

Resolves #91130.

@rustbot
Copy link
Collaborator

rustbot commented May 2, 2023

r? @b-naber

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 2, 2023
@AngelicosPhosphoros
Copy link
Contributor Author

Also, I cannot find where this cfg options are tested so I haven't added test yet. I would appreciate if someone points where to put it.

I tested this manually and it works:

Test description

Code:

#[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 rustc +stage1 test_c.rs -Copt-level=3 --crate-type=cdylib --emit=llvm-ir --edition=2021 -Cdebug_assertions=true -Coverflow_checks=false

; ModuleID = 'test_c.46e36185cb05fdac-cgu.0'
source_filename = "test_c.46e36185cb05fdac-cgu.0"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define noundef i32 @cast(i64 noundef %v) unnamed_addr #0 {
start:
  %0 = trunc i64 %v to i32
  ret i32 %0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable "target-cpu"="x86-64" }

!llvm.module.flags = !{!0}

!0 = !{i32 8, !"PIC Level", i32 2}

Compiled with rustc +stage1 test_c.rs -Copt-level=3 --crate-type=cdylib --emit=llvm-ir --edition=2021 -Cdebug_assertions=true -Coverflow_checks=true

; ModuleID = 'test_c.46e36185cb05fdac-cgu.0'
source_filename = "test_c.46e36185cb05fdac-cgu.0"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc"

%"core::num::error::TryFromIntError" = type { {} }

@alloc_00ae4b301f7fab8ac9617c03fcbd7274 = private unnamed_addr constant <{ [43 x i8] }> <{ [43 x i8] c"called `Result::unwrap()` on an `Err` value" }>, align 1
@vtable.0 = private unnamed_addr constant <{ ptr, [16 x i8], ptr }> <{ ptr @"_ZN4core3ptr54drop_in_place$LT$core..num..error..TryFromIntError$GT$17h7f451cb6d2235960E", [16 x i8] c"\00\00\00\00\00\00\00\00\01\00\00\00\00\00\00\00", ptr @"_ZN70_$LT$core..num..error..TryFromIntError$u20$as$u20$core..fmt..Debug$GT$3fmt17hc75af1d3d73262e8E" }>, align 8
@alloc_2bbc63c594421bee4d4201fca631d215 = private unnamed_addr constant <{ [9 x i8] }> <{ [9 x i8] c"test_c.rs" }>, align 1
@alloc_ea0d6a17a859d145cd5d93e8f6368c86 = private unnamed_addr constant <{ ptr, [16 x i8] }> <{ ptr @alloc_2bbc63c594421bee4d4201fca631d215, [16 x i8] c"\09\00\00\00\00\00\00\00\04\00\00\00\12\00\00\00" }>, align 8

; core::ptr::drop_in_place<core::num::error::TryFromIntError>
; Function Attrs: inlinehint mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define internal void @"_ZN4core3ptr54drop_in_place$LT$core..num..error..TryFromIntError$GT$17h7f451cb6d2235960E"(ptr nocapture readnone %_1) unnamed_addr #0 {
start:
  ret void
}

; Function Attrs: uwtable
define noundef i32 @cast(i64 noundef %v) unnamed_addr #1 personality ptr @__CxxFrameHandler3 {
start:
  %e.i = alloca %"core::num::error::TryFromIntError", align 1
  %_5.0 = icmp ugt i64 %v, 4294967295
  br i1 %_5.0, label %bb5.split, label %bb6.split

bb6.split:                                        ; preds = %start
  %_8 = trunc i64 %v to i32
  ret i32 %_8

bb5.split:                                        ; preds = %start
  call void @llvm.lifetime.start.p0(i64 0, ptr nonnull %e.i)
; call core::result::unwrap_failed
  call void @_ZN4core6result13unwrap_failed17h7fc0894136e1312bE(ptr noalias noundef nonnull readonly align 1 @alloc_00ae4b301f7fab8ac9617c03fcbd7274, i64 noundef 43, ptr noundef nonnull align 1 %e.i, ptr noalias noundef nonnull readonly align 8 dereferenceable(24) @vtable.0, ptr noalias noundef nonnull readonly align 8 dereferenceable(24) @alloc_ea0d6a17a859d145cd5d93e8f6368c86) #5
  unreachable
}

declare i32 @__CxxFrameHandler3(...) unnamed_addr #2

; <core::num::error::TryFromIntError as core::fmt::Debug>::fmt
; Function Attrs: uwtable
declare noundef zeroext i1 @"_ZN70_$LT$core..num..error..TryFromIntError$u20$as$u20$core..fmt..Debug$GT$3fmt17hc75af1d3d73262e8E"(ptr noalias noundef nonnull readonly align 1, ptr noalias noundef align 8 dereferenceable(64)) unnamed_addr #1

; core::result::unwrap_failed
; Function Attrs: cold noinline noreturn uwtable
declare void @_ZN4core6result13unwrap_failed17h7fc0894136e1312bE(ptr noalias noundef nonnull readonly align 1, i64 noundef, ptr noundef nonnull align 1, ptr noalias noundef readonly align 8 dereferenceable(24), ptr noalias noundef readonly align 8 dereferenceable(24)) unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #4

attributes #0 = { inlinehint mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable "target-cpu"="x86-64" }
attributes #1 = { uwtable "target-cpu"="x86-64" }
attributes #2 = { "target-cpu"="x86-64" }
attributes #3 = { cold noinline noreturn uwtable "target-cpu"="x86-64" }
attributes #4 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { noreturn }

!llvm.module.flags = !{!0}

!0 = !{i32 8, !"PIC Level", i32 2}

@jyn514
Copy link
Member

jyn514 commented May 2, 2023

Also, I cannot find where this cfg options are tested so I haven't added test yet. I would appreciate if someone points where to put it.

tests/ui/numbers-arithmetic seems like a good place

@AngelicosPhosphoros AngelicosPhosphoros force-pushed the overflow_checks_issue_91130 branch from 3e7512c to e9aa9be Compare May 2, 2023 15:25
Copy link
Member

@jyn514 jyn514 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems good to me but i'm not sure what the procedure is for adding a new built-in cfg

AngelicosPhosphoros added a commit to AngelicosPhosphoros/rust-reference that referenced this pull request May 2, 2023
@rust-log-analyzer

This comment has been minimized.

@AngelicosPhosphoros AngelicosPhosphoros force-pushed the overflow_checks_issue_91130 branch from e9aa9be to 32395b0 Compare May 2, 2023 15:59
@bors

This comment was marked as resolved.

@b-naber
Copy link
Contributor

b-naber commented May 7, 2023

r? rust-lang/compiler

@rustbot rustbot assigned petrochenkov and unassigned b-naber May 7, 2023
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 10, 2023
@AngelicosPhosphoros AngelicosPhosphoros force-pushed the overflow_checks_issue_91130 branch from 32395b0 to 5ac9b22 Compare May 11, 2023 13:07
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.
@AngelicosPhosphoros AngelicosPhosphoros force-pushed the overflow_checks_issue_91130 branch from 5ac9b22 to 7c263ad Compare May 11, 2023 14:07
@AngelicosPhosphoros
Copy link
Contributor Author

@petrochenkov I added feature gate and tracking issue.

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 11, 2023
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 11, 2023

📌 Commit 7c263ad has been approved by petrochenkov

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented May 11, 2023

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 11, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request May 13, 2023
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
@bors bors merged commit 36125c4 into rust-lang:master May 13, 2023
@rustbot rustbot added this to the 1.71.0 milestone May 13, 2023
@AngelicosPhosphoros AngelicosPhosphoros deleted the overflow_checks_issue_91130 branch May 13, 2023 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ER] cfg(overflow_checks)
8 participants