Skip to content

[stdlib] Remove optional comparison operators (SE-0121) #3637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions stdlib/public/core/Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -380,44 +380,6 @@ public func != <T>(lhs: _OptionalNilComparisonType, rhs: T?) -> Bool {
}
}

public func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}

public func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}

public func <= <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l <= r
default:
return !(rhs < lhs)
}
}

public func >= <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l >= r
default:
return !(lhs < rhs)
}
}

/// Performs a nil-coalescing operation, returning the wrapped value of an
/// `Optional` instance or a default value.
///
Expand Down
1 change: 0 additions & 1 deletion test/1_stdlib/Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func testRelation(_ p: (Int?, Int?) -> Bool) -> [Bool] {
OptionalTests.test("Equatable") {
expectEqual([true, false, false, false, false, true], testRelation(==))
expectEqual([false, true, true, true, true, false], testRelation(!=))
expectEqual([false, true, false, false, true, false], testRelation(<))
}

OptionalTests.test("CustomReflectable") {
Expand Down
2 changes: 1 addition & 1 deletion test/1_stdlib/TestCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class TestCalendar : TestCalendarSuper {
// Find the days numbered '31' after 'd', allowing the algorithm to move to the next day if required
c.enumerateDates(startingAfter: d, matching: DateComponents(day: 31), matchingPolicy: .nextTime) { result, exact, stop in
// Just stop some arbitrary time in the future
if result > d + 86400*365 { stop = true }
if result! > d + 86400*365 { stop = true }
count += 1
if exact { exactCount += 1 }
}
Expand Down
17 changes: 8 additions & 9 deletions test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -787,15 +787,14 @@ func nilComparison(i: Int, o: AnyObject) {
_ = i != nil // expected-warning {{comparing non-optional value of type 'Int' to nil always returns true}}
_ = nil != i // expected-warning {{comparing non-optional value of type 'Int' to nil always returns true}}

// These should all be illegal when SE-0121 lands.
_ = i < nil
_ = nil < i
_ = i <= nil
_ = nil <= i
_ = i > nil
_ = nil > i
_ = i >= nil
_ = nil >= i
_ = i < nil // expected-error {{type 'Int' is not optional, value can never be nil}}
_ = nil < i // expected-error {{type 'Int' is not optional, value can never be nil}}
_ = i <= nil // expected-error {{type 'Int' is not optional, value can never be nil}}
_ = nil <= i // expected-error {{type 'Int' is not optional, value can never be nil}}
_ = i > nil // expected-error {{type 'Int' is not optional, value can never be nil}}
_ = nil > i // expected-error {{type 'Int' is not optional, value can never be nil}}
_ = i >= nil // expected-error {{type 'Int' is not optional, value can never be nil}}
_ = nil >= i // expected-error {{type 'Int' is not optional, value can never be nil}}

_ = o === nil // expected-warning {{comparing non-optional value of type 'AnyObject' to nil always returns false}}
_ = o !== nil // expected-warning {{comparing non-optional value of type 'AnyObject' to nil always returns true}}
Expand Down
8 changes: 4 additions & 4 deletions test/Parse/try.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ i = try? foo() + bar()
i ?+= try? foo() + bar()
i ?+= try? foo() %%%% bar() // expected-error {{'try?' following assignment operator does not cover everything to its right}} // expected-error {{call can throw but is not marked with 'try'}} // expected-warning {{result of operator '%%%%' is unused}}
i ?+= try? foo() %%% bar()
_ = foo() < try? bar() // expected-error {{'try?' cannot appear to the right of a non-assignment operator}} // expected-error {{call can throw but is not marked with 'try'}}
_ = (try? foo()) < bar() // expected-error {{call can throw but is not marked with 'try'}}
_ = foo() < (try? bar()) // expected-error {{call can throw but is not marked with 'try'}}
_ = (try? foo()) < (try? bar())
_ = foo() == try? bar() // expected-error {{'try?' cannot appear to the right of a non-assignment operator}} // expected-error {{call can throw but is not marked with 'try'}}
_ = (try? foo()) == bar() // expected-error {{call can throw but is not marked with 'try'}}
_ = foo() == (try? bar()) // expected-error {{call can throw but is not marked with 'try'}}
_ = (try? foo()) == (try? bar())

let j = true ? try? foo() : try? bar() + 0
let k = true ? try? foo() : try? bar() %%% 0 // expected-error {{'try?' following conditional operator does not cover everything to its right}}
Expand Down