Files
swift-mirror/test/decl/var/nan_comparisons.swift
Suyash Srijan 49e7c6126c [Diagnostics] Diagnose comparisons with '.nan' and suggest using '.isNan' instead (#33860)
* [AST] Add 'FloatingPoint' known protocol kind

* [Sema] Emit a diagnostic for comparisons with '.nan' instead of using '.isNan'

* [Sema] Update '.nan' comparison diagnostic wording

* [Sema] Explicitly check for either arguments to be '.nan' and tweak a comment

* [Test] Tweak some comments

* [Diagnostic] Change 'isNan' to 'isNaN'

* [Sema] Fix a bug where firstArg was checked twice for FloatingPoint conformance and update some comments

* [Test] Fix comments in test file

* [NFC] Add a new 'isStandardComparisonOperator' method to 'Identifier' and use it in ConstraintSystem

* [NFC] Reuse argument decl extraction code and switch over to the new 'isStandardComparisonOperator' method

* [NFC] Update conformsToKnownProtocol to accept DeclContext and use it to check for FloatingPoint conformance
2020-09-09 22:08:42 +01:00

32 lines
1.9 KiB
Swift

// RUN: %target-typecheck-verify-swift
//////////////////////////////////////////////////////////////////////////////////////////////////
/////// Comparison with '.nan' static property instead of using '.isNaN' instance property ///////
//////////////////////////////////////////////////////////////////////////////////////////////////
// One side is '.nan' and the other isn't.
// Using '==' or '!=' for comparison should suggest using '.isNaN'.
let double: Double = 0.0
_ = double == .nan // expected-warning {{comparison with '.nan' using '==' is always false, use 'double.isNaN' to check if 'double' is not a number}}
_ = double != .nan // expected-warning {{comparison with '.nan' using '!=' is always true, use '!double.isNaN' to check if 'double' is a number}}
_ = 0.0 == .nan // // expected-warning {{comparison with '.nan' using '==' is always false, use '0.0.isNaN' to check if '0.0' is not a number}}
// One side is '.nan' and the other isn't. Using '>=', '>', '<', '<=' for comparison:
// We can't suggest using '.isNaN' here.
_ = 0.0 >= .nan // expected-warning {{comparison with '.nan' using '>=' is always false}}
_ = .nan > 1.1 // expected-warning {{comparison with '.nan' using '>' is always false}}
_ = .nan < 2.2 // expected-warning {{comparison with '.nan' using '<' is always false}}
_ = 3.3 <= .nan // expected-warning {{comparison with '.nan' using '<=' is always false}}
// Both sides are '.nan':
// We can't suggest using '.isNaN' here.
_ = Double.nan == Double.nan // expected-warning {{'.nan' == '.nan' is always false}}
_ = Double.nan != Double.nan // expected-warning {{'.nan' != '.nan' is always true}}
_ = Double.nan < Double.nan // expected-warning {{'.nan' < '.nan' is always false}}
_ = Double.nan <= Double.nan // expected-warning {{'.nan' <= '.nan' is always false}}
_ = Double.nan > Double.nan // expected-warning {{'.nan' > '.nan' is always false}}
_ = Double.nan >= Double.nan // expected-warning {{'.nan' >= '.nan' is always false}}