-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Implement strict integer operations that panic on overflow #116090
Conversation
r? @m-ou-se (rustbot has picked a reviewer for you, use r? to override) |
All of the exposed methods should probably be |
☔ The latest upstream changes (presumably #116176) made this pull request unmergeable. Please resolve the merge conflicts. |
3c34635
to
cad16f0
Compare
☔ The latest upstream changes (presumably #117482) made this pull request unmergeable. Please resolve the merge conflicts. |
cc @RalfJung (I didn't know if you were aware of this PR) |
library/core/src/num/int_macros.rs
Outdated
/// #![feature(strict_overflow_ops)] | ||
#[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")] | ||
/// ``` | ||
#[unstable(feature = "strict_overflow_ops", issue = "116064")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably better to create a new tracking issue for this, with the tracking issue template.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I created #118260.
cad16f0
to
8c7a5b0
Compare
library/core/src/num/int_macros.rs
Outdated
/// | ||
/// ## Overflow behavior | ||
/// | ||
/// This function will always panic on overflow, regardless of if overflow checks are enabled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// This function will always panic on overflow, regardless of if overflow checks are enabled. | |
/// This function will always panic on overflow, regardless of whether overflow checks are enabled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be useful to add a comment to this file to note that these functions are used by the strict_
methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RalfJung How would you feel about removing the descriptions from rustc_middle and instead of calling the panic
lang item, making each of the functions below a lang item and calling those? (In a follow-up PR.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think description
is used in a bunch of places where we really need a string, e.g. for MIR dumping.
But if there's some way to reduce the redundancy here, I'm all for it. This duplication of the panic message in 3 or 4 places has bothered me for a while, I just never found a great way to fix it.
library/core/src/num/int_macros.rs
Outdated
#[track_caller] | ||
pub const fn strict_add(self, rhs: Self) -> Self { | ||
let (a, b) = self.overflowing_add(rhs); | ||
if unlikely!(b) {overflow_panic::add()} else {a} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if unlikely!(b) {overflow_panic::add()} else {a} | |
if unlikely!(b) { overflow_panic::add() } else { a } |
@rustbot review |
@m-ou-se just a reminder that there's a PR waiting for review here :) |
@bors r+ |
…u-se Implement strict integer operations that panic on overflow This PR implements the first part of the ACP for adding panic on overflow style arithmetic operations (rust-lang/libs-team#270), mentioned in rust-lang#116064. It adds the following operations on both signed and unsigned integers: - `strict_add` - `strict_sub` - `strict_mul` - `strict_div` - `strict_div_euclid` - `strict_rem` - `strict_rem_euclid` - `strict_neg` - `strict_shl` - `strict_shr` - `strict_pow` Additionally, signed integers have: - `strict_add_unsigned` - `strict_sub_unsigned` - `strict_abs` And unsigned integers have: - `strict_add_signed` The `div` and `rem` operations are the same as normal division and remainder but are added for completeness similar to the corresponding `wrapping_*` operations. I'm not sure if I missed any operations, I basically found them from the `wrapping_*` and `checked_*` operations on both integer types.
Rollup of 8 pull requests Successful merges: - rust-lang#116090 (Implement strict integer operations that panic on overflow) - rust-lang#118811 (Use `bool` instead of `PartiolOrd` as return value of the comparison closure in `{slice,Iteraotr}::is_sorted_by`) - rust-lang#119081 (Add Ipv6Addr::is_ipv4_mapped) - rust-lang#119461 (Use an interpreter in MIR jump threading) - rust-lang#119996 (Move OS String implementation into `sys`) - rust-lang#120015 (coverage: Format all coverage tests with `rustfmt`) - rust-lang#120027 (pattern_analysis: Remove `Ty: Copy` bound) - rust-lang#120084 (fix(rust-analyzer): use new pkgid spec to compare) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#116090 - rmehri01:strict_integer_ops, r=m-ou-se Implement strict integer operations that panic on overflow This PR implements the first part of the ACP for adding panic on overflow style arithmetic operations (rust-lang/libs-team#270), mentioned in rust-lang#116064. It adds the following operations on both signed and unsigned integers: - `strict_add` - `strict_sub` - `strict_mul` - `strict_div` - `strict_div_euclid` - `strict_rem` - `strict_rem_euclid` - `strict_neg` - `strict_shl` - `strict_shr` - `strict_pow` Additionally, signed integers have: - `strict_add_unsigned` - `strict_sub_unsigned` - `strict_abs` And unsigned integers have: - `strict_add_signed` The `div` and `rem` operations are the same as normal division and remainder but are added for completeness similar to the corresponding `wrapping_*` operations. I'm not sure if I missed any operations, I basically found them from the `wrapping_*` and `checked_*` operations on both integer types.
This PR implements the first part of the ACP for adding panic on overflow style arithmetic operations (rust-lang/libs-team#270), mentioned in #116064.
It adds the following operations on both signed and unsigned integers:
strict_add
strict_sub
strict_mul
strict_div
strict_div_euclid
strict_rem
strict_rem_euclid
strict_neg
strict_shl
strict_shr
strict_pow
Additionally, signed integers have:
strict_add_unsigned
strict_sub_unsigned
strict_abs
And unsigned integers have:
strict_add_signed
The
div
andrem
operations are the same as normal division and remainder but are added for completeness similar to the correspondingwrapping_*
operations.I'm not sure if I missed any operations, I basically found them from the
wrapping_*
andchecked_*
operations on both integer types.