From bbee754adcf18974ade9917478abdcbad45244ae Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 31 Mar 2023 15:18:40 -0700 Subject: [PATCH] ManagedAtomic[LazyReference]: Always access ivar through pointer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Sources/Atomics/AtomicLazyReference.swift.gyb | 6 +++++- Sources/Atomics/HighLevelTypes.swift.gyb | 6 +++++- Sources/Atomics/autogenerated/AtomicLazyReference.swift | 6 +++++- Sources/Atomics/autogenerated/HighLevelTypes.swift | 6 +++++- Tests/AtomicsTests/AtomicLazyReference.swift | 8 +++++++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Sources/Atomics/AtomicLazyReference.swift.gyb b/Sources/Atomics/AtomicLazyReference.swift.gyb index d977bef..91bb3b5 100644 --- a/Sources/Atomics/AtomicLazyReference.swift.gyb +++ b/Sources/Atomics/AtomicLazyReference.swift.gyb @@ -139,6 +139,10 @@ public class ManagedAtomicLazyReference { @usableFromInline internal typealias _Rep = Optional>.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 { } deinit { - if let unmanaged = _storage.dispose() { + if let unmanaged = _ptr.pointee.dispose() { unmanaged.release() } } diff --git a/Sources/Atomics/HighLevelTypes.swift.gyb b/Sources/Atomics/HighLevelTypes.swift.gyb index a2796eb..c3cffc4 100644 --- a/Sources/Atomics/HighLevelTypes.swift.gyb +++ b/Sources/Atomics/HighLevelTypes.swift.gyb @@ -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) diff --git a/Sources/Atomics/autogenerated/AtomicLazyReference.swift b/Sources/Atomics/autogenerated/AtomicLazyReference.swift index 3e942a2..ca6ec18 100644 --- a/Sources/Atomics/autogenerated/AtomicLazyReference.swift +++ b/Sources/Atomics/autogenerated/AtomicLazyReference.swift @@ -142,6 +142,10 @@ public class ManagedAtomicLazyReference { @usableFromInline internal typealias _Rep = Optional>.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 { } deinit { - if let unmanaged = _storage.dispose() { + if let unmanaged = _ptr.pointee.dispose() { unmanaged.release() } } diff --git a/Sources/Atomics/autogenerated/HighLevelTypes.swift b/Sources/Atomics/autogenerated/HighLevelTypes.swift index 8a8d950..2da30da 100644 --- a/Sources/Atomics/autogenerated/HighLevelTypes.swift +++ b/Sources/Atomics/autogenerated/HighLevelTypes.swift @@ -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) diff --git a/Tests/AtomicsTests/AtomicLazyReference.swift b/Tests/AtomicsTests/AtomicLazyReference.swift index ce2c8f6..ae1bd41 100644 --- a/Tests/AtomicsTests/AtomicLazyReference.swift +++ b/Tests/AtomicsTests/AtomicLazyReference.swift @@ -15,12 +15,17 @@ import Atomics class AtomicLazyReferenceTests: XCTestCase { func test_unsafe_create_destroy() { + XCTAssertEqual(LifetimeTracked.instances, 0) let v = UnsafeAtomicLazyReference.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.create() XCTAssertNil(v.load()) @@ -39,6 +44,7 @@ class AtomicLazyReferenceTests: XCTestCase { } func test_managed_storeIfNilThenLoad() { + XCTAssertEqual(LifetimeTracked.instances, 0) do { let v = ManagedAtomicLazyReference() XCTAssertNil(v.load())