Swift 2.1: Unexpected compiler errors when comparing non-optionals to nil literals

Originator:Karoly.Lorentey
Number:rdar://23223652 Date Originated:22-Oct-2015 10:03 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 7.1 (7B91b)
Classification:Other Bug Reproducible:Always
 
Summary:
Playing with the predefined comparison operators for optionals, I noticed the following strange behavior in Swift 2.1:

let a = 42
let b: Int? = nil

42 < nil    ==> false
nil > 42    ==> false
42 + 0 < nil    ==> error: value of type 'Int' can never be nil, comparison isn't allowed
nil > 42 + 0    ==> false
42 < b    ==> false
b > 42    ==> false
a < b    ==> false
b > a    ==> false
a < nil    ==> error: value of type 'Int' can never be nil, comparison isn't allowed
nil > a    ==> error: type of expression is ambiguous without more context
-a < nil    ==> false
nil > -a    ==> false
a + 0 < nil    ==> error: ambiguous use of operator '+'
nil > a + 0    ==> false
Double.infinity < nil    ==> error: value of type ’Double’ can never be nil, comparison isn't allowed
-Double.infinity < nil    ==> false

I’m now very confused.

Steps to Reproduce:

Evaluate the above expressions using “xcrun swift” or in an Xcode playground.

Expected Results:
All expressions compile cleanly (or, even better, none of them do).

Actual Results:
Some of the expressions result in compiler errors. This is highly unintuitive.

Regression:
Unknown

Notes:

$ swift
Welcome to Apple Swift version 2.1 (700.1.101.6 700.1.76). Type :help for assistance.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!