Skip to content

Remove unsafeAddress(of:) #3644

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 1 commit into from
Jul 27, 2016
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
7 changes: 4 additions & 3 deletions stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ internal func sendBytes<T>(from address: UnsafePointer<T>, count: Int) {
internal func sendAddress(of instance: AnyObject) {
debugLog("BEGIN \(#function)")
defer { debugLog("END \(#function)") }
var address = unsafeAddress(of: instance)
var address = Unmanaged.passUnretained(instance).toOpaque()
sendBytes(from: &address, count: sizeof(UInt.self))
}

Expand Down Expand Up @@ -296,8 +296,9 @@ internal func reflect(instanceAddress: UInt, kind: InstanceKind) {
/// This reflects the stored properties of the immediate class.
/// The superclass is not (yet?) visited.
public func reflect(object: AnyObject) {
let address = unsafeAddress(of: object)
let addressValue = unsafeBitCast(address, to: UInt.self)
defer { _fixLifetime(object) }
var address = Unmanaged.passUnretained(object).toOpaque()
let addressValue = UInt(bitPattern: address)
reflect(instanceAddress: addressValue, kind: .Object)
}

Expand Down
3 changes: 0 additions & 3 deletions stdlib/public/SwiftShims/RuntimeShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ typedef struct Metadata Metadata;
#define bool _Bool
#endif

SWIFT_RUNTIME_EXPORT
bool swift_objc_class_usesNativeSwiftReferenceCounting(const void *);

/// Return an NSString to be used as the Mirror summary of the object
SWIFT_RUNTIME_STDLIB_INTERFACE
void *_swift_objCMirrorSummary(const void * nsObject);
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ extension _ArrayBuffer {
return _native.identity
}
else {
return unsafeAddress(of: _nonNative)
return UnsafeRawPointer(Unmanaged.passUnretained(_nonNative).toOpaque())
}
}

Expand Down
26 changes: 14 additions & 12 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ internal func _isClassOrObjCExistential<T>(_ x: T.Type) -> Bool {
/// Returns an `UnsafePointer` to the storage used for `object`. There's
/// not much you can do with this other than use it to identify the
/// object.
@_transparent
public func unsafeAddress(of object: AnyObject) -> UnsafeRawPointer {
return UnsafeRawPointer(Builtin.bridgeToRawPointer(object))
@available(*, unavailable, message: "Removed in Swift 3. Use Unmanaged.passUnretained(x).toOpaque() instead.")
public func unsafeAddress(of object: AnyObject) -> UnsafePointer<Void> {
Builtin.unreachable()
}

@available(*, unavailable, renamed: "unsafeAddress(of:)")
public func unsafeAddressOf(_ object: AnyObject) -> UnsafeRawPointer {
@available(*, unavailable, message: "Removed in Swift 3. Use Unmanaged.passUnretained(x).toOpaque() instead.")
public func unsafeAddressOf(_ object: AnyObject) -> UnsafePointer<Void> {
Builtin.unreachable()
}

Expand Down Expand Up @@ -293,17 +293,19 @@ public func _onFastPath() {

/// Returns `true` iff the class indicated by `theClass` uses native
/// Swift reference-counting.
@_versioned
@inline(__always)
internal func _usesNativeSwiftReferenceCounting(_ theClass: AnyClass) -> Bool {
#if _runtime(_ObjC)
return swift_objc_class_usesNativeSwiftReferenceCounting(
unsafeAddress(of: theClass)
)
// Declare it here instead of RuntimeShims.h, because we need to specify
// the type of argument to be AnyClass. This is currently not possible
// when using RuntimeShims.h
@_silgen_name("swift_objc_class_usesNativeSwiftReferenceCounting")
func _usesNativeSwiftReferenceCounting(_ theClass: AnyClass) -> Bool
#else
@_versioned
@inline(__always)
func _usesNativeSwiftReferenceCounting(_ theClass: AnyClass) -> Bool {
return true
#endif
}
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gribozavr Is it what you meant? Specially, is it correct to put @_version just before the non _ObjC version?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that's the right way to do it.

@_silgen_name("swift_class_getInstanceExtents")
func swift_class_getInstanceExtents(_ theClass: AnyClass)
Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/runtime/SwiftObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ - (BOOL)isNSValue__ { return NO; }
}

// version for SwiftShims
SWIFT_RUNTIME_EXPORT
extern "C"
bool
swift::swift_objc_class_usesNativeSwiftReferenceCounting(const void *theClass) {
swift_objc_class_usesNativeSwiftReferenceCounting(const void *theClass) {
#if SWIFT_OBJC_INTEROP
return usesNativeSwiftReferenceCounting((const ClassMetadata *)theClass);
#else
Expand Down
3 changes: 2 additions & 1 deletion test/1_stdlib/Renames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func _Arrays<T>(e: T) {
}

func _Builtin(o: AnyObject, oo: AnyObject?) {
_ = unsafeAddressOf(o) // expected-error {{'unsafeAddressOf' has been renamed to 'unsafeAddress(of:)'}} {{7-22=unsafeAddress}} {{23-23=of: }} {{none}}
_ = unsafeAddressOf(o) // expected-error {{Removed in Swift 3. Use Unmanaged.passUnretained(x).toOpaque() instead.}} {{none}}
_ = unsafeAddress(of: o) // expected-error {{Removed in Swift 3. Use Unmanaged.passUnretained(x).toOpaque() instead.}} {{none}}
_ = unsafeUnwrap(oo) // expected-error {{Removed in Swift 3. Please use Optional.unsafelyUnwrapped instead.}} {{none}}
}

Expand Down