Skip to content

Commit

Permalink
Tweak Library Integer Division Docs
Browse files Browse the repository at this point in the history
Improved the documentation and diagnostics related to panicking in the division-like methods in std:

* For signed methods that can overflow, clarified "results in overflow" to "self is -1 and rhs is Self::MIN." This is more concise that saying "results in overflow" and then explaining how it could overflow.
* For floor/ceil_div, corrected the documentation and made it more like the documentation in other methods.
* For signed methods that can overflow, explicitly mention that they are not affected by compiler flags.
* Removed all unused rustc_inherit_overflow_checks attributes. The non-division-like operations will never overflow.
* Added track_caller attributes to all methods that can panic. The panic messages will always be correct. For example, division methods all have / before %.
* Edited the saturating_div documentation to be consistent with similar methods.
  • Loading branch information
NCGThompson committed Jan 8, 2024
1 parent 75c68cf commit a0e94a2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
40 changes: 17 additions & 23 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,10 @@ macro_rules! int_impl {
/// Saturating integer division. Computes `self / rhs`, saturating at the
/// numeric bounds instead of overflowing.
///
/// # Panics
///
/// This function will panic if `rhs` is 0.
///
/// # Examples
///
/// Basic usage:
Expand All @@ -1131,11 +1135,6 @@ macro_rules! int_impl {
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
///
/// ```
///
/// ```should_panic
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
///
/// ```
#[stable(feature = "saturating_div", since = "1.58.0")]
#[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
#[must_use = "this returns the result of the operation, \
Expand Down Expand Up @@ -1913,6 +1912,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
if unlikely!(rhs == -1) {
(0, self == Self::MIN)
Expand Down Expand Up @@ -2152,7 +2152,8 @@ macro_rules! int_impl {
///
/// # Panics
///
/// This function will panic if `rhs` is 0 or the division results in overflow.
/// This function will panic if `rhs` is 0 or if `self` is -1 and `rhs` is
/// `Self::MIN`. This behavior is not affected by the `overflow-checks` flag.
///
/// # Examples
///
Expand All @@ -2172,7 +2173,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_inherit_overflow_checks]
#[track_caller]
pub const fn div_euclid(self, rhs: Self) -> Self {
let q = self / rhs;
if self % rhs < 0 {
Expand All @@ -2190,7 +2191,8 @@ macro_rules! int_impl {
///
/// # Panics
///
/// This function will panic if `rhs` is 0 or the division results in overflow.
/// This function will panic if `rhs` is 0 or if `self` is -1 and `rhs` is
/// `Self::MIN`. This behavior is not affected by the `overflow-checks` flag.
///
/// # Examples
///
Expand All @@ -2211,7 +2213,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_inherit_overflow_checks]
#[track_caller]
pub const fn rem_euclid(self, rhs: Self) -> Self {
let r = self % rhs;
if r < 0 {
Expand All @@ -2233,12 +2235,8 @@ macro_rules! int_impl {
///
/// # Panics
///
/// This function will panic if `rhs` is zero.
///
/// ## Overflow behavior
///
/// On overflow, this function will panic if overflow checks are enabled (default in debug
/// mode) and wrap if overflow checks are disabled (default in release mode).
/// This function will panic if `rhs` is 0 or if `self` is -1 and `rhs` is
/// `Self::MIN`. This behavior is not affected by the `overflow-checks` flag.
///
/// # Examples
///
Expand All @@ -2258,7 +2256,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_inherit_overflow_checks]
#[track_caller]
pub const fn div_floor(self, rhs: Self) -> Self {
let d = self / rhs;
let r = self % rhs;
Expand All @@ -2273,12 +2271,8 @@ macro_rules! int_impl {
///
/// # Panics
///
/// This function will panic if `rhs` is zero.
///
/// ## Overflow behavior
///
/// On overflow, this function will panic if overflow checks are enabled (default in debug
/// mode) and wrap if overflow checks are disabled (default in release mode).
/// This function will panic if `rhs` is 0 or if `self` is -1 and `rhs` is
/// `Self::MIN`. This behavior is not affected by the `overflow-checks` flag.
///
/// # Examples
///
Expand All @@ -2298,7 +2292,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_inherit_overflow_checks]
#[track_caller]
pub const fn div_ceil(self, rhs: Self) -> Self {
let d = self / rhs;
let r = self % rhs;
Expand Down
30 changes: 17 additions & 13 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,10 @@ macro_rules! uint_impl {
/// Saturating integer division. Computes `self / rhs`, saturating at the
/// numeric bounds instead of overflowing.
///
/// # Panics
///
/// This function will panic if `rhs` is 0.
///
/// # Examples
///
/// Basic usage:
Expand All @@ -1131,16 +1135,12 @@ macro_rules! uint_impl {
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
///
/// ```
///
/// ```should_panic
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
///
/// ```
#[stable(feature = "saturating_div", since = "1.58.0")]
#[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn saturating_div(self, rhs: Self) -> Self {
// on unsigned types, there is no overflow in integer division
self.wrapping_div(rhs)
Expand Down Expand Up @@ -1275,6 +1275,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[track_caller]
pub const fn wrapping_div(self, rhs: Self) -> Self {
self / rhs
}
Expand Down Expand Up @@ -1304,6 +1305,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[track_caller]
pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
self / rhs
}
Expand Down Expand Up @@ -1331,6 +1333,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[track_caller]
pub const fn wrapping_rem(self, rhs: Self) -> Self {
self % rhs
}
Expand Down Expand Up @@ -1361,6 +1364,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[track_caller]
pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
self % rhs
}
Expand Down Expand Up @@ -1743,6 +1747,7 @@ macro_rules! uint_impl {
#[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[track_caller]
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
(self / rhs, false)
}
Expand Down Expand Up @@ -1773,6 +1778,7 @@ macro_rules! uint_impl {
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[track_caller]
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
(self / rhs, false)
}
Expand Down Expand Up @@ -1800,6 +1806,7 @@ macro_rules! uint_impl {
#[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[track_caller]
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
(self % rhs, false)
}
Expand Down Expand Up @@ -1830,6 +1837,7 @@ macro_rules! uint_impl {
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[track_caller]
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
(self % rhs, false)
}
Expand Down Expand Up @@ -2065,7 +2073,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[rustc_inherit_overflow_checks]
#[track_caller]
pub const fn div_euclid(self, rhs: Self) -> Self {
self / rhs
}
Expand Down Expand Up @@ -2094,7 +2102,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[rustc_inherit_overflow_checks]
#[track_caller]
pub const fn rem_euclid(self, rhs: Self) -> Self {
self % rhs
}
Expand All @@ -2119,6 +2127,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[track_caller]
pub const fn div_floor(self, rhs: Self) -> Self {
self / rhs
}
Expand All @@ -2129,11 +2138,6 @@ macro_rules! uint_impl {
///
/// This function will panic if `rhs` is zero.
///
/// ## Overflow behavior
///
/// On overflow, this function will panic if overflow checks are enabled (default in debug
/// mode) and wrap if overflow checks are disabled (default in release mode).
///
/// # Examples
///
/// Basic usage:
Expand All @@ -2146,7 +2150,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_inherit_overflow_checks]
#[track_caller]
pub const fn div_ceil(self, rhs: Self) -> Self {
let d = self / rhs;
let r = self % rhs;
Expand Down

0 comments on commit a0e94a2

Please sign in to comment.