Skip to content

[Foundation] Collapse (SignedInteger|UnsignedInteger) reqts into FixedWidthInteger #19006

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 4 commits into from
Aug 29, 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
59 changes: 27 additions & 32 deletions stdlib/public/SDK/Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,34 @@ public extension CustomNSError {
}
}

extension CustomNSError where Self: RawRepresentable, Self.RawValue: SignedInteger {
// The error code of Error with integral raw values is the raw value.
public var errorCode: Int {
return numericCast(self.rawValue)
/// Convert an arbitrary binary integer to an Int, reinterpreting signed
/// -> unsigned if needed but trapping if the result is otherwise not
/// expressible.
func unsafeBinaryIntegerToInt<T: BinaryInteger>(_ value: T) -> Int {
if T.isSigned {
return numericCast(value)
}

let uintValue: UInt = numericCast(value)
return Int(bitPattern: uintValue)
}

/// Convert from an Int to an arbitrary binary integer, reinterpreting signed ->
/// unsigned if needed but trapping if the result is otherwise not expressible.
func unsafeBinaryIntegerFromInt<T: BinaryInteger>(_ value: Int) -> T {
if T.isSigned {
return numericCast(value)
}

let uintValue = UInt(bitPattern: value)
return numericCast(uintValue)
}

extension CustomNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
extension CustomNSError
where Self: RawRepresentable, Self.RawValue: FixedWidthInteger {
// The error code of Error with integral raw values is the raw value.
public var errorCode: Int {
return numericCast(self.rawValue)
return unsafeBinaryIntegerToInt(self.rawValue)
}
}

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

public extension Error where Self: CustomNSError, Self: RawRepresentable,
Self.RawValue: SignedInteger {
/// Default implementation for customized NSErrors.
var _code: Int { return self.errorCode }
}

public extension Error where Self: CustomNSError, Self: RawRepresentable,
Self.RawValue: UnsignedInteger {
Self.RawValue: FixedWidthInteger {
/// Default implementation for customized NSErrors.
var _code: Int { return self.errorCode }
}
Expand Down Expand Up @@ -384,7 +395,7 @@ extension _BridgedNSError {
public var _domain: String { return Self._nsErrorDomain }
}

extension _BridgedNSError where Self.RawValue: SignedInteger {
extension _BridgedNSError where Self.RawValue: FixedWidthInteger {
public var _code: Int { return Int(rawValue) }

public init?(_bridgedNSError: NSError) {
Expand All @@ -398,22 +409,6 @@ extension _BridgedNSError where Self.RawValue: SignedInteger {
public var hashValue: Int { return _code }
}

extension _BridgedNSError where Self.RawValue: UnsignedInteger {
public var _code: Int {
return Int(bitPattern: UInt(rawValue))
}

public init?(_bridgedNSError: NSError) {
if _bridgedNSError.domain != Self._nsErrorDomain {
return nil
}

self.init(rawValue: RawValue(UInt(bitPattern: _bridgedNSError.code)))
}

public var hashValue: Int { return _code }
}

/// Describes a bridged error that stores the underlying NSError, so
/// it can be queried.
public protocol _BridgedStoredNSError :
Expand Down Expand Up @@ -446,14 +441,14 @@ internal func _stringDictToAnyHashableDict(_ input: [String : Any])
/// Various helper implementations for _BridgedStoredNSError
extension _BridgedStoredNSError {
public var code: Code {
return Code(rawValue: numericCast(_nsError.code))!
return Code(rawValue: unsafeBinaryIntegerFromInt(_nsError.code))!
}

/// Initialize an error within this domain with the given ``code``
/// and ``userInfo``.
public init(_ code: Code, userInfo: [String : Any] = [:]) {
self.init(_nsError: NSError(domain: Self.errorDomain,
code: numericCast(code.rawValue),
code: unsafeBinaryIntegerToInt(code.rawValue),
userInfo: _stringDictToAnyHashableDict(userInfo)))
}

Expand Down
14 changes: 6 additions & 8 deletions stdlib/public/core/ErrorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,14 @@ extension Error {
}
}

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

extension Error where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
// The error code of Error with integral raw values is the raw value.
public var _code: Int {
return numericCast(self.rawValue)
let uintValue: UInt = numericCast(self.rawValue)
return Int(bitPattern: uintValue)
}
}
19 changes: 18 additions & 1 deletion test/stdlib/Error.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: %target-run-simple-swift
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -o %t/Error -DPTR_SIZE_%target-ptrsize -module-name main %s
// RUN: %target-run %t/Error
// REQUIRES: executable_test

import StdlibUnittest
Expand Down Expand Up @@ -143,5 +145,20 @@ ErrorTests.test("existential in lvalue") {
expectEqual(0, LifetimeTracked.instances)
}

enum UnsignedError: UInt, Error {
#if PTR_SIZE_64
case negativeOne = 0xFFFFFFFFFFFFFFFF
#elseif PTR_SIZE_32
case negativeOne = 0xFFFFFFFF
#else
#error ("Unknown pointer size")
#endif
}

ErrorTests.test("unsigned raw value") {
let negOne: Error = UnsignedError.negativeOne
expectEqual(-1, negOne._code)
}

runAllTests()

13 changes: 12 additions & 1 deletion test/stdlib/ErrorBridged.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: %target-run-simple-swift-swift3
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -o %t/ErrorBridged -DPTR_SIZE_%target-ptrsize -module-name main -swift-version 3 %s
// RUN: %target-run %t/ErrorBridged
// REQUIRES: executable_test
// REQUIRES: objc_interop

Expand Down Expand Up @@ -608,6 +610,14 @@ enum DefaultCustomizedError3 : UInt, CustomNSError {
case bad = 9
case worse = 115

#if PTR_SIZE_64
case dreadful = 0xFFFFFFFFFFFFFFFF
#elseif PTR_SIZE_32
case dreadful = 0xFFFFFFFF
#else
#error ("Unknown pointer size")
#endif

static var errorDomain: String {
return "customized3"
}
Expand All @@ -623,6 +633,7 @@ ErrorBridgingTests.test("Default-customized via CustomNSError") {
expectEqual(1, (DefaultCustomizedError1.worse as NSError).code)
expectEqual(13, (DefaultCustomizedError2.worse as NSError).code)
expectEqual(115, (DefaultCustomizedError3.worse as NSError).code)
expectEqual(-1, (DefaultCustomizedError3.dreadful as NSError).code)
expectEqual("main.DefaultCustomizedError1", (DefaultCustomizedError1.worse as NSError).domain)
expectEqual("customized3", (DefaultCustomizedError3.worse as NSError).domain)
expectEqual("main.DefaultCustomizedParent.ChildError", (DefaultCustomizedParent.ChildError.foo as NSError).domain)
Expand Down
45 changes: 45 additions & 0 deletions validation-test/Reflection/reflect_nested.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -lswiftSwiftReflectionTest %s -o %t/reflect_nested
// RUN: %target-run %target-swift-reflection-test %t/reflect_nested 2>&1 | %FileCheck %s --check-prefix=CHECK-%target-ptrsize
// REQUIRES: objc_interop
// REQUIRES: executable_test

import SwiftReflectionTest

class OuterGeneric<T> {
class Inner {
class Innermost<U> {
var x: T
var y: U

init(x: T, y: U) {
self.x = x
self.y = y
}
}
}
}

var obj = OuterGeneric.Inner.Innermost(x: 17, y: "hello")

reflect(object: obj)

// CHECK-64: Reflecting an object.
// CHECK-64: Instance pointer in child address space: 0x{{[0-9a-fA-F]+}}
// CHECK-64: Type reference:
// CHECK-64: (bound_generic_class reflect_nested.OuterGeneric.Inner.Innermost
// CHECK-64-NEXT: (struct Swift.Int)
// CHECK-64-NEXT: (struct Swift.String)

// CHECK-32: Reflecting an object.
// CHECK-32: Instance pointer in child address space: 0x{{[0-9a-fA-F]+}}
// CHECK-32: Type reference:
// CHECK-32: (bound_generic_class reflect_nested.OuterGeneric.Inner.Innermost
// CHECK-32-NEXT: (struct Swift.Int)
// CHECK-32-NEXT: (struct Swift.String)

doneReflecting()

// CHECK-64: Done.

// CHECK-32: Done.