Skip to content

Commit 285c3d3

Browse files
committed
[stdlib] Add support for noncopyables to withExtendedLifetime(of:) family
1 parent 72fa98c commit 285c3d3

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

stdlib/public/core/LifetimeManager.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -20,8 +20,10 @@
2020
/// return value for the `withExtendedLifetime(_:_:)` method.
2121
/// - Returns: The return value, if any, of the `body` closure parameter.
2222
@inlinable
23-
public func withExtendedLifetime<T, Result>(
24-
_ x: T, _ body: () throws -> Result
23+
@_preInverseGenerics
24+
public func withExtendedLifetime<T: ~Copyable, Result: ~Copyable>(
25+
_ x: borrowing T,
26+
_ body: () throws -> Result
2527
) rethrows -> Result {
2628
defer { _fixLifetime(x) }
2729
return try body()
@@ -38,6 +40,7 @@ public func withExtendedLifetime<T, Result>(
3840
/// - Returns: The return value, if any, of the `body` closure parameter.
3941
@inlinable
4042
public func withExtendedLifetime<T, Result>(
43+
// FIXME(NCG): This should have T, Result as ~Copyable, but then the closure would need to take a borrow
4144
_ x: T, _ body: (T) throws -> Result
4245
) rethrows -> Result {
4346
defer { _fixLifetime(x) }
@@ -47,7 +50,8 @@ public func withExtendedLifetime<T, Result>(
4750
// Fix the lifetime of the given instruction so that the ARC optimizer does not
4851
// shorten the lifetime of x to be before this point.
4952
@_transparent
50-
public func _fixLifetime<T>(_ x: T) {
53+
@_preInverseGenerics
54+
public func _fixLifetime<T: ~Copyable>(_ x: borrowing T) {
5155
Builtin.fixLifetime(x)
5256
}
5357

0 commit comments

Comments
 (0)