Skip to content

Commit df655e8

Browse files
committed
[stdlib] Add unsafeLifetime APIs
Unsafely discard any lifetime dependence on the `dependent` argument. Return a value identical to `dependent` with a new lifetime dependence on the `borrows` argument. This is required to enable lifetime enforcement in the standard library build.
1 parent 3542af9 commit df655e8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

stdlib/public/core/LifetimeManager.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,40 @@ extension String {
283283
public func _copy<T>(_ value: T) -> T {
284284
copy value
285285
}
286+
287+
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
288+
/// a value identical to `dependent` with a lifetime dependency on the caller's
289+
/// borrow scope of the `borrows` argument.
290+
///
291+
/// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
292+
/// should be expressed by a builtin that is hidden within the function body.
293+
@_unsafeNonescapableResult
294+
@_alwaysEmitIntoClient
295+
@inline(__always)
296+
@lifetime(borrow source)
297+
internal func _unsafeLifetime<T: ~Copyable & ~Escapable,
298+
U: ~Copyable & ~Escapable>(
299+
dependent: consuming T, borrows source: borrowing U
300+
) -> T {
301+
dependent
302+
}
303+
304+
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
305+
/// a value identical to `dependent` that inherits all lifetime dependencies from
306+
/// the `dependsOn` argument.
307+
///
308+
/// Transferring generic lifetime dependencies from `dependsOn` requires the
309+
/// result to have the same type.
310+
///
311+
/// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
312+
/// should be expressed by a builtin that is hidden within the function body.
313+
@_unsafeNonescapableResult
314+
@_alwaysEmitIntoClient
315+
@inline(__always)
316+
@lifetime(source)
317+
internal func _unsafeLifetime<T: ~Copyable & ~Escapable,
318+
U: ~Copyable & ~Escapable>(
319+
dependent: consuming T, dependsOn source: borrowing U
320+
) -> T {
321+
dependent
322+
}

0 commit comments

Comments
 (0)