Skip to content

Commit a7f2878

Browse files
authored
Fixes for FixedWidthInteger.random(in: ClosedRange<Self>, using:) (swiftlang#29633)
Allow the closed-range version of `FixedWidthInteger.random(in:using:)` to work for types larger than 64 bits when the entire valid range (`.min ... .max`) is passed in. Also, closed ranges are never empty, so the unnecessary `!isEmpty` precondition has been removed.
1 parent 8a76358 commit a7f2878

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

stdlib/public/core/Integers.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,11 +2733,6 @@ extension FixedWidthInteger {
27332733
in range: ClosedRange<Self>,
27342734
using generator: inout T
27352735
) -> Self {
2736-
_precondition(
2737-
!range.isEmpty,
2738-
"Can't get random value with an empty range"
2739-
)
2740-
27412736
// Compute delta, the distance between the lower and upper bounds. This
27422737
// value may not representable by the type Bound if Bound is signed, but
27432738
// is always representable as Bound.Magnitude.
@@ -2747,7 +2742,7 @@ extension FixedWidthInteger {
27472742
// If we used &+ instead, the result would be zero, which isn't helpful,
27482743
// so we actually need to handle this case separately.
27492744
if delta == Magnitude.max {
2750-
return Self(truncatingIfNeeded: generator.next())
2745+
return Self(truncatingIfNeeded: generator.next() as Magnitude)
27512746
}
27522747
// Need to widen delta to account for the right-endpoint of a closed range.
27532748
delta += 1

0 commit comments

Comments
 (0)