Skip to content

Commit

Permalink
Runtime: Implement convertNSErrorToErrorType and v.v. in Swift.
Browse files Browse the repository at this point in the history
These are compiler hooks that belong in the Foundation overlay; they don't need to be in the core runtime.
  • Loading branch information
jckarter committed Dec 22, 2015
1 parent 0c7ee1f commit e1589a0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 43 deletions.
8 changes: 0 additions & 8 deletions docs/Runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,6 @@ counting error values.

**ABI TODO**: `_n` r/r entry points

### swift\_convertErrorTypeToNSError, swift\_convertNSErrorToErrorType

**ObjC-only**. Standard library entry points used to handle implicit conversions
between `ErrorType` and `NSError`.

**ABI TODO**: These should be implemented as shims or in Swift code, not
in the runtime.

## Initialization

### swift_once
Expand Down
14 changes: 12 additions & 2 deletions stdlib/public/SDK/Foundation/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1044,15 +1044,25 @@ extension CGRectEdge {

public typealias NSErrorPointer = AutoreleasingUnsafeMutablePointer<NSError?>

public // COMPILER_INTRINSIC
let _nilObjCError: ErrorType = _GenericObjCError.NilError

@warn_unused_result
@_silgen_name("swift_convertNSErrorToErrorType")
public // COMPILER_INTRINSIC
func _convertNSErrorToErrorType(error: NSError?) -> ErrorType
func _convertNSErrorToErrorType(error: NSError?) -> ErrorType {
if let error = error {
return error
}
return _nilObjCError
}

@warn_unused_result
@_silgen_name("swift_convertErrorTypeToNSError")
public // COMPILER_INTRINSIC
func _convertErrorTypeToNSError(error: ErrorType) -> NSError
func _convertErrorTypeToNSError(error: ErrorType) -> NSError {
return unsafeDowncast(_bridgeErrorTypeToNSError(error))
}

//===----------------------------------------------------------------------===//
// Variadic initializers and methods
Expand Down
8 changes: 0 additions & 8 deletions stdlib/public/SDK/Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public enum _GenericObjCError : ErrorType {
case NilError
}

/// An intrinsic used by the runtime to create an error when an
/// Objective-C API indicates failure but produces a nil error.
@warn_unused_result
@_silgen_name("swift_allocNilObjCError")
public func _allocNilObjCError() -> ErrorType {
return _GenericObjCError.NilError
}

/// An internal protocol to represent Swift error enums that map to standard
/// Cocoa NSError domains.
public protocol _ObjectiveCBridgeableErrorType : ErrorType {
Expand Down
8 changes: 0 additions & 8 deletions stdlib/public/runtime/ErrorObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,6 @@ extern "C" void swift_unexpectedError(SwiftError *object)
/// Initialize an ErrorType box to make it usable as an NSError instance.
extern "C" id swift_bridgeErrorTypeToNSError(SwiftError *errorObject);

/// Convert an (optional) NSError instance to a (non-optional)
/// ErrorType box.
extern "C" SwiftError *swift_convertNSErrorToErrorType(id errorObject);

/// Convert a (non-optional) ErrorType box to a (non-optional)
/// NSError instance.
extern "C" id swift_convertErrorTypeToNSError(SwiftError *errorObject);

/// Attempt to dynamically cast an NSError instance to a Swift ErrorType
/// implementation using the _ObjectiveCBridgeableErrorType protocol.
///
Expand Down
17 changes: 0 additions & 17 deletions stdlib/public/runtime/ErrorObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,6 @@ static id _swift_bridgeErrorTypeToNSError_(SwiftError *errorObject) {
return _swift_bridgeErrorTypeToNSError(errorObject);
}

SwiftError *
swift::swift_convertNSErrorToErrorType(id errorObject) {
// The fast path is that we have a real error object.
if (errorObject) return reinterpret_cast<SwiftError*>(errorObject);

// Unlike Objective-C, we can't just propagate nil errors around.
auto allocNilError =
(SwiftError*(*)()) dlsym(RTLD_DEFAULT, "swift_allocNilObjCError");
assert(allocNilError && "didn't link Foundation overlay?");
return allocNilError();
}

id swift::swift_convertErrorTypeToNSError(SwiftError *errorObject) {
assert(errorObject && "bridging a nil error!");
return swift_bridgeErrorTypeToNSError(errorObject);
}

bool
swift::tryDynamicCastNSErrorToValue(OpaqueValue *dest,
OpaqueValue *src,
Expand Down

0 comments on commit e1589a0

Please sign in to comment.