Skip to content

Commit cf74d83

Browse files
authored
Merge pull request #2959 from spevans/pr_nsnumber_forced_cast
NSNumber: Forced bridging an invalid value would not trap.
2 parents 2570c3b + bd710df commit cf74d83

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

Sources/Foundation/NSNumber.swift

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ extension Int8 : _ObjectiveCBridgeable {
4949
}
5050

5151
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int8?) {
52-
result = _unconditionallyBridgeFromObjectiveC(x)
52+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
53+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
54+
}
5355
}
5456

5557
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int8?) -> Bool {
@@ -87,7 +89,9 @@ extension UInt8 : _ObjectiveCBridgeable {
8789
}
8890

8991
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt8?) {
90-
result = _unconditionallyBridgeFromObjectiveC(x)
92+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
93+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
94+
}
9195
}
9296

9397
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt8?) -> Bool {
@@ -125,7 +129,9 @@ extension Int16 : _ObjectiveCBridgeable {
125129
}
126130

127131
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int16?) {
128-
result = _unconditionallyBridgeFromObjectiveC(x)
132+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
133+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
134+
}
129135
}
130136

131137
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int16?) -> Bool {
@@ -163,7 +169,9 @@ extension UInt16 : _ObjectiveCBridgeable {
163169
}
164170

165171
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt16?) {
166-
result = _unconditionallyBridgeFromObjectiveC(x)
172+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
173+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
174+
}
167175
}
168176

169177
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt16?) -> Bool {
@@ -201,7 +209,9 @@ extension Int32 : _ObjectiveCBridgeable {
201209
}
202210

203211
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int32?) {
204-
result = _unconditionallyBridgeFromObjectiveC(x)
212+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
213+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
214+
}
205215
}
206216

207217
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int32?) -> Bool {
@@ -239,7 +249,9 @@ extension UInt32 : _ObjectiveCBridgeable {
239249
}
240250

241251
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt32?) {
242-
result = _unconditionallyBridgeFromObjectiveC(x)
252+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
253+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
254+
}
243255
}
244256

245257
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt32?) -> Bool {
@@ -277,7 +289,9 @@ extension Int64 : _ObjectiveCBridgeable {
277289
}
278290

279291
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int64?) {
280-
result = _unconditionallyBridgeFromObjectiveC(x)
292+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
293+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
294+
}
281295
}
282296

283297
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int64?) -> Bool {
@@ -315,7 +329,9 @@ extension UInt64 : _ObjectiveCBridgeable {
315329
}
316330

317331
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt64?) {
318-
result = _unconditionallyBridgeFromObjectiveC(x)
332+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
333+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
334+
}
319335
}
320336

321337
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt64?) -> Bool {
@@ -353,7 +369,9 @@ extension Int : _ObjectiveCBridgeable {
353369
}
354370

355371
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int?) {
356-
result = _unconditionallyBridgeFromObjectiveC(x)
372+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
373+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
374+
}
357375
}
358376

359377
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int?) -> Bool {
@@ -391,7 +409,9 @@ extension UInt : _ObjectiveCBridgeable {
391409
}
392410

393411
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt?) {
394-
result = _unconditionallyBridgeFromObjectiveC(x)
412+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
413+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
414+
}
395415
}
396416

397417
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt?) -> Bool {
@@ -437,7 +457,9 @@ extension Float : _ObjectiveCBridgeable {
437457
}
438458

439459
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Float?) {
440-
result = _unconditionallyBridgeFromObjectiveC(x)
460+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
461+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
462+
}
441463
}
442464

443465
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Float?) -> Bool {
@@ -488,7 +510,9 @@ extension Double : _ObjectiveCBridgeable {
488510
}
489511

490512
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Double?) {
491-
result = _unconditionallyBridgeFromObjectiveC(x)
513+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
514+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
515+
}
492516
}
493517

494518
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Double?) -> Bool {
@@ -533,7 +557,9 @@ extension Bool : _ObjectiveCBridgeable {
533557
}
534558

535559
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Bool?) {
536-
result = _unconditionallyBridgeFromObjectiveC(x)
560+
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
561+
fatalError("Unable to bridge \(type(of: x)) to \(self)")
562+
}
537563
}
538564

539565
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Bool?) -> Bool {

0 commit comments

Comments
 (0)