ManagedAtomic[LazyReference]: Always access ivar through pointer

The deinit implementations used to access the ivar directly, but that can confuse the compiler into assuming the value never changes after its initialization, because it doesn’t see accesses that are done via the pointer.

Stop accessing the ivar directly once it has been initialized.
This commit is contained in:
Karoy Lorentey committed 2023-03-31 15:19:02 -07:00
1 parent 951d3b2df4
commit bbee754adc
5 files changed
+27 -5

No files matched your search

@@ -139,6 +139,10 @@ public class ManagedAtomicLazyReference<Instance: AnyObject> {
@usableFromInline
internal typealias _Rep = Optional<Unmanaged<Instance>>.AtomicRepresentation
/// The atomic representation of the value stored inside.
///
/// Warning: This ivar must only ever be accessed via `_ptr` after
/// its initialization.
@usableFromInline
internal let _storage: _Rep
@@ -149,7 +153,7 @@ public class ManagedAtomicLazyReference<Instance: AnyObject> {
}
deinit {
if let unmanaged = _storage.dispose() {
if let unmanaged = _ptr.pointee.dispose() {
unmanaged.release()
}
}
+5 -1
View File
@@ -97,6 +97,10 @@ where Value.AtomicRepresentation.Value == Value {
@usableFromInline
internal typealias _Storage = Value.AtomicRepresentation
/// The atomic representation of the value stored inside.
///
/// Warning: This ivar must only ever be accessed via `_ptr` after
/// its initialization.
@usableFromInline
internal var _storage: _Storage
@@ -108,7 +112,7 @@ where Value.AtomicRepresentation.Value == Value {
}
deinit {
_ = _storage.dispose()
_ = _ptr.pointee.dispose()
}
@_alwaysEmitIntoClient @inline(__always)
@@ -142,6 +142,10 @@ public class ManagedAtomicLazyReference<Instance: AnyObject> {
@usableFromInline
internal typealias _Rep = Optional<Unmanaged<Instance>>.AtomicRepresentation
/// The atomic representation of the value stored inside.
///
/// Warning: This ivar must only ever be accessed via `_ptr` after
/// its initialization.
@usableFromInline
internal let _storage: _Rep
@@ -152,7 +156,7 @@ public class ManagedAtomicLazyReference<Instance: AnyObject> {
}
deinit {
if let unmanaged = _storage.dispose() {
if let unmanaged = _ptr.pointee.dispose() {
unmanaged.release()
}
}
@@ -99,6 +99,10 @@ where Value.AtomicRepresentation.Value == Value {
@usableFromInline
internal typealias _Storage = Value.AtomicRepresentation
/// The atomic representation of the value stored inside.
///
/// Warning: This ivar must only ever be accessed via `_ptr` after
/// its initialization.
@usableFromInline
internal var _storage: _Storage
@@ -110,7 +114,7 @@ where Value.AtomicRepresentation.Value == Value {
}
deinit {
_ = _storage.dispose()
_ = _ptr.pointee.dispose()
}
@_alwaysEmitIntoClient @inline(__always)
+7 -1
View File
@@ -15,12 +15,17 @@ import Atomics
class AtomicLazyReferenceTests: XCTestCase {
func test_unsafe_create_destroy() {
XCTAssertEqual(LifetimeTracked.instances, 0)
let v = UnsafeAtomicLazyReference<LifetimeTracked>.create()
defer { v.destroy() }
defer {
v.destroy()
XCTAssertEqual(LifetimeTracked.instances, 0)
}
XCTAssertNil(v.load())
}
func test_unsafe_storeIfNilThenLoad() {
XCTAssertEqual(LifetimeTracked.instances, 0)
do {
let v = UnsafeAtomicLazyReference<LifetimeTracked>.create()
XCTAssertNil(v.load())
@@ -39,6 +44,7 @@ class AtomicLazyReferenceTests: XCTestCase {
}
func test_managed_storeIfNilThenLoad() {
XCTAssertEqual(LifetimeTracked.instances, 0)
do {
let v = ManagedAtomicLazyReference<LifetimeTracked>()
XCTAssertNil(v.load())