Skip to content

Commit a4d5cad

Browse files
committed
Fix creation of UnsafeRawPointer for found references
1 parent 4b2c6c0 commit a4d5cad

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

stdlib/public/core/RuntimeFunctionCounters.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,27 @@ internal func _collectAllReferencesInsideObjectImpl(
3838
let count = mirror.children.count
3939

4040
let id: ObjectIdentifier?
41-
let toAnyObject: AnyObject?
41+
let ref: UnsafeRawPointer?
4242
if type(of: value) is AnyObject.Type {
4343
// Object is a class (but not an ObjC-bridged struct)
44-
toAnyObject = _unsafeDowncastToAnyObject(fromAny: value)
45-
id = ObjectIdentifier(toAnyObject!)
46-
} else if type(of: value) is Builtin.BridgeObject.Type ||
47-
type(of: value) is Builtin.NativeObject.Type {
48-
toAnyObject = _unsafeDowncastToAnyObject(fromAny: value)
49-
id = ObjectIdentifier(toAnyObject!)
44+
let toAnyObject = _unsafeDowncastToAnyObject(fromAny: value)
45+
ref = UnsafeRawPointer(Unmanaged.passUnretained(toAnyObject).toOpaque())
46+
id = ObjectIdentifier(toAnyObject)
47+
} else if type(of: value) is Builtin.BridgeObject.Type {
48+
ref = UnsafeRawPointer(
49+
Builtin.bridgeToRawPointer(value as! Builtin.BridgeObject))
50+
id = nil
51+
} else if type(of: value) is Builtin.NativeObject.Type {
52+
ref = UnsafeRawPointer(
53+
Builtin.bridgeToRawPointer(value as! Builtin.NativeObject))
54+
id = nil
5055
} else if let metatypeInstance = value as? Any.Type {
5156
// Object is a metatype
5257
id = ObjectIdentifier(metatypeInstance)
53-
toAnyObject = nil
58+
ref = nil
5459
} else {
5560
id = nil
56-
toAnyObject = nil
61+
ref = nil
5762
}
5863

5964
if let theId = id {
@@ -67,9 +72,8 @@ internal func _collectAllReferencesInsideObjectImpl(
6772
}
6873

6974
// If it is a reference, add it to the result.
70-
if let toAnyObject = toAnyObject {
71-
references.append(
72-
UnsafeRawPointer(Unmanaged.passUnretained(toAnyObject).toOpaque()))
75+
if let ref = ref {
76+
references.append(ref)
7377
}
7478

7579
// Recursively visit the children of the current value.

0 commit comments

Comments
 (0)