Skip to content

Commit 6ef7cca

Browse files
committed
Added decodable error handling and changed from operator constructors to uncheckedBounds constructor
1 parent 15e8fd2 commit 6ef7cca

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

stdlib/public/core/ClosedRange.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,13 @@ extension ClosedRange: Decodable where Bound: Decodable {
494494
var container = try decoder.unkeyedContainer()
495495
let lowerBound = try container.decode(Bound.self)
496496
let upperBound = try container.decode(Bound.self)
497-
self = lowerBound...upperBound
497+
guard lowerBound <= upperBound else {
498+
throw DecodingError.dataCorrupted(
499+
DecodingError.Context(
500+
codingPath: decoder.codingPath,
501+
debugDescription: "Cannot initialize \(ClosedRange.self) with a lowerBound (\(lowerBound)) greater than upperBound (\(upperBound))"))
502+
}
503+
self.init(uncheckedBounds: (lower: lowerBound, upper: upperBound))
498504
}
499505
}
500506

stdlib/public/core/Range.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,13 @@ extension Range: Decodable where Bound: Decodable {
410410
var container = try decoder.unkeyedContainer()
411411
let lowerBound = try container.decode(Bound.self)
412412
let upperBound = try container.decode(Bound.self)
413-
self = lowerBound..<upperBound
413+
guard lowerBound <= upperBound else {
414+
throw DecodingError.dataCorrupted(
415+
DecodingError.Context(
416+
codingPath: decoder.codingPath,
417+
debugDescription: "Cannot initialize \(Range.self) with a lowerBound (\(lowerBound)) greater than upperBound (\(upperBound))"))
418+
}
419+
self.init(uncheckedBounds: (lower: lowerBound, upper: upperBound))
414420
}
415421
}
416422

@@ -467,7 +473,7 @@ extension PartialRangeUpTo: RangeExpression {
467473
extension PartialRangeUpTo: Decodable where Bound: Decodable {
468474
public init(from decoder: Decoder) throws {
469475
var container = try decoder.unkeyedContainer()
470-
self = try (..<container.decode(Bound.self))
476+
try self.init(container.decode(Bound.self))
471477
}
472478
}
473479

@@ -522,7 +528,7 @@ extension PartialRangeThrough: RangeExpression {
522528
extension PartialRangeThrough: Decodable where Bound: Decodable {
523529
public init(from decoder: Decoder) throws {
524530
var container = try decoder.unkeyedContainer()
525-
self = try (...container.decode(Bound.self))
531+
try self.init(container.decode(Bound.self))
526532
}
527533
}
528534

@@ -672,7 +678,7 @@ extension PartialRangeFrom: Sequence
672678
extension PartialRangeFrom: Decodable where Bound: Decodable {
673679
public init(from decoder: Decoder) throws {
674680
var container = try decoder.unkeyedContainer()
675-
self = try container.decode(Bound.self)...
681+
try self.init(container.decode(Bound.self))
676682
}
677683
}
678684

0 commit comments

Comments
 (0)