Skip to content

Commit 8a9065a

Browse files
authored
Revert " [Foundation] Collapse (SignedInteger|UnsignedInteger) reqts into FixedWidthInteger"
1 parent 3510f08 commit 8a9065a

File tree

5 files changed

+42
-108
lines changed

5 files changed

+42
-108
lines changed

stdlib/public/SDK/Foundation/NSError.swift

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -162,34 +162,17 @@ public extension CustomNSError {
162162
}
163163
}
164164

165-
/// Convert an arbitrary binary integer to an Int, reinterpreting signed
166-
/// -> unsigned if needed but trapping if the result is otherwise not
167-
/// expressible.
168-
func unsafeBinaryIntegerToInt<T: BinaryInteger>(_ value: T) -> Int {
169-
if T.isSigned {
170-
return numericCast(value)
171-
}
172-
173-
let uintValue: UInt = numericCast(value)
174-
return Int(bitPattern: uintValue)
175-
}
176-
177-
/// Convert from an Int to an arbitrary binary integer, reinterpreting signed ->
178-
/// unsigned if needed but trapping if the result is otherwise not expressible.
179-
func unsafeBinaryIntegerFromInt<T: BinaryInteger>(_ value: Int) -> T {
180-
if T.isSigned {
181-
return numericCast(value)
165+
extension CustomNSError where Self: RawRepresentable, Self.RawValue: SignedInteger {
166+
// The error code of Error with integral raw values is the raw value.
167+
public var errorCode: Int {
168+
return numericCast(self.rawValue)
182169
}
183-
184-
let uintValue = UInt(bitPattern: value)
185-
return numericCast(uintValue)
186170
}
187171

188-
extension CustomNSError
189-
where Self: RawRepresentable, Self.RawValue: FixedWidthInteger {
172+
extension CustomNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
190173
// The error code of Error with integral raw values is the raw value.
191174
public var errorCode: Int {
192-
return unsafeBinaryIntegerToInt(self.rawValue)
175+
return numericCast(self.rawValue)
193176
}
194177
}
195178

@@ -202,7 +185,13 @@ public extension Error where Self : CustomNSError {
202185
}
203186

204187
public extension Error where Self: CustomNSError, Self: RawRepresentable,
205-
Self.RawValue: FixedWidthInteger {
188+
Self.RawValue: SignedInteger {
189+
/// Default implementation for customized NSErrors.
190+
var _code: Int { return self.errorCode }
191+
}
192+
193+
public extension Error where Self: CustomNSError, Self: RawRepresentable,
194+
Self.RawValue: UnsignedInteger {
206195
/// Default implementation for customized NSErrors.
207196
var _code: Int { return self.errorCode }
208197
}
@@ -395,7 +384,7 @@ extension _BridgedNSError {
395384
public var _domain: String { return Self._nsErrorDomain }
396385
}
397386

398-
extension _BridgedNSError where Self.RawValue: FixedWidthInteger {
387+
extension _BridgedNSError where Self.RawValue: SignedInteger {
399388
public var _code: Int { return Int(rawValue) }
400389

401390
public init?(_bridgedNSError: NSError) {
@@ -409,6 +398,22 @@ extension _BridgedNSError where Self.RawValue: FixedWidthInteger {
409398
public var hashValue: Int { return _code }
410399
}
411400

401+
extension _BridgedNSError where Self.RawValue: UnsignedInteger {
402+
public var _code: Int {
403+
return Int(bitPattern: UInt(rawValue))
404+
}
405+
406+
public init?(_bridgedNSError: NSError) {
407+
if _bridgedNSError.domain != Self._nsErrorDomain {
408+
return nil
409+
}
410+
411+
self.init(rawValue: RawValue(UInt(bitPattern: _bridgedNSError.code)))
412+
}
413+
414+
public var hashValue: Int { return _code }
415+
}
416+
412417
/// Describes a bridged error that stores the underlying NSError, so
413418
/// it can be queried.
414419
public protocol _BridgedStoredNSError :
@@ -441,14 +446,14 @@ internal func _stringDictToAnyHashableDict(_ input: [String : Any])
441446
/// Various helper implementations for _BridgedStoredNSError
442447
extension _BridgedStoredNSError {
443448
public var code: Code {
444-
return Code(rawValue: unsafeBinaryIntegerFromInt(_nsError.code))!
449+
return Code(rawValue: numericCast(_nsError.code))!
445450
}
446451

447452
/// Initialize an error within this domain with the given ``code``
448453
/// and ``userInfo``.
449454
public init(_ code: Code, userInfo: [String : Any] = [:]) {
450455
self.init(_nsError: NSError(domain: Self.errorDomain,
451-
code: unsafeBinaryIntegerToInt(code.rawValue),
456+
code: numericCast(code.rawValue),
452457
userInfo: _stringDictToAnyHashableDict(userInfo)))
453458
}
454459

stdlib/public/core/ErrorType.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,16 @@ extension Error {
211211
}
212212
}
213213

214-
extension Error where Self: RawRepresentable, Self.RawValue: FixedWidthInteger {
214+
extension Error where Self: RawRepresentable, Self.RawValue: SignedInteger {
215215
// The error code of Error with integral raw values is the raw value.
216216
public var _code: Int {
217-
if Self.RawValue.isSigned {
218-
return numericCast(self.rawValue)
219-
}
217+
return numericCast(self.rawValue)
218+
}
219+
}
220220

221-
let uintValue: UInt = numericCast(self.rawValue)
222-
return Int(bitPattern: uintValue)
221+
extension Error where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
222+
// The error code of Error with integral raw values is the raw value.
223+
public var _code: Int {
224+
return numericCast(self.rawValue)
223225
}
224226
}

test/stdlib/Error.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -o %t/Error -DPTR_SIZE_%target-ptrsize -module-name main %s
3-
// RUN: %target-run %t/Error
1+
// RUN: %target-run-simple-swift
42
// REQUIRES: executable_test
53

64
import StdlibUnittest
@@ -145,20 +143,5 @@ ErrorTests.test("existential in lvalue") {
145143
expectEqual(0, LifetimeTracked.instances)
146144
}
147145

148-
enum UnsignedError: UInt, Error {
149-
#if PTR_SIZE_64
150-
case negativeOne = 0xFFFFFFFFFFFFFFFF
151-
#elseif PTR_SIZE_32
152-
case negativeOne = 0xFFFFFFFF
153-
#else
154-
#error ("Unknown pointer size")
155-
#endif
156-
}
157-
158-
ErrorTests.test("unsigned raw value") {
159-
let negOne: Error = UnsignedError.negativeOne
160-
expectEqual(-1, negOne._code)
161-
}
162-
163146
runAllTests()
164147

test/stdlib/ErrorBridged.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -o %t/ErrorBridged -DPTR_SIZE_%target-ptrsize -module-name main -swift-version 3 %s
3-
// RUN: %target-run %t/ErrorBridged
1+
// RUN: %target-run-simple-swift-swift3
42
// REQUIRES: executable_test
53
// REQUIRES: objc_interop
64

@@ -610,14 +608,6 @@ enum DefaultCustomizedError3 : UInt, CustomNSError {
610608
case bad = 9
611609
case worse = 115
612610

613-
#if PTR_SIZE_64
614-
case dreadful = 0xFFFFFFFFFFFFFFFF
615-
#elseif PTR_SIZE_32
616-
case dreadful = 0xFFFFFFFF
617-
#else
618-
#error ("Unknown pointer size")
619-
#endif
620-
621611
static var errorDomain: String {
622612
return "customized3"
623613
}
@@ -633,7 +623,6 @@ ErrorBridgingTests.test("Default-customized via CustomNSError") {
633623
expectEqual(1, (DefaultCustomizedError1.worse as NSError).code)
634624
expectEqual(13, (DefaultCustomizedError2.worse as NSError).code)
635625
expectEqual(115, (DefaultCustomizedError3.worse as NSError).code)
636-
expectEqual(-1, (DefaultCustomizedError3.dreadful as NSError).code)
637626
expectEqual("main.DefaultCustomizedError1", (DefaultCustomizedError1.worse as NSError).domain)
638627
expectEqual("customized3", (DefaultCustomizedError3.worse as NSError).domain)
639628
expectEqual("main.DefaultCustomizedParent.ChildError", (DefaultCustomizedParent.ChildError.foo as NSError).domain)

validation-test/Reflection/reflect_nested.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)