Skip to content

Commit e08ebde

Browse files
authored
Merge pull request #73045 from lorentey/noncopyable-primitives-refinements
[stdlib] Round out ~Copyable generalizations in stdlib primitives
2 parents bfa910d + 1b89293 commit e08ebde

13 files changed

+319
-123
lines changed

stdlib/public/core/CTypes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ extension OpaquePointer {
202202
/// Converts a typed `UnsafeMutablePointer` to an opaque C pointer.
203203
@_transparent
204204
@_preInverseGenerics
205-
public init<T>(@_nonEphemeral _ from: UnsafeMutablePointer<T>) {
205+
public init<T: ~Copyable>(@_nonEphemeral _ from: UnsafeMutablePointer<T>) {
206206
self._rawValue = from._rawValue
207207
}
208208

@@ -211,7 +211,7 @@ extension OpaquePointer {
211211
/// The result is `nil` if `from` is `nil`.
212212
@_transparent
213213
@_preInverseGenerics
214-
public init?<T>(@_nonEphemeral _ from: UnsafeMutablePointer<T>?) {
214+
public init?<T: ~Copyable>(@_nonEphemeral _ from: UnsafeMutablePointer<T>?) {
215215
guard let unwrapped = from else { return nil }
216216
self.init(unwrapped)
217217
}

stdlib/public/core/LifetimeManager.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@
1919
/// extended. If `body` has a return value, that value is also used as the
2020
/// return value for the `withExtendedLifetime(_:_:)` method.
2121
/// - Returns: The return value, if any, of the `body` closure parameter.
22-
@inlinable
23-
@_preInverseGenerics
22+
@_alwaysEmitIntoClient
2423
public func withExtendedLifetime<T: ~Copyable, Result: ~Copyable>(
2524
_ x: borrowing T,
26-
_ body: () throws -> Result
25+
_ body: () throws -> Result // FIXME: Typed throws rdar://126576356
26+
) rethrows -> Result {
27+
defer { _fixLifetime(x) }
28+
return try body()
29+
}
30+
31+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
32+
@usableFromInline
33+
internal func withExtendedLifetime<T, Result>(
34+
_ x: T,
35+
_ body: () throws -> Result // FIXME: Typed throws rdar://126576356
2736
) rethrows -> Result {
2837
defer { _fixLifetime(x) }
2938
return try body()
@@ -38,10 +47,18 @@ public func withExtendedLifetime<T: ~Copyable, Result: ~Copyable>(
3847
/// extended. If `body` has a return value, that value is also used as the
3948
/// return value for the `withExtendedLifetime(_:_:)` method.
4049
/// - Returns: The return value, if any, of the `body` closure parameter.
41-
@inlinable
42-
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
44-
_ x: T, _ body: (T) throws -> Result
50+
@_alwaysEmitIntoClient
51+
public func withExtendedLifetime<T, Result: ~Copyable>(
52+
_ x: T, _ body: (T) throws -> Result // FIXME: Typed throws rdar://126576356
53+
) rethrows -> Result {
54+
defer { _fixLifetime(x) }
55+
return try body(x)
56+
}
57+
58+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
59+
@usableFromInline
60+
internal func withExtendedLifetime<T, Result>(
61+
_ x: T, _ body: (T) throws -> Result // FIXME: Typed throws rdar://126576356
4562
) rethrows -> Result {
4663
defer { _fixLifetime(x) }
4764
return try body(x)

stdlib/public/core/ManagedBuffer.swift

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ extension ManagedBuffer where Element: ~Copyable {
7979
@inlinable
8080
public final class func create(
8181
minimumCapacity: Int,
82-
makingHeaderWith factory: (
83-
ManagedBuffer<Header, Element>) throws -> Header
82+
makingHeaderWith factory: (ManagedBuffer<Header, Element>) throws -> Header
8483
) rethrows -> ManagedBuffer<Header, Element> {
85-
8684
let p = Builtin.allocWithTailElems_1(
8785
self,
8886
minimumCapacity._builtinWordValue, Element.self)
@@ -124,42 +122,78 @@ extension ManagedBuffer where Element: ~Copyable {
124122
internal final var headerAddress: UnsafeMutablePointer<Header> {
125123
return UnsafeMutablePointer<Header>(Builtin.addressof(&header))
126124
}
125+
}
127126

127+
extension ManagedBuffer where Element: ~Copyable {
128128
/// Call `body` with an `UnsafeMutablePointer` to the stored
129129
/// `Header`.
130130
///
131131
/// - Note: This pointer is valid only for the duration of the
132132
/// call to `body`.
133-
@_preInverseGenerics
134-
@inlinable
135-
public final func withUnsafeMutablePointerToHeader<R>(
136-
_ body: (UnsafeMutablePointer<Header>) throws -> R
137-
) rethrows -> R {
138-
return try withUnsafeMutablePointers { (v, _) in return try body(v) }
133+
@_alwaysEmitIntoClient
134+
@inline(__always)
135+
public final func withUnsafeMutablePointerToHeader<E: Error, R: ~Copyable>(
136+
_ body: (UnsafeMutablePointer<Header>) throws(E) -> R
137+
) throws(E) -> R {
138+
try withUnsafeMutablePointers { (v, _) throws(E) in try body(v) }
139139
}
140140

141141
/// Call `body` with an `UnsafeMutablePointer` to the `Element`
142142
/// storage.
143143
///
144144
/// - Note: This pointer is valid only for the duration of the
145145
/// call to `body`.
146-
@_preInverseGenerics
147-
@inlinable
148-
public final func withUnsafeMutablePointerToElements<R>(
149-
_ body: (UnsafeMutablePointer<Element>) throws -> R
150-
) rethrows -> R {
151-
return try withUnsafeMutablePointers { return try body($1) }
146+
@_alwaysEmitIntoClient
147+
@inline(__always)
148+
public final func withUnsafeMutablePointerToElements<E: Error, R: ~Copyable>(
149+
_ body: (UnsafeMutablePointer<Element>) throws(E) -> R
150+
) throws(E) -> R {
151+
try withUnsafeMutablePointers { (_, v) throws(E) in try body(v) }
152152
}
153153

154154
/// Call `body` with `UnsafeMutablePointer`s to the stored `Header`
155155
/// and raw `Element` storage.
156156
///
157157
/// - Note: These pointers are valid only for the duration of the
158158
/// call to `body`.
159-
@_preInverseGenerics
160-
@inlinable
161-
public final func withUnsafeMutablePointers<R>(
162-
_ body: (UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>) throws -> R
159+
@_alwaysEmitIntoClient
160+
@inline(__always)
161+
public final func withUnsafeMutablePointers<E: Error, R: ~Copyable>(
162+
_ body: (
163+
UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>
164+
) throws(E) -> R
165+
) throws(E) -> R {
166+
defer { _fixLifetime(self) }
167+
return try body(headerAddress, firstElementAddress)
168+
}
169+
}
170+
171+
extension ManagedBuffer {
172+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
173+
@_silgen_name("$ss13ManagedBufferC25withUnsafeMutablePointersyqd__qd__SpyxG_Spyq_GtKXEKlF")
174+
@usableFromInline
175+
internal final func __legacy_withUnsafeMutablePointerToHeader<R>(
176+
_ body: (UnsafeMutablePointer<Header>) throws -> R
177+
) rethrows -> R {
178+
return try withUnsafeMutablePointers { (v, _) in return try body(v) }
179+
}
180+
181+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
182+
@_silgen_name("$ss13ManagedBufferC32withUnsafeMutablePointerToHeaderyqd__qd__SpyxGKXEKlF")
183+
@usableFromInline
184+
internal final func __legacy_withUnsafeMutablePointerToElements<R>(
185+
_ body: (UnsafeMutablePointer<Element>) throws -> R
186+
) rethrows -> R {
187+
return try withUnsafeMutablePointers { return try body($1) }
188+
}
189+
190+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
191+
@_silgen_name("$ss13ManagedBufferC34withUnsafeMutablePointerToElementsyqd__qd__Spyq_GKXEKlF")
192+
@usableFromInline
193+
internal final func __legacy_withUnsafeMutablePointers<R>(
194+
_ body: (
195+
UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>
196+
) throws -> R
163197
) rethrows -> R {
164198
defer { _fixLifetime(self) }
165199
return try body(headerAddress, firstElementAddress)
@@ -202,7 +236,10 @@ extension ManagedBuffer where Element: ~Copyable {
202236
/// }
203237
///
204238
@frozen
205-
public struct ManagedBufferPointer<Header, Element: ~Copyable> {
239+
public struct ManagedBufferPointer<
240+
Header,
241+
Element: ~Copyable
242+
>: Copyable {
206243

207244
@_preInverseGenerics
208245
@usableFromInline
@@ -244,9 +281,6 @@ public struct ManagedBufferPointer<Header, Element: ~Copyable> {
244281
ManagedBufferPointer(unsafeBufferObject: $0).capacity
245282
}))
246283
}
247-
// FIXME: workaround for <rdar://problem/18619176>. If we don't
248-
// access header somewhere, its addressor gets linked away
249-
_ = header
250284
}
251285

252286
/// Manage the given `buffer`.
@@ -352,7 +386,9 @@ extension ManagedBufferPointer where Element: ~Copyable {
352386
yield &_headerPointer.pointee
353387
}
354388
}
389+
}
355390

391+
extension ManagedBufferPointer where Element: ~Copyable {
356392
/// Returns the object instance being used for storage.
357393
@_preInverseGenerics
358394
@inlinable
@@ -379,39 +415,36 @@ extension ManagedBufferPointer where Element: ~Copyable {
379415
///
380416
/// - Note: This pointer is valid only
381417
/// for the duration of the call to `body`.
382-
@_preInverseGenerics
383-
@inlinable
384-
public func withUnsafeMutablePointerToHeader<R>(
385-
_ body: (UnsafeMutablePointer<Header>) throws -> R
386-
) rethrows -> R {
387-
return try withUnsafeMutablePointers { (v, _) in return try body(v) }
418+
@_alwaysEmitIntoClient
419+
public func withUnsafeMutablePointerToHeader<E: Error, R: ~Copyable>(
420+
_ body: (UnsafeMutablePointer<Header>) throws(E) -> R
421+
) throws(E) -> R {
422+
try withUnsafeMutablePointers { (v, _) throws(E) in try body(v) }
388423
}
389424

390425
/// Call `body` with an `UnsafeMutablePointer` to the `Element`
391426
/// storage.
392427
///
393428
/// - Note: This pointer is valid only for the duration of the
394429
/// call to `body`.
395-
@_preInverseGenerics
396-
@inlinable
397-
public func withUnsafeMutablePointerToElements<R>(
398-
_ body: (UnsafeMutablePointer<Element>) throws -> R
399-
) rethrows -> R {
400-
return try withUnsafeMutablePointers { return try body($1) }
430+
@_alwaysEmitIntoClient
431+
public func withUnsafeMutablePointerToElements<E: Error, R: ~Copyable>(
432+
_ body: (UnsafeMutablePointer<Element>) throws(E) -> R
433+
) throws(E) -> R {
434+
try withUnsafeMutablePointers { (_, v) throws(E) in try body(v) }
401435
}
402436

403437
/// Call `body` with `UnsafeMutablePointer`s to the stored `Header`
404438
/// and raw `Element` storage.
405439
///
406440
/// - Note: These pointers are valid only for the duration of the
407441
/// call to `body`.
408-
@_preInverseGenerics
409-
@inlinable
410-
public func withUnsafeMutablePointers<R>(
442+
@_alwaysEmitIntoClient
443+
public func withUnsafeMutablePointers<E: Error, R: ~Copyable>(
411444
_ body: (
412445
UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>
413-
) throws -> R
414-
) rethrows -> R {
446+
) throws(E) -> R
447+
) throws(E) -> R {
415448
defer { _fixLifetime(_nativeBuffer) }
416449
return try body(_headerPointer, _elementPointer)
417450
}
@@ -427,6 +460,38 @@ extension ManagedBufferPointer where Element: ~Copyable {
427460
}
428461
}
429462

463+
extension ManagedBufferPointer {
464+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
465+
@_silgen_name("$ss20ManagedBufferPointerV017withUnsafeMutableC8ToHeaderyqd__qd__SpyxGKXEKlF")
466+
@usableFromInline
467+
internal func withUnsafeMutablePointerToHeader<R>(
468+
_ body: (UnsafeMutablePointer<Header>) throws -> R
469+
) rethrows -> R {
470+
try withUnsafeMutablePointers { (v, _) in try body(v) }
471+
}
472+
473+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
474+
@_silgen_name("$ss20ManagedBufferPointerV017withUnsafeMutableC10ToElementsyqd__qd__Spyq_GKXEKlF")
475+
@usableFromInline
476+
internal func withUnsafeMutablePointerToElements<R>(
477+
_ body: (UnsafeMutablePointer<Element>) throws -> R
478+
) rethrows -> R {
479+
try withUnsafeMutablePointers { (_, v) in try body(v) }
480+
}
481+
482+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
483+
@_silgen_name("$ss20ManagedBufferPointerV25withUnsafeMutablePointersyqd__qd__SpyxG_Spyq_GtKXEKlF")
484+
@usableFromInline
485+
internal func withUnsafeMutablePointers<R>(
486+
_ body: (
487+
UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>
488+
) throws -> R
489+
) rethrows -> R {
490+
defer { _fixLifetime(_nativeBuffer) }
491+
return try body(_headerPointer, _elementPointer)
492+
}
493+
}
494+
430495
extension ManagedBufferPointer where Element: ~Copyable {
431496
@_preInverseGenerics
432497
@inlinable

stdlib/public/core/Optional.swift

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,24 @@ extension Optional {
185185
/// of the instance.
186186
/// - Returns: The result of the given closure. If this instance is `nil`,
187187
/// returns `nil`.
188-
@inlinable
189-
public func map<U>(
190-
// FIXME: This needs to support typed throws.
188+
@_alwaysEmitIntoClient
189+
@_disfavoredOverload // FIXME: Workaround for source compat issue with
190+
// functions that used to shadow the original map
191+
// (rdar://125016028)
192+
public func map<E: Error, U: ~Copyable>(
193+
_ transform: (Wrapped) throws(E) -> U
194+
) throws(E) -> U? {
195+
switch self {
196+
case .some(let y):
197+
return .some(try transform(y))
198+
case .none:
199+
return .none
200+
}
201+
}
202+
203+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
204+
@usableFromInline
205+
internal func map<U>(
191206
_ transform: (Wrapped) throws -> U
192207
) rethrows -> U? {
193208
switch self {
@@ -220,7 +235,7 @@ extension Optional where Wrapped: ~Copyable {
220235
) throws(E) -> U? {
221236
#if $NoncopyableGenerics
222237
switch self {
223-
case .some(_borrowing y):
238+
case .some(borrowing y):
224239
return .some(try transform(y))
225240
case .none:
226241
return .none
@@ -251,9 +266,24 @@ extension Optional {
251266
/// of the instance.
252267
/// - Returns: The result of the given closure. If this instance is `nil`,
253268
/// returns `nil`.
254-
@inlinable
255-
public func flatMap<U>(
256-
// FIXME: This needs to support typed throws.
269+
@_alwaysEmitIntoClient
270+
@_disfavoredOverload // FIXME: Workaround for source compat issue with
271+
// functions that used to shadow the original flatMap
272+
// (rdar://125016028)
273+
public func flatMap<E: Error, U: ~Copyable>(
274+
_ transform: (Wrapped) throws(E) -> U?
275+
) throws(E) -> U? {
276+
switch self {
277+
case .some(let y):
278+
return try transform(y)
279+
case .none:
280+
return .none
281+
}
282+
}
283+
284+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
285+
@usableFromInline
286+
internal func flatMap<U>(
257287
_ transform: (Wrapped) throws -> U?
258288
) rethrows -> U? {
259289
switch self {
@@ -286,7 +316,7 @@ extension Optional where Wrapped: ~Copyable {
286316
_ transform: (borrowing Wrapped) throws(E) -> U?
287317
) throws(E) -> U? {
288318
switch self {
289-
case .some(_borrowing y):
319+
case .some(borrowing y):
290320
return try transform(y)
291321
case .none:
292322
return .none
@@ -770,10 +800,9 @@ extension Optional where Wrapped: ~Copyable {
770800
/// type as the `Wrapped` type of `optional`.
771801
@_transparent
772802
@_alwaysEmitIntoClient
773-
// FIXME: This needs to support typed throws.
774803
public func ?? <T: ~Copyable>(
775804
optional: consuming T?,
776-
defaultValue: @autoclosure () throws -> T
805+
defaultValue: @autoclosure () throws -> T // FIXME: typed throw
777806
) rethrows -> T {
778807
switch consume optional {
779808
case .some(let value):
@@ -784,8 +813,9 @@ public func ?? <T: ~Copyable>(
784813
}
785814

786815
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
816+
@_silgen_name("$ss2qqoiyxxSg_xyKXKtKlF")
787817
@usableFromInline
788-
internal func ?? <T>(
818+
internal func _legacy_abi_optionalNilCoalescingOperator <T>(
789819
optional: T?,
790820
defaultValue: @autoclosure () throws -> T
791821
) rethrows -> T {

0 commit comments

Comments
 (0)