Skip to content

Commit 5eff906

Browse files
committed
stdlib: use _withUnprotected... functions instead of Builtin.unprotectedAddressOf
1 parent 1ae1c51 commit 5eff906

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

stdlib/public/core/AnyHashable.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ public struct AnyHashable {
159159
}
160160

161161
self.init(_box: _ConcreteHashableBox(false)) // Dummy value
162-
let ptr = UnsafeMutablePointer<AnyHashable>(Builtin.unprotectedAddressOf(&self))
163-
_makeAnyHashableUpcastingToHashableBaseType(
164-
base,
165-
storingResultInto: ptr)
162+
_withUnprotectedUnsafeMutablePointer(to: &self) {
163+
_makeAnyHashableUpcastingToHashableBaseType(
164+
base,
165+
storingResultInto: $0)
166+
}
166167
}
167168

168169
internal init<H: Hashable>(_usingDefaultRepresentationOf base: H) {

stdlib/public/core/KeyPath.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,8 +1951,9 @@ func _modifyAtWritableKeyPath_impl<Root, Value>(
19511951
keyPath: _unsafeUncheckedDowncast(keyPath,
19521952
to: ReferenceWritableKeyPath<Root, Value>.self))
19531953
}
1954-
let ptr = UnsafePointer<Root>(Builtin.unprotectedAddressOf(&root))
1955-
return keyPath._projectMutableAddress(from: ptr)
1954+
return _withUnprotectedUnsafePointer(to: &root) {
1955+
keyPath._projectMutableAddress(from: $0)
1956+
}
19561957
}
19571958

19581959
// The release that ends the access scope is guaranteed to happen
@@ -1981,8 +1982,9 @@ func _setAtWritableKeyPath<Root, Value>(
19811982
value: value)
19821983
}
19831984
// TODO: we should be able to do this more efficiently than projecting.
1984-
let ptr = UnsafePointer<Root>(Builtin.unprotectedAddressOf(&root))
1985-
let (addr, owner) = keyPath._projectMutableAddress(from: ptr)
1985+
let (addr, owner) = _withUnprotectedUnsafePointer(to: &root) {
1986+
keyPath._projectMutableAddress(from: $0)
1987+
}
19861988
addr.pointee = value
19871989
_fixLifetime(owner)
19881990
// FIXME: this needs a deallocation barrier to ensure that the

stdlib/public/core/Random.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,9 @@ public struct SystemRandomNumberGenerator: RandomNumberGenerator, Sendable {
158158
@inlinable
159159
public mutating func next() -> UInt64 {
160160
var random: UInt64 = 0
161-
#if $BuiltinUnprotectedAddressOf
162-
let ptr = UnsafeMutablePointer<UInt64>(Builtin.unprotectedAddressOf(&random))
163-
#else
164-
let ptr = UnsafeMutablePointer<UInt64>(Builtin.addressof(&random))
165-
#endif
166-
swift_stdlib_random(ptr, MemoryLayout<UInt64>.size)
161+
_withUnprotectedUnsafeMutablePointer(to: &random) {
162+
swift_stdlib_random($0, MemoryLayout<UInt64>.size)
163+
}
167164
return random
168165
}
169166
}

stdlib/public/core/VarArgs.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,11 @@ public func _encodeBitsAsWords<T>(_ x: T) -> [Int] {
215215
_internalInvariant(!result.isEmpty)
216216
var tmp = x
217217
// FIXME: use UnsafeMutablePointer.assign(from:) instead of memcpy.
218-
#if $BuiltinUnprotectedAddressOf
219-
_memcpy(dest: UnsafeMutablePointer(result._baseAddressIfContiguous!),
220-
src: UnsafeMutablePointer(Builtin.unprotectedAddressOf(&tmp)),
221-
size: UInt(MemoryLayout<T>.size))
222-
#else
223-
_memcpy(dest: UnsafeMutablePointer(result._baseAddressIfContiguous!),
224-
src: UnsafeMutablePointer(Builtin.addressof(&tmp)),
225-
size: UInt(MemoryLayout<T>.size))
226-
#endif
218+
_withUnprotectedUnsafeMutablePointer(to: &tmp) {
219+
_memcpy(dest: UnsafeMutablePointer(result._baseAddressIfContiguous!),
220+
src: $0,
221+
size: UInt(MemoryLayout<T>.size))
222+
}
227223
return result
228224
}
229225

0 commit comments

Comments
 (0)