Skip to content

Commit 7496d44

Browse files
committed
[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.
1 parent 44e9417 commit 7496d44

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

stdlib/public/core/UnsafePointer.swift

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,14 @@ extension UnsafeMutablePointer {
905905
}
906906
}
907907

908+
#if true // Builtin.initialize doesn't support noncopyable types (rdar://123253877)
909+
extension UnsafeMutablePointer {
910+
@_alwaysEmitIntoClient
911+
public func initialize(to value: consuming Pointee) {
912+
Builtin.initialize(value, self._rawValue)
913+
}
914+
}
915+
#else
908916
extension UnsafeMutablePointer where Pointee: ~Copyable {
909917
/// Initializes this pointer's memory with a single instance of the given
910918
/// value.
@@ -929,6 +937,7 @@ extension UnsafeMutablePointer {
929937
Builtin.initialize(value, self._rawValue)
930938
}
931939
}
940+
#endif
932941

933942
extension UnsafeMutablePointer where Pointee: ~Copyable {
934943
/// Retrieves and returns the referenced instance, returning the pointer's
@@ -1046,6 +1055,37 @@ extension UnsafeMutablePointer {
10461055
}
10471056
}
10481057

1058+
#if true // Builtin.takeArray* don't support noncopyable types (rdar://123253877)
1059+
extension UnsafeMutablePointer {
1060+
@_alwaysEmitIntoClient
1061+
public func moveInitialize(
1062+
@_nonEphemeral from source: UnsafeMutablePointer, count: Int
1063+
) {
1064+
_debugPrecondition(
1065+
count >= 0, "UnsafeMutablePointer.moveInitialize with negative count")
1066+
if self < source || self >= source + count {
1067+
// initialize forward from a disjoint or following overlapping range.
1068+
Builtin.takeArrayFrontToBack(
1069+
Pointee.self, self._rawValue, source._rawValue, count._builtinWordValue)
1070+
// This builtin is equivalent to:
1071+
// for i in 0..<count {
1072+
// (self + i).initialize(to: (source + i).move())
1073+
// }
1074+
}
1075+
else if self != source {
1076+
// initialize backward from a non-following overlapping range.
1077+
Builtin.takeArrayBackToFront(
1078+
Pointee.self, self._rawValue, source._rawValue, count._builtinWordValue)
1079+
// This builtin is equivalent to:
1080+
// var src = source + count
1081+
// var dst = self + count
1082+
// while dst != self {
1083+
// (--dst).initialize(to: (--src).move())
1084+
// }
1085+
}
1086+
}
1087+
}
1088+
#else
10491089
extension UnsafeMutablePointer where Pointee: ~Copyable {
10501090
/// Moves instances from initialized source memory into the uninitialized
10511091
/// memory referenced by this pointer, leaving the source memory
@@ -1125,6 +1165,7 @@ extension UnsafeMutablePointer {
11251165
}
11261166
}
11271167
}
1168+
#endif
11281169

11291170
extension UnsafeMutablePointer {
11301171
/// Initializes the memory referenced by this pointer with the values
@@ -1160,6 +1201,26 @@ extension UnsafeMutablePointer {
11601201
}
11611202
}
11621203

1204+
#if true // Builtin.assignTakeArray doesn't support noncopyable types (rdar://123253877)
1205+
extension UnsafeMutablePointer {
1206+
@_alwaysEmitIntoClient
1207+
public func moveUpdate(
1208+
@_nonEphemeral from source: UnsafeMutablePointer, count: Int
1209+
) {
1210+
_debugPrecondition(
1211+
count >= 0, "UnsafeMutablePointer.moveUpdate(from:) with negative count")
1212+
_debugPrecondition(
1213+
self + count <= source || source + count <= self,
1214+
"moveUpdate overlapping range")
1215+
Builtin.assignTakeArray(
1216+
Pointee.self, self._rawValue, source._rawValue, count._builtinWordValue)
1217+
// These builtins are equivalent to:
1218+
// for i in 0..<count {
1219+
// self[i] = (source + i).move()
1220+
// }
1221+
}
1222+
}
1223+
#else
11631224
extension UnsafeMutablePointer where Pointee: ~Copyable {
11641225
/// Update this pointer's initialized memory by moving the specified number
11651226
/// of instances the source pointer's memory, leaving the source memory
@@ -1227,7 +1288,21 @@ extension UnsafeMutablePointer {
12271288
moveUpdate(from: source, count: count)
12281289
}
12291290
}
1291+
#endif
12301292

1293+
#if true // Builtin.destroyArray doesn't support noncopyable types (rdar://123253877)
1294+
extension UnsafeMutablePointer {
1295+
@_alwaysEmitIntoClient
1296+
@discardableResult
1297+
public func deinitialize(count: Int) -> UnsafeMutableRawPointer {
1298+
_debugPrecondition(count >= 0, "UnsafeMutablePointer.deinitialize with negative count")
1299+
// TODO: IRGen optimization when `count` value is statically known to be 1,
1300+
// then call `Builtin.destroy(Pointee.self, _rawValue)` instead.
1301+
Builtin.destroyArray(Pointee.self, _rawValue, count._builtinWordValue)
1302+
return UnsafeMutableRawPointer(self)
1303+
}
1304+
}
1305+
#else
12311306
extension UnsafeMutablePointer where Pointee: ~Copyable {
12321307
/// Deinitializes the specified number of values starting at this pointer.
12331308
///
@@ -1263,6 +1338,7 @@ extension UnsafeMutablePointer {
12631338
return UnsafeMutableRawPointer(self)
12641339
}
12651340
}
1341+
#endif
12661342

12671343
extension UnsafeMutablePointer /* where Pointee: ~Copyable */ {
12681344
// FIXME: We want this to have the constraint above, but that triggers a .swiftinterface issue.

0 commit comments

Comments
 (0)