Skip to content

Commit 6af4863

Browse files
committed
Improve comments for assert_unchecked calls
1 parent 6afbf14 commit 6af4863

File tree

4 files changed

+26
-45
lines changed

4 files changed

+26
-45
lines changed

library/core/src/num/int_macros.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -1584,31 +1584,24 @@ macro_rules! int_impl {
15841584
crate::num::int_sqrt::$ActualT(self as $ActualT) as $SelfT
15851585
};
15861586

1587-
// SAFETY: Inform the optimizer what the range of outputs is.
1588-
// If testing `core` crashes with no panic message and a
1587+
// Inform the optimizer what the range of outputs is. If
1588+
// testing `core` crashes with no panic message and a
15891589
// `num::int_sqrt::i*` test failed, it's because your edits
15901590
// caused these assertions to become false.
15911591
//
1592-
// Integer square root is a monotonically nondecreasing
1592+
// SAFETY: Integer square root is a monotonically nondecreasing
15931593
// function, which means that increasing the input will never
1594-
// cause the output to decrease.
1595-
//
1596-
// The minimum input in this `else` branch is 0. The maximum
1597-
// input is `<$ActualT>::MAX`.
1598-
//
1599-
// When n is 0, sqrt(n) is 0. If n increases above 0, sqrt(n)
1600-
// can't decrease below 0, so sqrt(n) can't decrease below 0 no
1601-
// matter what n is.
1602-
//
1603-
// When n is below `<$ActualT>::MAX`, sqrt(n) can't decrease at
1604-
// all when you increase n to `<$ActualT>::MAX`, so sqrt(n)
1605-
// can't be above sqrt(`<$ActualT>::MAX`) no matter what n is.
1594+
// cause the output to decrease. Thus, since the input for
1595+
// nonnegative signed integers is bounded by
1596+
// `[0, <$ActualT>::MAX]`, sqrt(n) will be bounded by
1597+
// `[sqrt(0), sqrt(<$ActualT>::MAX)]`.
16061598
unsafe {
1607-
crate::hint::assert_unchecked(result >= 0);
16081599
// SAFETY: `<$ActualT>::MAX` is nonnegative.
16091600
const MAX_RESULT: $SelfT = unsafe {
16101601
crate::num::int_sqrt::$ActualT(<$ActualT>::MAX) as $SelfT
16111602
};
1603+
1604+
crate::hint::assert_unchecked(result >= 0);
16121605
crate::hint::assert_unchecked(result <= MAX_RESULT);
16131606
}
16141607

library/core/src/num/int_sqrt.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,13 @@ macro_rules! first_stage {
171171
/// Generates a middle stage of the computation.
172172
macro_rules! middle_stage {
173173
($original_bits:literal, $ty:ty, $n:ident, $s:ident, $r:ident) => {{
174-
// SAFETY: Inform the optimizer that `$s` is nonzero. This will allow
175-
// it to avoid generating code to handle division-by-zero panics in the
174+
// Inform the optimizer that `$s` is nonzero. This will allow it to
175+
// avoid generating code to handle division-by-zero panics in the
176176
// divisions below.
177177
//
178-
// If the original `$n` is zero, the top of the `unsigned_fn` macro
179-
// recurses instead of continuing to this point, so the original `$n`
180-
// wasn't a 0 if we've reached here.
178+
// SAFETY: If the original `$n` is zero, the top of the `unsigned_fn`
179+
// macro recurses instead of continuing to this point, so the original
180+
// `$n` wasn't a 0 if we've reached here.
181181
//
182182
// Then the `unsigned_fn` macro normalizes `$n` so that at least one of
183183
// the two most-significant bits is a 1.
@@ -221,11 +221,11 @@ macro_rules! middle_stage {
221221
/// Generates the last stage of the computation before denormalization.
222222
macro_rules! last_stage {
223223
($ty:ty, $n:ident, $s:ident, $r:ident) => {{
224-
// SAFETY: Inform the optimizer that `$s` is nonzero. This will allow
225-
// it to avoid generating code to handle division-by-zero panics in the
224+
// Inform the optimizer that `$s` is nonzero. This will allow it to
225+
// avoid generating code to handle division-by-zero panics in the
226226
// division below.
227227
//
228-
// See the proof in the `middle_stage` macro above.
228+
// SAFETY: See the proof in the `middle_stage` macro above.
229229
unsafe { core::hint::assert_unchecked($s != 0) };
230230

231231
const HALF_BITS: u32 = <$ty>::BITS >> 1;

library/core/src/num/nonzero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
15541554
// function, which means that increasing the input will never cause
15551555
// the output to decrease. Thus, since the input for nonzero
15561556
// unsigned integers has a lower bound of 1, the lower bound of the
1557-
// result will be sqrt(1), which is 1.
1557+
// results will be sqrt(1), which is 1.
15581558
unsafe {
15591559
hint::assert_unchecked(result >= 1);
15601560
}

library/core/src/num/uint_macros.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -2708,28 +2708,16 @@ macro_rules! uint_impl {
27082708
pub const fn isqrt(self) -> Self {
27092709
let result = crate::num::int_sqrt::$ActualT(self as $ActualT) as $SelfT;
27102710

2711-
// SAFETY: Inform the optimizer what the range of outputs is.
2712-
// If testing `core` crashes with no panic message and a
2713-
// `num::int_sqrt::u*` test failed, it's because your edits
2714-
// caused these assertions or the assertions in `fn isqrt` of
2715-
// `nonzero.rs` to become false.
2711+
// Inform the optimizer what the range of outputs is. If testing
2712+
// `core` crashes with no panic message and a `num::int_sqrt::u*`
2713+
// test failed, it's because your edits caused these assertions or
2714+
// the assertions in `fn isqrt` of `nonzero.rs` to become false.
27162715
//
2717-
// Integer square root is a monotonically nondecreasing
2716+
// SAFETY: Integer square root is a monotonically nondecreasing
27182717
// function, which means that increasing the input will never
2719-
// cause the output to decrease.
2720-
//
2721-
// The minimum input is 0. The maximum input is `<$ActualT>::MAX`.
2722-
//
2723-
// When n is 0, sqrt(n) is 0. If n increases above 0, sqrt(n)
2724-
// can't decrease below 0, so sqrt(n) can't decrease below 0 no
2725-
// matter what n is.
2726-
//
2727-
// However, the optimizer already knows that zero is the minimum
2728-
// value of all unsigned integer types, so we won't mention that.
2729-
//
2730-
// When n is below `<$ActualT>::MAX`, sqrt(n) can't decrease at
2731-
// all when you increase n to `<$ActualT>::MAX`, so sqrt(n)
2732-
// can't be above sqrt(`<$ActualT>::MAX`) no matter what n is.
2718+
// cause the output to decrease. Thus, since the input for unsigned
2719+
// integers is bounded by `[0, <$ActualT>::MAX]`, sqrt(n) will be
2720+
// bounded by `[sqrt(0), sqrt(<$ActualT>::MAX)]`.
27332721
unsafe {
27342722
const MAX_RESULT: $SelfT = crate::num::int_sqrt::$ActualT(<$ActualT>::MAX) as $SelfT;
27352723
crate::hint::assert_unchecked(result <= MAX_RESULT);

0 commit comments

Comments
 (0)