Skip to content

Commit c9999ac

Browse files
committed
[stdlib] Unsafe[Mutable]Pointer: Add support for non-copyable Pointee types
[stdlib] Pull back @_aeic on pointer → integer conversions [stdlib] UnsafeMutablePointer.allocate: Fix thinko [stdlib] Disable support for noncopyable pointees on some pointer operations We have to temporarily pull back support for noncopyable pointees for UnsafeMutablePointer.initialize(to:), .moveInitialize, .moveUpdate, as the builtins they’re calling are no longer accepting such types. These will return following a builtin audit. [stdlib] Remove workarounds for certain builtins not supporting noncopyable use swiftlang#71733 fixed this! [stdlib] Update FIXME [stdlib] UnsafePointer: Update Swift version numbers [stdlib] UnsafePointer: Actually hide legacy ABI [stdlib] Remove workaround for U[M]BP.withMemoryRebound
1 parent adf5d79 commit c9999ac

File tree

7 files changed

+394
-91
lines changed

7 files changed

+394
-91
lines changed

stdlib/public/Cxx/UnsafeCxxIterators.swift

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
///
1919
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/InputIterator
2020
public protocol UnsafeCxxInputIterator: Equatable {
21-
associatedtype Pointee
21+
associatedtype Pointee: ~Copyable
2222

2323
/// Returns the unwrapped result of C++ `operator*()`.
2424
///
2525
/// Generally, Swift creates this property automatically for C++ types that
2626
/// define `operator*()`.
27+
@_borrowed
2728
var pointee: Pointee { get }
2829

2930
/// Returns an iterator pointing to the next item in the sequence.
@@ -33,19 +34,23 @@ public protocol UnsafeCxxInputIterator: Equatable {
3334
func successor() -> Self
3435
}
3536

36-
extension UnsafePointer: UnsafeCxxInputIterator {}
37+
extension UnsafePointer: UnsafeCxxInputIterator
38+
where Pointee: ~Copyable {}
3739

38-
extension UnsafeMutablePointer: UnsafeCxxInputIterator {}
40+
extension UnsafeMutablePointer: UnsafeCxxInputIterator
41+
where Pointee: ~Copyable {}
3942

4043
extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator {
4144
public typealias Pointee = Wrapped.Pointee
4245

4346
@inlinable
4447
public var pointee: Pointee {
45-
if let value = self {
46-
return value.pointee
48+
_read {
49+
guard let value = self else {
50+
fatalError("Could not dereference nullptr")
51+
}
52+
yield value.pointee
4753
}
48-
fatalError("Could not dereference nullptr")
4954
}
5055

5156
@inlinable
@@ -58,10 +63,12 @@ extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator
5863
}
5964

6065
public protocol UnsafeCxxMutableInputIterator: UnsafeCxxInputIterator {
66+
@_borrowed
6167
override var pointee: Pointee { get set }
6268
}
6369

64-
extension UnsafeMutablePointer: UnsafeCxxMutableInputIterator {}
70+
extension UnsafeMutablePointer: UnsafeCxxMutableInputIterator
71+
where Pointee: ~Copyable {}
6572

6673
/// Bridged C++ iterator that allows computing the distance between two of its
6774
/// instances, and advancing an instance by a given number of elements.
@@ -77,10 +84,14 @@ public protocol UnsafeCxxRandomAccessIterator: UnsafeCxxInputIterator {
7784
static func +=(lhs: inout Self, rhs: Distance)
7885
}
7986

80-
extension UnsafePointer: UnsafeCxxRandomAccessIterator {}
87+
extension UnsafePointer: UnsafeCxxRandomAccessIterator
88+
where Pointee: ~Copyable {}
8189

82-
extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator {}
90+
extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator
91+
where Pointee: ~Copyable {}
8392

84-
public protocol UnsafeCxxMutableRandomAccessIterator: UnsafeCxxRandomAccessIterator, UnsafeCxxMutableInputIterator {}
93+
public protocol UnsafeCxxMutableRandomAccessIterator:
94+
UnsafeCxxRandomAccessIterator, UnsafeCxxMutableInputIterator {}
8595

86-
extension UnsafeMutablePointer: UnsafeCxxMutableRandomAccessIterator {}
96+
extension UnsafeMutablePointer: UnsafeCxxMutableRandomAccessIterator
97+
where Pointee: ~Copyable {}

stdlib/public/Synchronization/AtomicPointers.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
@available(SwiftStdlib 6.0, *)
18-
extension UnsafePointer: AtomicRepresentable {
18+
extension UnsafePointer: AtomicRepresentable where Pointee: ~Copyable {
1919
/// The storage representation type that `Self` encodes to and decodes from
2020
/// which is a suitable type when used in atomic operations.
2121
@available(SwiftStdlib 6.0, *)
@@ -65,7 +65,7 @@ extension UnsafePointer: AtomicRepresentable {
6565
}
6666

6767
@available(SwiftStdlib 6.0, *)
68-
extension UnsafePointer: AtomicOptionalRepresentable {
68+
extension UnsafePointer: AtomicOptionalRepresentable where Pointee: ~Copyable {
6969
/// The storage representation type that encodes to and decodes from
7070
/// `Optional<Self>` which is a suitable type when used in atomic operations
7171
/// on `Optional`.
@@ -121,7 +121,7 @@ extension UnsafePointer: AtomicOptionalRepresentable {
121121
//===----------------------------------------------------------------------===//
122122

123123
@available(SwiftStdlib 6.0, *)
124-
extension UnsafeMutablePointer: AtomicRepresentable {
124+
extension UnsafeMutablePointer: AtomicRepresentable where Pointee: ~Copyable {
125125
/// The storage representation type that `Self` encodes to and decodes from
126126
/// which is a suitable type when used in atomic operations.
127127
@available(SwiftStdlib 6.0, *)
@@ -171,7 +171,8 @@ extension UnsafeMutablePointer: AtomicRepresentable {
171171
}
172172

173173
@available(SwiftStdlib 6.0, *)
174-
extension UnsafeMutablePointer: AtomicOptionalRepresentable {
174+
extension UnsafeMutablePointer: AtomicOptionalRepresentable
175+
where Pointee: ~Copyable {
175176
/// The storage representation type that encodes to and decodes from
176177
/// `Optional<Self>` which is a suitable type when used in atomic operations
177178
/// on `Optional`.

stdlib/public/core/MigrationSupport.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ extension UnsafeMutableRawPointer: _CustomPlaygroundQuickLookable {
425425
}
426426
}
427427

428-
extension UnsafePointer: _CustomPlaygroundQuickLookable {
428+
extension UnsafePointer: _CustomPlaygroundQuickLookable
429+
where Pointee: ~Copyable // FIXME: Remove this (rdar://123262870)
430+
{
429431
private var summary: String {
430432
let ptrValue = UInt64(bitPattern: Int64(Int(Builtin.ptrtoint_Word(_rawValue))))
431433
return ptrValue == 0
@@ -439,7 +441,9 @@ extension UnsafePointer: _CustomPlaygroundQuickLookable {
439441
}
440442
}
441443

442-
extension UnsafeMutablePointer: _CustomPlaygroundQuickLookable {
444+
extension UnsafeMutablePointer: _CustomPlaygroundQuickLookable
445+
where Pointee: ~Copyable // FIXME: Remove this (rdar://123262870)
446+
{
443447
private var summary: String {
444448
let ptrValue = UInt64(bitPattern: Int64(Int(Builtin.ptrtoint_Word(_rawValue))))
445449
return ptrValue == 0

stdlib/public/core/Pointer.swift

Lines changed: 3 additions & 3 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
@@ -30,8 +30,8 @@ public protocol _Pointer
3030
, _BitwiseCopyable {
3131
/// A type that represents the distance between two pointers.
3232
typealias Distance = Int
33-
34-
associatedtype Pointee
33+
34+
associatedtype Pointee: ~Copyable
3535

3636
/// The underlying raw pointer value.
3737
var _rawValue: Builtin.RawPointer { get }

0 commit comments

Comments
 (0)