Skip to content

Commit

Permalink
Auto merge of #122520 - scottmcm:stabilize_unchecked_math_basics, r=j…
Browse files Browse the repository at this point in the history
…hpratt

Stabilize `unchecked_{add,sub,mul}`

Tracking issue: #85122

I think we might as well just stabilize these basic three.  They're the ones that have `nuw`/`nsw` flags in LLVM.

Notably, this doesn't include the potentially-more-complex or -more-situational things like `unchecked_neg` or `unchecked_shr` that are under different feature flags.

To quote Ralf rust-lang/rust#85122 (comment),

> Are there any objections to stabilizing at least `unchecked_{add,sub,mul}`? For those there shouldn't be any surprises about what their safety requirements are.

*Semantially* these are [already available on stable, even in `const`, via](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=bdb1ff889b61950897f1e9f56d0c9a36) `checked_*`+`unreachable_unchecked`.  So IMHO we might as well just let people write them directly, rather than try to go through a `let Some(x) = x.checked_add(y) else { unsafe { hint::unreachable_unchecked() }};` dance.

I added additional text to each method to attempt to better describe the behaviour and encourage `wrapping_*` instead.

r? rust-lang/libs-api
  • Loading branch information
bors committed Mar 29, 2024
2 parents 8d829ae + d35807c commit 331d9f0
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 8 deletions.
2 changes: 0 additions & 2 deletions tests/fail/intrinsics/unchecked_add1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(unchecked_math)]

fn main() {
// MAX overflow
let _val = unsafe { 40000u16.unchecked_add(30000) }; //~ ERROR: overflow executing `unchecked_add`
Expand Down
2 changes: 0 additions & 2 deletions tests/fail/intrinsics/unchecked_add2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(unchecked_math)]

fn main() {
// MIN overflow
let _val = unsafe { (-30000i16).unchecked_add(-8000) }; //~ ERROR: overflow executing `unchecked_add`
Expand Down
1 change: 0 additions & 1 deletion tests/fail/intrinsics/unchecked_mul1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(unchecked_math)]
fn main() {
// MAX overflow
let _val = unsafe { 300u16.unchecked_mul(250u16) }; //~ ERROR: overflow executing `unchecked_mul`
Expand Down
1 change: 0 additions & 1 deletion tests/fail/intrinsics/unchecked_mul2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(unchecked_math)]
fn main() {
// MIN overflow
let _val = unsafe { 1_000_000_000i32.unchecked_mul(-4) }; //~ ERROR: overflow executing `unchecked_mul`
Expand Down
1 change: 0 additions & 1 deletion tests/fail/intrinsics/unchecked_sub1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(unchecked_math)]
fn main() {
// MIN overflow
let _val = unsafe { 14u32.unchecked_sub(22) }; //~ ERROR: overflow executing `unchecked_sub`
Expand Down
1 change: 0 additions & 1 deletion tests/fail/intrinsics/unchecked_sub2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(unchecked_math)]
fn main() {
// MAX overflow
let _val = unsafe { 30000i16.unchecked_sub(-7000) }; //~ ERROR: overflow executing `unchecked_sub`
Expand Down

0 comments on commit 331d9f0

Please sign in to comment.