Skip to content

Commit 1efb994

Browse files
committed
Enable strict memory safety in the Synchronization module
1 parent d9b5a46 commit 1efb994

File tree

7 files changed

+83
-81
lines changed

7 files changed

+83
-81
lines changed

stdlib/public/Synchronization/Atomics/Atomic.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public struct Atomic<Value: AtomicRepresentable>: ~Copyable {
2222
@_alwaysEmitIntoClient
2323
@_transparent
2424
var _address: UnsafeMutablePointer<Value.AtomicRepresentation> {
25-
UnsafeMutablePointer<Value.AtomicRepresentation>(_rawAddress)
25+
unsafe UnsafeMutablePointer<Value.AtomicRepresentation>(_rawAddress)
2626
}
2727

2828
@available(SwiftStdlib 6.0, *)
@@ -39,7 +39,7 @@ public struct Atomic<Value: AtomicRepresentable>: ~Copyable {
3939
@_alwaysEmitIntoClient
4040
@_transparent
4141
public init(_ initialValue: consuming Value) {
42-
_address.initialize(to: Value.encodeAtomicRepresentation(initialValue))
42+
unsafe _address.initialize(to: Value.encodeAtomicRepresentation(initialValue))
4343
}
4444

4545
// Deinit's can't be marked @_transparent. Do these things need all of these
@@ -48,10 +48,10 @@ public struct Atomic<Value: AtomicRepresentable>: ~Copyable {
4848
@_alwaysEmitIntoClient
4949
@inlinable
5050
deinit {
51-
let oldValue = Value.decodeAtomicRepresentation(_address.pointee)
51+
let oldValue = unsafe Value.decodeAtomicRepresentation(_address.pointee)
5252
_ = consume oldValue
5353

54-
_address.deinitialize(count: 1)
54+
unsafe _address.deinitialize(count: 1)
5555
}
5656
}
5757

stdlib/public/Synchronization/Atomics/AtomicLazyReference.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717
@available(SwiftStdlib 6.0, *)
1818
@frozen
1919
@_staticExclusiveOnly
20+
@safe
2021
public struct AtomicLazyReference<Instance: AnyObject>: ~Copyable {
2122
@usableFromInline
2223
let storage: Atomic<Unmanaged<Instance>?>
2324

2425
@available(SwiftStdlib 6.0, *)
2526
@inlinable
2627
public init() {
27-
storage = Atomic<Unmanaged<Instance>?>(nil)
28+
storage = unsafe Atomic<Unmanaged<Instance>?>(nil)
2829
}
2930

3031
@inlinable
3132
deinit {
32-
if let unmanaged = storage.load(ordering: .acquiring) {
33-
unmanaged.release()
33+
if let unmanaged = unsafe storage.load(ordering: .acquiring) {
34+
unsafe unmanaged.release()
3435
}
3536
}
3637
}
@@ -68,8 +69,8 @@ extension AtomicLazyReference {
6869
/// was passed to this function.
6970
@available(SwiftStdlib 6.0, *)
7071
public func storeIfNil(_ desired: consuming Instance) -> Instance {
71-
let desiredUnmanaged = Unmanaged.passRetained(desired)
72-
let (exchanged, current) = storage.compareExchange(
72+
let desiredUnmanaged = unsafe Unmanaged.passRetained(desired)
73+
let (exchanged, current) = unsafe storage.compareExchange(
7374
expected: nil,
7475
desired: desiredUnmanaged,
7576
ordering: .acquiringAndReleasing
@@ -78,11 +79,11 @@ extension AtomicLazyReference {
7879
if !exchanged {
7980
// The reference has already been initialized. Balance the retain that we
8081
// performed on 'desired'.
81-
desiredUnmanaged.release()
82-
return current!.takeUnretainedValue()
82+
unsafe desiredUnmanaged.release()
83+
return unsafe current!.takeUnretainedValue()
8384
}
8485

85-
return desiredUnmanaged.takeUnretainedValue()
86+
return unsafe desiredUnmanaged.takeUnretainedValue()
8687
}
8788

8889
/// Atomically loads and returns the current value of this reference.
@@ -94,8 +95,8 @@ extension AtomicLazyReference {
9495
/// `nil` if it has not been written to yet.
9596
@available(SwiftStdlib 6.0, *)
9697
public func load() -> Instance? {
97-
let value = storage.load(ordering: .acquiring)
98-
return value?.takeUnretainedValue()
98+
let value = unsafe storage.load(ordering: .acquiring)
99+
return unsafe value?.takeUnretainedValue()
99100
}
100101
}
101102

0 commit comments

Comments
 (0)