@@ -366,93 +366,59 @@ public func _bridgeNSErrorToError<
366
366
}
367
367
}
368
368
369
- /// Helper protocol for _BridgedNSError, which used to provide
370
- /// default implementations.
371
- public protocol __BridgedNSError : Error {
369
+ /// Describes a raw representable type that is bridged to a particular
370
+ /// NSError domain.
371
+ ///
372
+ /// This protocol is used primarily to generate the conformance to
373
+ /// _ObjectiveCBridgeableError for such an enum defined in Swift.
374
+ public protocol _BridgedNSError :
375
+ _ObjectiveCBridgeableError , RawRepresentable , Hashable
376
+ where Self. RawValue: FixedWidthInteger {
377
+ /// The NSError domain to which this type is bridged.
372
378
static var _nsErrorDomain : String { get }
373
379
}
374
380
375
- // Allow two bridged NSError types to be compared.
376
- extension __BridgedNSError
377
- where Self: RawRepresentable , Self. RawValue: SignedInteger {
378
- public static func == ( lhs: Self , rhs: Self ) -> Bool {
379
- return lhs. rawValue == rhs. rawValue
380
- }
381
+ extension _BridgedNSError {
382
+ public var _domain : String { return Self . _nsErrorDomain }
381
383
}
382
384
383
- public extension __BridgedNSError
384
- where Self: RawRepresentable , Self. RawValue: SignedInteger {
385
- public var _domain : String { return Self . _nsErrorDomain }
385
+ extension _BridgedNSError where Self. RawValue: SignedInteger {
386
386
public var _code : Int { return Int ( rawValue) }
387
387
388
- public init ? ( rawValue: RawValue ) {
389
- self = unsafeBitCast ( rawValue, to: Self . self)
390
- }
391
-
392
388
public init ? ( _bridgedNSError: NSError ) {
393
389
if _bridgedNSError. domain != Self . _nsErrorDomain {
394
390
return nil
395
391
}
396
392
397
- self . init ( rawValue: RawValue ( IntMax ( _bridgedNSError. code) ) )
393
+ self . init ( rawValue: RawValue ( _bridgedNSError. code) )
398
394
}
399
395
400
396
public var hashValue : Int { return _code }
401
397
}
402
398
403
- // Allow two bridged NSError types to be compared.
404
- extension __BridgedNSError
405
- where Self: RawRepresentable , Self. RawValue: UnsignedInteger {
406
- public static func == ( lhs: Self , rhs: Self ) -> Bool {
407
- return lhs. rawValue == rhs. rawValue
408
- }
409
- }
410
-
411
- public extension __BridgedNSError
412
- where Self: RawRepresentable , Self. RawValue: UnsignedInteger {
413
- public var _domain : String { return Self . _nsErrorDomain }
399
+ extension _BridgedNSError where Self. RawValue: UnsignedInteger {
414
400
public var _code : Int {
415
401
return Int ( bitPattern: UInt ( rawValue) )
416
402
}
417
403
418
- public init ? ( rawValue: RawValue ) {
419
- self = unsafeBitCast ( rawValue, to: Self . self)
420
- }
421
-
422
404
public init ? ( _bridgedNSError: NSError ) {
423
405
if _bridgedNSError. domain != Self . _nsErrorDomain {
424
406
return nil
425
407
}
426
408
427
- self . init ( rawValue: RawValue ( UIntMax ( UInt ( _bridgedNSError. code) ) ) )
409
+ self . init ( rawValue: RawValue ( UInt ( bitPattern : _bridgedNSError. code) ) )
428
410
}
429
411
430
412
public var hashValue : Int { return _code }
431
413
}
432
414
433
- /// Describes a raw representable type that is bridged to a particular
434
- /// NSError domain.
435
- ///
436
- /// This protocol is used primarily to generate the conformance to
437
- /// _ObjectiveCBridgeableError for such an enum.
438
- public protocol _BridgedNSError : __BridgedNSError ,
439
- RawRepresentable ,
440
- _ObjectiveCBridgeableError ,
441
- Hashable {
442
- /// The NSError domain to which this type is bridged.
443
- static var _nsErrorDomain : String { get }
444
- }
445
-
446
415
/// Describes a bridged error that stores the underlying NSError, so
447
416
/// it can be queried.
448
417
public protocol _BridgedStoredNSError :
449
- __BridgedNSError , _ObjectiveCBridgeableError , CustomNSError ,
450
- Hashable {
418
+ _ObjectiveCBridgeableError , CustomNSError , Hashable {
451
419
/// The type of an error code.
452
- associatedtype Code : _ErrorCodeProtocol
453
-
454
- /// The error code for the given error.
455
- var code : Code { get }
420
+ associatedtype Code : _ErrorCodeProtocol , RawRepresentable
421
+ where Code. RawValue: FixedWidthInteger
456
422
457
423
//// Retrieves the embedded NSError.
458
424
var _nsError : NSError { get }
@@ -476,49 +442,30 @@ internal func _stringDictToAnyHashableDict(_ input: [String : Any])
476
442
}
477
443
478
444
/// Various helper implementations for _BridgedStoredNSError
479
- public extension _BridgedStoredNSError
480
- where Code: RawRepresentable , Code. RawValue: SignedInteger {
481
- // FIXME: Generalize to Integer.
445
+ extension _BridgedStoredNSError {
482
446
public var code : Code {
483
447
return Code ( rawValue: numericCast ( _nsError. code) ) !
484
448
}
485
449
486
450
/// Initialize an error within this domain with the given ``code``
487
451
/// and ``userInfo``.
488
452
public init ( _ code: Code , userInfo: [ String : Any ] = [ : ] ) {
489
- self . init ( _nsError: NSError ( domain: Self . _nsErrorDomain ,
453
+ self . init ( _nsError: NSError ( domain: Self . errorDomain ,
490
454
code: numericCast ( code. rawValue) ,
491
455
userInfo: _stringDictToAnyHashableDict ( userInfo) ) )
492
456
}
493
457
494
458
/// The user-info dictionary for an error that was bridged from
495
459
/// NSError.
496
- var userInfo : [ String : Any ] { return errorUserInfo }
497
- }
498
-
499
- /// Various helper implementations for _BridgedStoredNSError
500
- public extension _BridgedStoredNSError
501
- where Code: RawRepresentable , Code. RawValue: UnsignedInteger {
502
- // FIXME: Generalize to Integer.
503
- public var code : Code {
504
- return Code ( rawValue: numericCast ( _nsError. code) ) !
505
- }
506
-
507
- /// Initialize an error within this domain with the given ``code``
508
- /// and ``userInfo``.
509
- public init ( _ code: Code , userInfo: [ String : Any ] = [ : ] ) {
510
- self . init ( _nsError: NSError ( domain: Self . _nsErrorDomain,
511
- code: numericCast ( code. rawValue) ,
512
- userInfo: _stringDictToAnyHashableDict ( userInfo) ) )
513
- }
460
+ public var userInfo : [ String : Any ] { return errorUserInfo }
514
461
}
515
462
516
- /// Implementation of __BridgedNSError for all _BridgedStoredNSErrors.
463
+ /// Implementation of _ObjectiveCBridgeableError for all _BridgedStoredNSErrors.
517
464
public extension _BridgedStoredNSError {
518
- /// Default implementation of ``init(_bridgedNSError)`` to provide
465
+ /// Default implementation of ``init(_bridgedNSError: )`` to provide
519
466
/// bridging from NSError.
520
467
public init ? ( _bridgedNSError error: NSError ) {
521
- if error. domain != Self . _nsErrorDomain {
468
+ if error. domain != Self . errorDomain {
522
469
return nil
523
470
}
524
471
@@ -531,7 +478,8 @@ public extension _BridgedStoredNSError {
531
478
// FIXME: Would prefer to have a clear "extract an NSError
532
479
// directly" operation.
533
480
534
- static var errorDomain : String { return _nsErrorDomain }
481
+ // Synthesized by the compiler.
482
+ // static var errorDomain: String
535
483
536
484
var errorCode : Int { return _nsError. code }
537
485
@@ -555,20 +503,15 @@ public extension _BridgedStoredNSError {
555
503
/// Describes the code of an error.
556
504
public protocol _ErrorCodeProtocol : Equatable {
557
505
/// The corresponding error code.
558
- associatedtype _ErrorType
559
-
560
- // FIXME: We want _ErrorType to be _BridgedStoredNSError and have its
561
- // Code match Self, but we cannot express those requirements yet.
506
+ associatedtype _ErrorType : _BridgedStoredNSError where _ErrorType. Code == Self
562
507
}
563
508
564
- extension _ErrorCodeProtocol where Self . _ErrorType : _BridgedStoredNSError {
509
+ extension _ErrorCodeProtocol {
565
510
/// Allow one to match an error code against an arbitrary error.
566
511
public static func ~= ( match: Self , error: Error ) -> Bool {
567
512
guard let specificError = error as? Self . _ErrorType else { return false }
568
513
569
- // FIXME: Work around IRGen crash when we set Code == Code._ErrorType.Code.
570
- let specificCode = specificError. code as! Self
571
- return match == specificCode
514
+ return match == specificError. code
572
515
}
573
516
}
574
517
@@ -595,7 +538,7 @@ public struct CocoaError : _BridgedStoredNSError {
595
538
self . _nsError = error
596
539
}
597
540
598
- public static var _nsErrorDomain : String { return NSCocoaErrorDomain }
541
+ public static var errorDomain : String { return NSCocoaErrorDomain }
599
542
600
543
/// The error code itself.
601
544
public struct Code : RawRepresentable , Hashable , _ErrorCodeProtocol {
@@ -1826,7 +1769,7 @@ public struct URLError : _BridgedStoredNSError {
1826
1769
self . _nsError = error
1827
1770
}
1828
1771
1829
- public static var _nsErrorDomain : String { return NSURLErrorDomain }
1772
+ public static var errorDomain : String { return NSURLErrorDomain }
1830
1773
1831
1774
/// The error code itself.
1832
1775
public struct Code : RawRepresentable , Hashable , _ErrorCodeProtocol {
@@ -2486,7 +2429,7 @@ public struct POSIXError : _BridgedStoredNSError {
2486
2429
self . _nsError = error
2487
2430
}
2488
2431
2489
- public static var _nsErrorDomain : String { return NSPOSIXErrorDomain }
2432
+ public static var errorDomain : String { return NSPOSIXErrorDomain }
2490
2433
2491
2434
public typealias Code = POSIXErrorCode
2492
2435
}
@@ -2972,7 +2915,7 @@ public struct MachError : _BridgedStoredNSError {
2972
2915
self . _nsError = error
2973
2916
}
2974
2917
2975
- public static var _nsErrorDomain : String { return NSMachErrorDomain }
2918
+ public static var errorDomain : String { return NSMachErrorDomain }
2976
2919
2977
2920
public typealias Code = MachErrorCode
2978
2921
}
0 commit comments