Skip to content

Commit a0e94a2

Browse files
committed
Tweak Library Integer Division Docs
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.
1 parent 75c68cf commit a0e94a2

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

library/core/src/num/int_macros.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,10 @@ macro_rules! int_impl {
11211121
/// Saturating integer division. Computes `self / rhs`, saturating at the
11221122
/// numeric bounds instead of overflowing.
11231123
///
1124+
/// # Panics
1125+
///
1126+
/// This function will panic if `rhs` is 0.
1127+
///
11241128
/// # Examples
11251129
///
11261130
/// Basic usage:
@@ -1131,11 +1135,6 @@ macro_rules! int_impl {
11311135
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
11321136
///
11331137
/// ```
1134-
///
1135-
/// ```should_panic
1136-
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
1137-
///
1138-
/// ```
11391138
#[stable(feature = "saturating_div", since = "1.58.0")]
11401139
#[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
11411140
#[must_use = "this returns the result of the operation, \
@@ -1913,6 +1912,7 @@ macro_rules! int_impl {
19131912
#[must_use = "this returns the result of the operation, \
19141913
without modifying the original"]
19151914
#[inline]
1915+
#[track_caller]
19161916
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
19171917
if unlikely!(rhs == -1) {
19181918
(0, self == Self::MIN)
@@ -2152,7 +2152,8 @@ macro_rules! int_impl {
21522152
///
21532153
/// # Panics
21542154
///
2155-
/// This function will panic if `rhs` is 0 or the division results in overflow.
2155+
/// This function will panic if `rhs` is 0 or if `self` is -1 and `rhs` is
2156+
/// `Self::MIN`. This behavior is not affected by the `overflow-checks` flag.
21562157
///
21572158
/// # Examples
21582159
///
@@ -2172,7 +2173,7 @@ macro_rules! int_impl {
21722173
#[must_use = "this returns the result of the operation, \
21732174
without modifying the original"]
21742175
#[inline]
2175-
#[rustc_inherit_overflow_checks]
2176+
#[track_caller]
21762177
pub const fn div_euclid(self, rhs: Self) -> Self {
21772178
let q = self / rhs;
21782179
if self % rhs < 0 {
@@ -2190,7 +2191,8 @@ macro_rules! int_impl {
21902191
///
21912192
/// # Panics
21922193
///
2193-
/// This function will panic if `rhs` is 0 or the division results in overflow.
2194+
/// This function will panic if `rhs` is 0 or if `self` is -1 and `rhs` is
2195+
/// `Self::MIN`. This behavior is not affected by the `overflow-checks` flag.
21942196
///
21952197
/// # Examples
21962198
///
@@ -2211,7 +2213,7 @@ macro_rules! int_impl {
22112213
#[must_use = "this returns the result of the operation, \
22122214
without modifying the original"]
22132215
#[inline]
2214-
#[rustc_inherit_overflow_checks]
2216+
#[track_caller]
22152217
pub const fn rem_euclid(self, rhs: Self) -> Self {
22162218
let r = self % rhs;
22172219
if r < 0 {
@@ -2233,12 +2235,8 @@ macro_rules! int_impl {
22332235
///
22342236
/// # Panics
22352237
///
2236-
/// This function will panic if `rhs` is zero.
2237-
///
2238-
/// ## Overflow behavior
2239-
///
2240-
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2241-
/// mode) and wrap if overflow checks are disabled (default in release mode).
2238+
/// This function will panic if `rhs` is 0 or if `self` is -1 and `rhs` is
2239+
/// `Self::MIN`. This behavior is not affected by the `overflow-checks` flag.
22422240
///
22432241
/// # Examples
22442242
///
@@ -2258,7 +2256,7 @@ macro_rules! int_impl {
22582256
#[must_use = "this returns the result of the operation, \
22592257
without modifying the original"]
22602258
#[inline]
2261-
#[rustc_inherit_overflow_checks]
2259+
#[track_caller]
22622260
pub const fn div_floor(self, rhs: Self) -> Self {
22632261
let d = self / rhs;
22642262
let r = self % rhs;
@@ -2273,12 +2271,8 @@ macro_rules! int_impl {
22732271
///
22742272
/// # Panics
22752273
///
2276-
/// This function will panic if `rhs` is zero.
2277-
///
2278-
/// ## Overflow behavior
2279-
///
2280-
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2281-
/// mode) and wrap if overflow checks are disabled (default in release mode).
2274+
/// This function will panic if `rhs` is 0 or if `self` is -1 and `rhs` is
2275+
/// `Self::MIN`. This behavior is not affected by the `overflow-checks` flag.
22822276
///
22832277
/// # Examples
22842278
///
@@ -2298,7 +2292,7 @@ macro_rules! int_impl {
22982292
#[must_use = "this returns the result of the operation, \
22992293
without modifying the original"]
23002294
#[inline]
2301-
#[rustc_inherit_overflow_checks]
2295+
#[track_caller]
23022296
pub const fn div_ceil(self, rhs: Self) -> Self {
23032297
let d = self / rhs;
23042298
let r = self % rhs;

library/core/src/num/uint_macros.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,10 @@ macro_rules! uint_impl {
11231123
/// Saturating integer division. Computes `self / rhs`, saturating at the
11241124
/// numeric bounds instead of overflowing.
11251125
///
1126+
/// # Panics
1127+
///
1128+
/// This function will panic if `rhs` is 0.
1129+
///
11261130
/// # Examples
11271131
///
11281132
/// Basic usage:
@@ -1131,16 +1135,12 @@ macro_rules! uint_impl {
11311135
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
11321136
///
11331137
/// ```
1134-
///
1135-
/// ```should_panic
1136-
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
1137-
///
1138-
/// ```
11391138
#[stable(feature = "saturating_div", since = "1.58.0")]
11401139
#[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
11411140
#[must_use = "this returns the result of the operation, \
11421141
without modifying the original"]
11431142
#[inline]
1143+
#[track_caller]
11441144
pub const fn saturating_div(self, rhs: Self) -> Self {
11451145
// on unsigned types, there is no overflow in integer division
11461146
self.wrapping_div(rhs)
@@ -1275,6 +1275,7 @@ macro_rules! uint_impl {
12751275
#[must_use = "this returns the result of the operation, \
12761276
without modifying the original"]
12771277
#[inline(always)]
1278+
#[track_caller]
12781279
pub const fn wrapping_div(self, rhs: Self) -> Self {
12791280
self / rhs
12801281
}
@@ -1304,6 +1305,7 @@ macro_rules! uint_impl {
13041305
#[must_use = "this returns the result of the operation, \
13051306
without modifying the original"]
13061307
#[inline(always)]
1308+
#[track_caller]
13071309
pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
13081310
self / rhs
13091311
}
@@ -1331,6 +1333,7 @@ macro_rules! uint_impl {
13311333
#[must_use = "this returns the result of the operation, \
13321334
without modifying the original"]
13331335
#[inline(always)]
1336+
#[track_caller]
13341337
pub const fn wrapping_rem(self, rhs: Self) -> Self {
13351338
self % rhs
13361339
}
@@ -1361,6 +1364,7 @@ macro_rules! uint_impl {
13611364
#[must_use = "this returns the result of the operation, \
13621365
without modifying the original"]
13631366
#[inline(always)]
1367+
#[track_caller]
13641368
pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
13651369
self % rhs
13661370
}
@@ -1743,6 +1747,7 @@ macro_rules! uint_impl {
17431747
#[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
17441748
#[must_use = "this returns the result of the operation, \
17451749
without modifying the original"]
1750+
#[track_caller]
17461751
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
17471752
(self / rhs, false)
17481753
}
@@ -1773,6 +1778,7 @@ macro_rules! uint_impl {
17731778
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
17741779
#[must_use = "this returns the result of the operation, \
17751780
without modifying the original"]
1781+
#[track_caller]
17761782
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
17771783
(self / rhs, false)
17781784
}
@@ -1800,6 +1806,7 @@ macro_rules! uint_impl {
18001806
#[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
18011807
#[must_use = "this returns the result of the operation, \
18021808
without modifying the original"]
1809+
#[track_caller]
18031810
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
18041811
(self % rhs, false)
18051812
}
@@ -1830,6 +1837,7 @@ macro_rules! uint_impl {
18301837
#[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
18311838
#[must_use = "this returns the result of the operation, \
18321839
without modifying the original"]
1840+
#[track_caller]
18331841
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
18341842
(self % rhs, false)
18351843
}
@@ -2065,7 +2073,7 @@ macro_rules! uint_impl {
20652073
#[must_use = "this returns the result of the operation, \
20662074
without modifying the original"]
20672075
#[inline(always)]
2068-
#[rustc_inherit_overflow_checks]
2076+
#[track_caller]
20692077
pub const fn div_euclid(self, rhs: Self) -> Self {
20702078
self / rhs
20712079
}
@@ -2094,7 +2102,7 @@ macro_rules! uint_impl {
20942102
#[must_use = "this returns the result of the operation, \
20952103
without modifying the original"]
20962104
#[inline(always)]
2097-
#[rustc_inherit_overflow_checks]
2105+
#[track_caller]
20982106
pub const fn rem_euclid(self, rhs: Self) -> Self {
20992107
self % rhs
21002108
}
@@ -2119,6 +2127,7 @@ macro_rules! uint_impl {
21192127
#[must_use = "this returns the result of the operation, \
21202128
without modifying the original"]
21212129
#[inline(always)]
2130+
#[track_caller]
21222131
pub const fn div_floor(self, rhs: Self) -> Self {
21232132
self / rhs
21242133
}
@@ -2129,11 +2138,6 @@ macro_rules! uint_impl {
21292138
///
21302139
/// This function will panic if `rhs` is zero.
21312140
///
2132-
/// ## Overflow behavior
2133-
///
2134-
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2135-
/// mode) and wrap if overflow checks are disabled (default in release mode).
2136-
///
21372141
/// # Examples
21382142
///
21392143
/// Basic usage:
@@ -2146,7 +2150,7 @@ macro_rules! uint_impl {
21462150
#[must_use = "this returns the result of the operation, \
21472151
without modifying the original"]
21482152
#[inline]
2149-
#[rustc_inherit_overflow_checks]
2153+
#[track_caller]
21502154
pub const fn div_ceil(self, rhs: Self) -> Self {
21512155
let d = self / rhs;
21522156
let r = self % rhs;

0 commit comments

Comments
 (0)