mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Update standard library for id-as-Any.
This commit is contained in:
@@ -593,3 +593,19 @@ func _isOptional<T>(_ type: T.Type) -> Bool {
|
||||
public func unsafeUnwrap<T>(_ nonEmpty: T?) -> T {
|
||||
Builtin.unreachable()
|
||||
}
|
||||
|
||||
/// Extract an object reference from an Any known to contain an object.
|
||||
internal func _unsafeDowncastToAnyObject(fromAny any: Any) -> AnyObject {
|
||||
_sanityCheck(any.dynamicType is AnyObject.Type
|
||||
|| any.dynamicType is AnyObject.Protocol,
|
||||
"Any expected to contain object reference")
|
||||
// With a SIL instruction, we could more efficiently grab the object reference
|
||||
// out of the Any's inline storage.
|
||||
|
||||
// On Linux, bridging isn't supported, so this is a force cast.
|
||||
#if _runtime(_ObjC)
|
||||
return any as AnyObject
|
||||
#else
|
||||
return any as! AnyObject
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -51,9 +51,7 @@ public enum _DebuggerSupport {
|
||||
otherwise: ()->T
|
||||
) -> T {
|
||||
if isClass(value) {
|
||||
if let ao = value as? AnyObject {
|
||||
return ifClass(ao)
|
||||
}
|
||||
return ifClass(_unsafeDowncastToAnyObject(fromAny: value))
|
||||
}
|
||||
return otherwise()
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ import SwiftShims
|
||||
public protocol Error {
|
||||
var _domain: String { get }
|
||||
var _code: Int { get }
|
||||
var _userInfo: AnyObject? { get }
|
||||
var _userInfo: Any? { get }
|
||||
}
|
||||
|
||||
#if _runtime(_ObjC)
|
||||
@@ -135,7 +135,7 @@ public func _stdlib_getErrorCode<T : Error>(_ x: UnsafePointer<T>) -> Int {
|
||||
@_silgen_name("swift_stdlib_getErrorUserInfoNSDictionary")
|
||||
public func _stdlib_getErrorUserInfoNSDictionary<T : Error>(_ x: UnsafePointer<T>)
|
||||
-> AnyObject? {
|
||||
return x.pointee._userInfo
|
||||
return x.pointee._userInfo.map { $0 as AnyObject }
|
||||
}
|
||||
|
||||
@_silgen_name("swift_stdlib_getErrorDefaultUserInfo")
|
||||
@@ -172,7 +172,7 @@ extension Error {
|
||||
return String(reflecting: self.dynamicType)
|
||||
}
|
||||
|
||||
public var _userInfo: AnyObject? {
|
||||
public var _userInfo: Any? {
|
||||
#if _runtime(_ObjC)
|
||||
return _stdlib_getErrorDefaultUserInfo(self)
|
||||
#else
|
||||
|
||||
@@ -166,20 +166,20 @@ public struct Mirror {
|
||||
_ subject: Subject, _ ancestorRepresentation: AncestorRepresentation
|
||||
) -> () -> Mirror? {
|
||||
|
||||
if let subject = subject as? AnyObject,
|
||||
let subjectClass = Subject.self as? AnyClass,
|
||||
if let subjectClass = Subject.self as? AnyClass,
|
||||
let superclass = _getSuperclass(subjectClass) {
|
||||
|
||||
switch ancestorRepresentation {
|
||||
case .generated:
|
||||
return {
|
||||
self._legacyMirror(subject, asClass: superclass).map {
|
||||
self._legacyMirror(_unsafeDowncastToAnyObject(fromAny: subject), asClass: superclass).map {
|
||||
Mirror(legacy: $0, subjectType: superclass)
|
||||
}
|
||||
}
|
||||
case .customized(let makeAncestor):
|
||||
return {
|
||||
Mirror(subject, subjectClass: superclass, ancestor: makeAncestor())
|
||||
Mirror(_unsafeDowncastToAnyObject(fromAny: subject), subjectClass: superclass,
|
||||
ancestor: makeAncestor())
|
||||
}
|
||||
case .suppressed:
|
||||
break
|
||||
|
||||
@@ -19,7 +19,7 @@ func _getObjCCount(_: _MagicMirrorData) -> Int
|
||||
func _getObjCChild<T>(_: Int, _: _MagicMirrorData) -> (T, _Mirror)
|
||||
|
||||
func _getObjCSummary(_ data: _MagicMirrorData) -> String {
|
||||
let theDescription = _swift_stdlib_objcDebugDescription(data._loadValue())
|
||||
let theDescription = _swift_stdlib_objcDebugDescription(data._loadValue(ofType: AnyObject.self)) as AnyObject
|
||||
return _cocoaStringToSwiftString_NonASCII(theDescription)
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ struct _ObjCMirror : _Mirror {
|
||||
public var value: Any { return data.objcValue }
|
||||
public var valueType: Any.Type { return data.objcValueType }
|
||||
public var objectIdentifier: ObjectIdentifier? {
|
||||
return data._loadValue() as ObjectIdentifier
|
||||
return data._loadValue(ofType: ObjectIdentifier.self)
|
||||
}
|
||||
public var count: Int {
|
||||
return _getObjCCount(data)
|
||||
|
||||
@@ -214,10 +214,9 @@ internal func _dump_unlocked<TargetStream : TextOutputStream>(
|
||||
_dumpPrint_unlocked(value, mirror, &target)
|
||||
|
||||
let id: ObjectIdentifier?
|
||||
if let classInstance = value as? AnyObject,
|
||||
value.dynamicType is AnyObject.Type {
|
||||
if value.dynamicType is AnyObject.Type {
|
||||
// Object is a class (but not an ObjC-bridged struct)
|
||||
id = ObjectIdentifier(classInstance)
|
||||
id = ObjectIdentifier(_unsafeDowncastToAnyObject(fromAny: value))
|
||||
} else if let metatypeInstance = value as? Any.Type {
|
||||
// Object is a metatype
|
||||
id = ObjectIdentifier(metatypeInstance)
|
||||
@@ -382,7 +381,7 @@ public struct _MagicMirrorData {
|
||||
return result
|
||||
}
|
||||
|
||||
public func _loadValue<T>() -> T {
|
||||
public func _loadValue<T>(ofType _: T.Type) -> T {
|
||||
return Builtin.load(ptr) as T
|
||||
}
|
||||
}
|
||||
@@ -557,7 +556,7 @@ struct _ClassMirror : _Mirror {
|
||||
var value: Any { return data.value }
|
||||
var valueType: Any.Type { return data.valueType }
|
||||
var objectIdentifier: ObjectIdentifier? {
|
||||
return data._loadValue() as ObjectIdentifier
|
||||
return data._loadValue(ofType: ObjectIdentifier.self)
|
||||
}
|
||||
var count: Int {
|
||||
return _getClassCount(data)
|
||||
@@ -609,7 +608,7 @@ struct _MetatypeMirror : _Mirror {
|
||||
var valueType: Any.Type { return data.valueType }
|
||||
|
||||
var objectIdentifier: ObjectIdentifier? {
|
||||
return data._loadValue() as ObjectIdentifier
|
||||
return data._loadValue(ofType: ObjectIdentifier.self)
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
@@ -619,7 +618,7 @@ struct _MetatypeMirror : _Mirror {
|
||||
_preconditionFailure("no children")
|
||||
}
|
||||
var summary: String {
|
||||
return _typeName(data._loadValue() as Any.Type)
|
||||
return _typeName(data._loadValue(ofType: Any.Type.self))
|
||||
}
|
||||
var quickLookObject: PlaygroundQuickLook? { return nil }
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ public // @testable
|
||||
func _stdlib_binary_CFStringCreateCopy(
|
||||
_ source: _CocoaString
|
||||
) -> _CocoaString {
|
||||
let result = _swift_stdlib_CFStringCreateCopy(nil, source)
|
||||
let result = _swift_stdlib_CFStringCreateCopy(nil, source) as AnyObject
|
||||
Builtin.release(result)
|
||||
return result
|
||||
}
|
||||
@@ -109,9 +109,9 @@ internal func _cocoaStringSlice(
|
||||
_swift_stdlib_CFStringGetCharactersPtr(cfSelf) == nil,
|
||||
"Known contiguously stored strings should already be converted to Swift")
|
||||
|
||||
let cfResult: AnyObject = _swift_stdlib_CFStringCreateWithSubstring(
|
||||
let cfResult = _swift_stdlib_CFStringCreateWithSubstring(
|
||||
nil, cfSelf, _swift_shims_CFRange(
|
||||
location: bounds.lowerBound, length: bounds.count))
|
||||
location: bounds.lowerBound, length: bounds.count)) as AnyObject
|
||||
|
||||
return String(_cocoaString: cfResult)._core
|
||||
}
|
||||
@@ -149,8 +149,8 @@ extension String {
|
||||
// "copy" it into a value to be sure nobody will modify behind
|
||||
// our backs. In practice, when value is already immutable, this
|
||||
// just does a retain.
|
||||
let cfImmutableValue: _swift_shims_CFStringRef
|
||||
= _stdlib_binary_CFStringCreateCopy(_cocoaString)
|
||||
let cfImmutableValue
|
||||
= _stdlib_binary_CFStringCreateCopy(_cocoaString) as AnyObject
|
||||
|
||||
let length = _swift_stdlib_CFStringGetLength(cfImmutableValue)
|
||||
|
||||
@@ -177,7 +177,7 @@ extension String {
|
||||
count: length,
|
||||
elementShift: isUTF16 ? 1 : 0,
|
||||
hasCocoaBuffer: true,
|
||||
owner: unsafeBitCast(cfImmutableValue, to: Optional<AnyObject>.self))
|
||||
owner: cfImmutableValue)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user