@@ -291,17 +291,34 @@ public extension CustomNSError {
291
291
}
292
292
}
293
293
294
- extension CustomNSError where Self: RawRepresentable , Self. RawValue: SignedInteger {
295
- // The error code of Error with integral raw values is the raw value.
296
- public var errorCode : Int {
297
- return numericCast ( self . rawValue)
294
+ /// Convert an arbitrary binary integer to an Int, reinterpreting signed ->
295
+ /// unsigned if needed but trapping if the result is otherwise not
296
+ /// expressible.
297
+ func unsafeBinaryIntegerToInt< T: BinaryInteger > ( _ value: T ) -> Int {
298
+ if T . isSigned {
299
+ return numericCast ( value)
300
+ }
301
+
302
+ let uintValue : UInt = numericCast ( value)
303
+ return Int ( bitPattern: uintValue)
304
+ }
305
+
306
+ /// Convert from an Int to an arbitrary binary integer, reinterpreting signed
307
+ /// -> unsigned if needed but trapping if the result is otherwise not
308
+ /// expressible.
309
+ func unsafeBinaryIntegerFromInt< T: BinaryInteger > ( _ value: Int ) -> T {
310
+ if T . isSigned {
311
+ return numericCast ( value)
298
312
}
313
+
314
+ let uintValue = UInt ( bitPattern: value)
315
+ return numericCast ( uintValue)
299
316
}
300
317
301
- extension CustomNSError where Self: RawRepresentable , Self. RawValue: UnsignedInteger {
318
+ extension CustomNSError where Self: RawRepresentable , Self. RawValue: BinaryInteger {
302
319
// The error code of Error with integral raw values is the raw value.
303
320
public var errorCode : Int {
304
- return numericCast ( self . rawValue)
321
+ return unsafeBinaryIntegerToInt ( self . rawValue)
305
322
}
306
323
}
307
324
@@ -313,12 +330,7 @@ public extension Error where Self : CustomNSError {
313
330
var _code : Int { return self . errorCode }
314
331
}
315
332
316
- public extension Error where Self: CustomNSError , Self: RawRepresentable , Self. RawValue: SignedInteger {
317
- /// Default implementation for customized NSErrors.
318
- var _code : Int { return self . errorCode }
319
- }
320
-
321
- public extension Error where Self: CustomNSError , Self: RawRepresentable , Self. RawValue: UnsignedInteger {
333
+ public extension Error where Self: CustomNSError , Self: RawRepresentable , Self. RawValue: BinaryInteger {
322
334
/// Default implementation for customized NSErrors.
323
335
var _code : Int { return self . errorCode }
324
336
}
@@ -415,42 +427,16 @@ public protocol __BridgedNSError : Error {
415
427
}
416
428
417
429
// Allow two bridged NSError types to be compared.
418
- extension __BridgedNSError where Self: RawRepresentable , Self. RawValue: SignedInteger {
419
- public static func == ( lhs: Self , rhs: Self ) -> Bool {
420
- return lhs. rawValue == rhs. rawValue
421
- }
422
- }
423
-
424
- extension __BridgedNSError where Self: RawRepresentable , Self. RawValue: SignedInteger {
425
- public var _domain : String { return Self . _nsErrorDomain }
426
- public var _code : Int { return Int ( rawValue) }
427
-
428
- public init ? ( rawValue: RawValue ) {
429
- self = unsafeBitCast ( rawValue, to: Self . self)
430
- }
431
-
432
- public init ? ( _bridgedNSError: NSError ) {
433
- if _bridgedNSError. domain != Self . _nsErrorDomain {
434
- return nil
435
- }
436
-
437
- self . init ( rawValue: RawValue ( Int ( _bridgedNSError. code) ) )
438
- }
439
-
440
- public var hashValue : Int { return _code }
441
- }
442
-
443
- // Allow two bridged NSError types to be compared.
444
- extension __BridgedNSError where Self: RawRepresentable , Self. RawValue: UnsignedInteger {
430
+ extension __BridgedNSError where Self: RawRepresentable , Self. RawValue: BinaryInteger {
445
431
public static func == ( lhs: Self , rhs: Self ) -> Bool {
446
432
return lhs. rawValue == rhs. rawValue
447
433
}
448
434
}
449
435
450
- extension __BridgedNSError where Self: RawRepresentable , Self. RawValue: UnsignedInteger {
436
+ extension __BridgedNSError where Self: RawRepresentable , Self. RawValue: BinaryInteger {
451
437
public var _domain : String { return Self . _nsErrorDomain }
452
438
public var _code : Int {
453
- return Int ( bitPattern : UInt ( rawValue) )
439
+ return Int ( rawValue)
454
440
}
455
441
456
442
public init ? ( rawValue: RawValue ) {
@@ -462,7 +448,7 @@ extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: Unsigned
462
448
return nil
463
449
}
464
450
465
- self . init ( rawValue: RawValue ( UInt ( _bridgedNSError. code) ) )
451
+ self . init ( rawValue: RawValue ( Int ( _bridgedNSError. code) ) )
466
452
}
467
453
468
454
public var hashValue : Int { return _code }
@@ -499,17 +485,16 @@ public protocol _BridgedStoredNSError : __BridgedNSError, _ObjectiveCBridgeableE
499
485
}
500
486
501
487
/// Various helper implementations for _BridgedStoredNSError
502
- extension _BridgedStoredNSError where Code: RawRepresentable , Code. RawValue: SignedInteger {
503
- // FIXME: Generalize to Integer.
488
+ extension _BridgedStoredNSError where Code: RawRepresentable , Code. RawValue: BinaryInteger {
504
489
public var code : Code {
505
- return Code ( rawValue: numericCast ( _nsError. code) ) !
490
+ return Code ( rawValue: unsafeBinaryIntegerFromInt ( _nsError. code) ) !
506
491
}
507
492
508
493
/// Initialize an error within this domain with the given ``code``
509
494
/// and ``userInfo``.
510
495
public init ( _ code: Code , userInfo: [ String : Any ] = [ : ] ) {
511
496
self . init ( _nsError: NSError ( domain: Self . _nsErrorDomain,
512
- code: numericCast ( code. rawValue) ,
497
+ code: unsafeBinaryIntegerToInt ( code. rawValue) ,
513
498
userInfo: userInfo) )
514
499
}
515
500
@@ -518,22 +503,6 @@ extension _BridgedStoredNSError where Code: RawRepresentable, Code.RawValue: Sig
518
503
public var userInfo : [ String : Any ] { return errorUserInfo }
519
504
}
520
505
521
- /// Various helper implementations for _BridgedStoredNSError
522
- extension _BridgedStoredNSError where Code: RawRepresentable , Code. RawValue: UnsignedInteger {
523
- // FIXME: Generalize to Integer.
524
- public var code : Code {
525
- return Code ( rawValue: numericCast ( _nsError. code) ) !
526
- }
527
-
528
- /// Initialize an error within this domain with the given ``code``
529
- /// and ``userInfo``.
530
- public init ( _ code: Code , userInfo: [ String : Any ] = [ : ] ) {
531
- self . init ( _nsError: NSError ( domain: Self . _nsErrorDomain,
532
- code: numericCast ( code. rawValue) ,
533
- userInfo: userInfo) )
534
- }
535
- }
536
-
537
506
/// Implementation of __BridgedNSError for all _BridgedStoredNSErrors.
538
507
extension _BridgedStoredNSError {
539
508
/// Default implementation of ``init(_bridgedNSError)`` to provide
@@ -568,20 +537,16 @@ public extension _BridgedStoredNSError {
568
537
/// Describes the code of an error.
569
538
public protocol _ErrorCodeProtocol : Equatable {
570
539
/// The corresponding error code.
571
- associatedtype _ErrorType
572
-
573
- // FIXME: We want _ErrorType to be _BridgedStoredNSError and have its
574
- // Code match Self, but we cannot express those requirements yet.
540
+ associatedtype _ErrorType : _BridgedStoredNSError
541
+ where _ErrorType. Code == Self
575
542
}
576
543
577
- extension _ErrorCodeProtocol where Self . _ErrorType : _BridgedStoredNSError {
544
+ extension _ErrorCodeProtocol {
578
545
/// Allow one to match an error code against an arbitrary error.
579
546
public static func ~= ( match: Self , error: Error ) -> Bool {
580
547
guard let specificError = error as? Self . _ErrorType else { return false }
581
548
582
- // FIXME: Work around IRGen crash when we set Code == Code._ErrorType.Code.
583
- let specificCode = specificError. code as! Self
584
- return match == specificCode
549
+ return match == specificError. code
585
550
}
586
551
}
587
552
0 commit comments