Skip to content

Commit 7b4b264

Browse files
committed
Added tests, moved checks onto operators.
1 parent cf39b60 commit 7b4b264

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

stdlib/public/core/ClosedRange.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,12 @@ extension Comparable {
330330
/// - Parameters:
331331
/// - minimum: The lower bound for the range.
332332
/// - maximum: The upper bound for the range.
333+
///
334+
/// - Precondition: `minimum <= maximum`.
333335
@_transparent
334336
public static func ... (minimum: Self, maximum: Self) -> ClosedRange<Self> {
335337
_precondition(
336-
minimum <= maximum, "Can't form Range with upperBound < lowerBound")
338+
minimum <= maximum, "Range requires lowerBound <= upperBound")
337339
return ClosedRange(uncheckedBounds: (lower: minimum, upper: maximum))
338340
}
339341
}

stdlib/public/core/Range.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,7 @@ public struct PartialRangeThrough<Bound: Comparable> {
529529
public let upperBound: Bound
530530

531531
@inlinable // trivial-implementation
532-
public init(_ upperBound: Bound) {
533-
_precondition(upperBound == upperBound,
534-
"PartialRangeThrough requries that upperBound not be unordered.")
535-
self.upperBound = upperBound
536-
}
532+
public init(_ upperBound: Bound) { self.upperBound = upperBound }
537533
}
538534

539535
extension PartialRangeThrough: RangeExpression {
@@ -648,11 +644,7 @@ public struct PartialRangeFrom<Bound: Comparable> {
648644
public let lowerBound: Bound
649645

650646
@inlinable // trivial-implementation
651-
public init(_ lowerBound: Bound) {
652-
_precondition(lowerBound == lowerBound,
653-
"PartialRangeFrom requries that lowerBound not be unordered.")
654-
self.lowerBound = lowerBound
655-
}
647+
public init(_ lowerBound: Bound) { self.lowerBound = lowerBound }
656648
}
657649

658650
extension PartialRangeFrom: RangeExpression {
@@ -731,6 +723,8 @@ extension Comparable {
731723
/// - Parameters:
732724
/// - minimum: The lower bound for the range.
733725
/// - maximum: The upper bound for the range.
726+
///
727+
/// - Precondition: `minimum <= maximum`.
734728
@_transparent
735729
public static func ..< (minimum: Self, maximum: Self) -> Range<Self> {
736730
_precondition(minimum <= maximum,
@@ -760,8 +754,12 @@ extension Comparable {
760754
/// // Prints "[10, 20, 30]"
761755
///
762756
/// - Parameter maximum: The upper bound for the range.
757+
///
758+
/// - Precondition: `maximum` must compare equal to itself (i.e. cannot be NaN).
763759
@_transparent
764760
public static prefix func ..< (maximum: Self) -> PartialRangeUpTo<Self> {
761+
_precondition(maximum == maximum,
762+
"Range cannot have an unordered upper bound.")
765763
return PartialRangeUpTo(maximum)
766764
}
767765

@@ -787,8 +785,12 @@ extension Comparable {
787785
/// // Prints "[10, 20, 30, 40]"
788786
///
789787
/// - Parameter maximum: The upper bound for the range.
788+
///
789+
/// - Precondition: `maximum` must compare equal to itself (i.e. cannot be NaN).
790790
@_transparent
791791
public static prefix func ... (maximum: Self) -> PartialRangeThrough<Self> {
792+
_precondition(maximum == maximum,
793+
"Range cannot have an unordered upper bound.")
792794
return PartialRangeThrough(maximum)
793795
}
794796

@@ -814,8 +816,12 @@ extension Comparable {
814816
/// // Prints "[40, 50, 60, 70]"
815817
///
816818
/// - Parameter minimum: The lower bound for the range.
819+
///
820+
/// - Precondition: `minimum` must compare equal to itself (i.e. cannot be NaN).
817821
@_transparent
818822
public static postfix func ... (minimum: Self) -> PartialRangeFrom<Self> {
823+
_precondition(minimum == minimum,
824+
"Range cannot have an unordered lower bound.")
819825
return PartialRangeFrom(minimum)
820826
}
821827
}

test/stdlib/RangeTraps.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,47 @@ RangeTraps.test("CountablePartialRangeFrom")
7474
_ = it.next()
7575
}
7676

77+
RangeTraps.test("nanLowerBound")
78+
.code {
79+
expectCrashLater()
80+
_ = Double.nan ... 0
81+
}
82+
83+
RangeTraps.test("nanUpperBound")
84+
.code {
85+
expectCrashLater()
86+
_ = 0 ... Double.nan
87+
}
88+
89+
RangeTraps.test("nanLowerBoundPartial")
90+
.code {
91+
expectCrashLater()
92+
_ = Double.nan ..< 0
93+
}
94+
95+
RangeTraps.test("nanUpperBoundPartial")
96+
.code {
97+
expectCrashLater()
98+
_ = 0 ..< Double.nan
99+
}
77100

101+
RangeTraps.test("fromNaN")
102+
.code {
103+
expectCrashLater()
104+
_ = Double.nan...
105+
}
106+
107+
RangeTraps.test("toNaN")
108+
.code {
109+
expectCrashLater()
110+
_ = ..<Double.nan
111+
}
112+
113+
RangeTraps.test("throughNaN")
114+
.code {
115+
expectCrashLater()
116+
_ = ...Double.nan
117+
}
78118

79119
runAllTests()
80120

0 commit comments

Comments
 (0)