Skip to content

Commit 7820ddc

Browse files
committed
[stdlib] Change _withUnsafeGuaranteedRef to use Builtin.convertUnownedUnsafeToGuaranteed.
This builtin (which lowers to raw SIL that doesn't use an actual builtin instruction) allows us to access an unmanaged value at +0 with a language guarantee rather than relying on the optimizer. Previously, we did not do this directly since without OSSA, we were scared that the frontend/optimizer would not be able to safely emit this code. Now that we have ownership ssa, we are able to ensure that the frontend always copies the +0 value passed into the closure if the value +0 escapes from the closure (either via a return, storing into memory, or by passing off as a +1 parameter to a function). rdar://59735604
1 parent dddcfb8 commit 7820ddc

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

stdlib/public/core/Unmanaged.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ public struct Unmanaged<Instance: AnyObject> {
206206
public func _withUnsafeGuaranteedRef<Result>(
207207
_ body: (Instance) throws -> Result
208208
) rethrows -> Result {
209-
let (guaranteedInstance, token) = Builtin.unsafeGuaranteed(_value)
210-
let result = try body(guaranteedInstance)
211-
Builtin.unsafeGuaranteedEnd(token)
212-
return result
209+
var tmp = self
210+
let fakeBase: Int? = nil
211+
return try body(Builtin.convertUnownedUnsafeToGuaranteed(fakeBase,
212+
&tmp._value))
213213
}
214214

215215
/// Performs an unbalanced retain of the object.

0 commit comments

Comments
 (0)