Skip to content

Commit 86444a3

Browse files
committed
Swift SIL: don't use Unmanaged to convert between bridged and native SIL objects.
Looking at the SIL I came to the conclusion that `Unmanaged` ins fundamentally broken. It's easier to directly bitcast between bridged and native SIL objects. It also produces simpler SIL which can be further optimized
1 parent 3fca8cd commit 86444a3

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,19 @@ public typealias SwiftObject = UnsafeMutablePointer<BridgedSwiftObject>
120120

121121
extension UnsafeMutablePointer where Pointee == BridgedSwiftObject {
122122
public init<T: AnyObject>(_ object: T) {
123-
let ptr = Unmanaged.passUnretained(object).toOpaque()
123+
let ptr = unsafeBitCast(object, to: UnsafeMutableRawPointer.self)
124124
self = ptr.bindMemory(to: BridgedSwiftObject.self, capacity: 1)
125125
}
126126

127127
public func getAs<T: AnyObject>(_ objectType: T.Type) -> T {
128-
return Unmanaged<T>.fromOpaque(self).takeUnretainedValue()
128+
return unsafeBitCast(self, to: T.self)
129129
}
130130
}
131131

132132
extension Optional where Wrapped == UnsafeMutablePointer<BridgedSwiftObject> {
133133
public func getAs<T: AnyObject>(_ objectType: T.Type) -> T? {
134134
if let pointer = self {
135-
return Unmanaged<T>.fromOpaque(pointer).takeUnretainedValue()
135+
return pointer.getAs(objectType)
136136
}
137137
return nil
138138
}

0 commit comments

Comments
 (0)