Skip to content

SR-8407: NSNumber as? Bool should only work for 0 or 1 #1642

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 31, 2018
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
9 changes: 7 additions & 2 deletions Foundation/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,13 @@ extension Bool : _ObjectiveCBridgeable {
}

public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Bool?) -> Bool {
result = x.boolValue
return true
if x.intValue == 0 || x.intValue == 1 {
result = x.boolValue
return true
} else {
result = nil
return false
}
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Bool {
Expand Down
48 changes: 48 additions & 0 deletions TestFoundation/TestNSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TestNSNumber : XCTestCase {
("test_objCType", test_objCType ),
("test_stringValue", test_stringValue),
("test_Equals", test_Equals),
("test_boolValue", test_boolValue),
]
}

Expand Down Expand Up @@ -1214,4 +1215,51 @@ class TestNSNumber : XCTestCase {
XCTAssertEqual(NSNumber(value: Double.greatestFiniteMagnitude).compare(NSNumber(value: 0)), .orderedDescending)
XCTAssertTrue(NSNumber(value: Double(-0.0)) == NSNumber(value: Double(0.0)))
}

func test_boolValue() {
XCTAssertEqual(NSNumber(value: UInt8.max).boolValue, true)
XCTAssertEqual(NSNumber(value: UInt8.min).boolValue, false)

XCTAssertEqual(NSNumber(value: UInt16.max).boolValue, true)
XCTAssertEqual(NSNumber(value: UInt16.min).boolValue, false)

XCTAssertEqual(NSNumber(value: UInt32.max).boolValue, true)
XCTAssertEqual(NSNumber(value: UInt32.min).boolValue, false)

XCTAssertEqual(NSNumber(value: UInt64.max).boolValue, true)
XCTAssertEqual(NSNumber(value: UInt64.min).boolValue, false)

XCTAssertEqual(NSNumber(value: UInt.max).boolValue, true)
XCTAssertEqual(NSNumber(value: UInt.min).boolValue, false)

XCTAssertEqual(NSNumber(value: Int8.max).boolValue, true)
XCTAssertEqual(NSNumber(value: Int8.max - 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int8.min).boolValue, true)
XCTAssertEqual(NSNumber(value: Int8.min + 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int8(-1)).boolValue, true)

XCTAssertEqual(NSNumber(value: Int16.max).boolValue, true)
XCTAssertEqual(NSNumber(value: Int16.max - 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int16.min).boolValue, true)
XCTAssertEqual(NSNumber(value: Int16.min + 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int16(-1)).boolValue, true)

XCTAssertEqual(NSNumber(value: Int32.max).boolValue, true)
XCTAssertEqual(NSNumber(value: Int32.max - 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int32.min).boolValue, true)
XCTAssertEqual(NSNumber(value: Int32.min + 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int32(-1)).boolValue, true)

XCTAssertEqual(NSNumber(value: Int64.max).boolValue, true)
XCTAssertEqual(NSNumber(value: Int64.max - 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int64.min).boolValue, false) // Darwin compatibility
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is no longer the case, we finally fixed that in High Sierra.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? I tested on HS 10.13.6:

print(NSNumber(value: Int64.min).boolValue)
false

XCTAssertEqual(NSNumber(value: Int64.min + 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int64(-1)).boolValue, true)

XCTAssertEqual(NSNumber(value: Int.max).boolValue, true)
XCTAssertEqual(NSNumber(value: Int.max - 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int.min).boolValue, false) // Darwin compatibility
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto here

XCTAssertEqual(NSNumber(value: Int.min + 1).boolValue, true)
XCTAssertEqual(NSNumber(value: Int(-1)).boolValue, true)
}
}
32 changes: 32 additions & 0 deletions TestFoundation/TestNSNumberBridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TestNSNumberBridging : XCTestCase {
("testNSNumberBridgeFromDouble", testNSNumberBridgeFromDouble),
("test_numericBitPatterns_to_floatingPointTypes", test_numericBitPatterns_to_floatingPointTypes),
("testNSNumberBridgeAnyHashable", testNSNumberBridgeAnyHashable),
("testNSNumberToBool", testNSNumberToBool),
]
}

Expand Down Expand Up @@ -623,6 +624,37 @@ class TestNSNumberBridging : XCTestCase {
XCTAssertEqual(value, ns_value)
}
}

func testNSNumberToBool() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is correct behavior, thanks for the update!

let b0 = NSNumber(value: 0) as? Bool
XCTAssertNotNil(b0)
XCTAssertEqual(b0, false)

let b1 = NSNumber(value: false) as? Bool
XCTAssertNotNil(b1)
XCTAssertEqual(b1, false)

let b2 = NSNumber(value: 1) as? Bool
XCTAssertNotNil(b2)
XCTAssertEqual(b2, true)

let b3 = NSNumber(value: true) as? Bool
XCTAssertNotNil(b3)
XCTAssertEqual(b3, true)

XCTAssertNil(NSNumber(value: -1) as? Bool)
XCTAssertNil(NSNumber(value: 2) as? Bool)
XCTAssertNil(NSNumber(value: Int8.min) as? Bool)
XCTAssertNil(NSNumber(value: Int8.max) as? Bool)
XCTAssertNil(NSNumber(value: Int16.min) as? Bool)
XCTAssertNil(NSNumber(value: Int16.max) as? Bool)
XCTAssertNil(NSNumber(value: Int32.min) as? Bool)
XCTAssertNil(NSNumber(value: Int32.max) as? Bool)
XCTAssertNil(NSNumber(value: Int64.min) as? Bool)
XCTAssertNil(NSNumber(value: Int64.max) as? Bool)
XCTAssertNil(NSNumber(value: Int.min) as? Bool)
XCTAssertNil(NSNumber(value: Int.max) as? Bool)
}
}

extension Float {
Expand Down