From cc86c556be7f86d893d28df996a33d06ad58b2f2 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 30 Sep 2022 11:04:47 -0700 Subject: [PATCH] Fix support for non-final classes in AtomicReference MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Swift 5.6/7 generates new compiler warnings when non-final classes conform to `AtomicReference`. The warnings are pointing out a real design issue: Self is covariant in protocols, but we intend atomic operations to always deal with the base class rather than individual subclasses. Add a new associated type requirement to AtomicValue to represent the actual type that originally conformed to the protocol, and add explicit same-type requirements to clients to ensure they aren’t asked to return a subclass. Unfortunately, this change is source breaking: ``` class Base: AtomicReference {} class Derived: Base {} let ref = ManagedAtomic(nil) // before: OK // after: 'ManagedAtomic' requires the types 'Derived' and 'Base' be equivalent ``` --- Sources/Atomics/AtomicOptional.swift | 5 +++-- Sources/Atomics/AtomicRawRepresentable.swift | 14 ++++++++++++-- Sources/Atomics/AtomicStrongReference.swift | 11 +++-------- Sources/Atomics/AtomicValue.swift | 3 ++- Sources/Atomics/HighLevelTypes.swift.gyb | 6 ++++-- Sources/Atomics/autogenerated/HighLevelTypes.swift | 6 ++++-- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Sources/Atomics/AtomicOptional.swift b/Sources/Atomics/AtomicOptional.swift index 37b9d06..d645e8d 100644 --- a/Sources/Atomics/AtomicOptional.swift +++ b/Sources/Atomics/AtomicOptional.swift @@ -17,9 +17,10 @@ public protocol AtomicOptionalWrappable: AtomicValue { /// The atomic storage representation for `Optional`. associatedtype AtomicOptionalRepresentation: AtomicStorage - where AtomicOptionalRepresentation.Value == Self? + where AtomicOptionalRepresentation.Value == _AtomicValue? } -extension Optional: AtomicValue where Wrapped: AtomicOptionalWrappable { +extension Optional: AtomicValue +where Wrapped: AtomicOptionalWrappable, Wrapped._AtomicValue == Wrapped { public typealias AtomicRepresentation = Wrapped.AtomicOptionalRepresentation } diff --git a/Sources/Atomics/AtomicRawRepresentable.swift b/Sources/Atomics/AtomicRawRepresentable.swift index 82ace1f..5bbf4e7 100644 --- a/Sources/Atomics/AtomicRawRepresentable.swift +++ b/Sources/Atomics/AtomicRawRepresentable.swift @@ -10,7 +10,13 @@ // //===----------------------------------------------------------------------===// -extension RawRepresentable where Self: AtomicValue, RawValue: AtomicValue { +extension RawRepresentable +where + Self: AtomicValue, + RawValue: AtomicValue, + RawValue._AtomicValue == RawValue +{ + public typealias _AtomicValue = Self public typealias AtomicRepresentation = AtomicRawRepresentableStorage } @@ -18,7 +24,11 @@ extension RawRepresentable where Self: AtomicValue, RawValue: AtomicValue { /// type whose `RawValue` conforms to `AtomicValue`. @frozen public struct AtomicRawRepresentableStorage: AtomicStorage -where Value: RawRepresentable, Value.RawValue: AtomicValue { +where + Value: RawRepresentable, + Value.RawValue: AtomicValue, + Value.RawValue._AtomicValue == Value.RawValue +{ @usableFromInline internal typealias Storage = Value.RawValue.AtomicRepresentation @usableFromInline internal var _storage: Storage diff --git a/Sources/Atomics/AtomicStrongReference.swift b/Sources/Atomics/AtomicStrongReference.swift index 8931435..f6ed94d 100644 --- a/Sources/Atomics/AtomicStrongReference.swift +++ b/Sources/Atomics/AtomicStrongReference.swift @@ -15,15 +15,10 @@ import _AtomicsShims /// A class type that supports atomic strong references. public protocol AtomicReference: AnyObject, AtomicOptionalWrappable where - AtomicRepresentation == AtomicReferenceStorage, - AtomicOptionalRepresentation == AtomicOptionalReferenceStorage + _AtomicValue: AnyObject, + AtomicRepresentation == AtomicReferenceStorage<_AtomicValue>, + AtomicOptionalRepresentation == AtomicOptionalReferenceStorage<_AtomicValue> { - // These were added as a workaround for https://bugs.swift.org/browse/SR-10251 - // FIXME: We should remove these once the package requires a - // compiler version that contains that fix. - override associatedtype AtomicRepresentation = AtomicReferenceStorage - override associatedtype AtomicOptionalRepresentation = - AtomicOptionalReferenceStorage } /// The maximum number of other threads that can start accessing a diff --git a/Sources/Atomics/AtomicValue.swift b/Sources/Atomics/AtomicValue.swift index 3ba83b3..bfb0523 100644 --- a/Sources/Atomics/AtomicValue.swift +++ b/Sources/Atomics/AtomicValue.swift @@ -13,9 +13,10 @@ /// A type that supports atomic operations through a separate atomic storage /// representation. public protocol AtomicValue { + associatedtype _AtomicValue = Self /// The atomic storage representation for this value. associatedtype AtomicRepresentation: AtomicStorage - where AtomicRepresentation.Value == Self + where AtomicRepresentation.Value == _AtomicValue } /// The storage representation for an atomic value, providing pointer-based diff --git a/Sources/Atomics/HighLevelTypes.swift.gyb b/Sources/Atomics/HighLevelTypes.swift.gyb index aef8e08..6935c63 100644 --- a/Sources/Atomics/HighLevelTypes.swift.gyb +++ b/Sources/Atomics/HighLevelTypes.swift.gyb @@ -18,7 +18,8 @@ ${autogenerated_warning()} /// An unsafe reference type holding an atomic value, requiring manual memory /// management of the underlying storage representation. @frozen -public struct UnsafeAtomic { +public struct UnsafeAtomic +where Value._AtomicValue == Value { public typealias Storage = Value.AtomicRepresentation @usableFromInline @@ -75,7 +76,8 @@ extension UnsafeAtomic: @unchecked Sendable where Value: Sendable {} /// A reference type holding an atomic value, with automatic memory management. @_fixed_layout -public class ManagedAtomic { +public class ManagedAtomic +where Value._AtomicValue == Value { @usableFromInline internal var _storage: Value.AtomicRepresentation diff --git a/Sources/Atomics/autogenerated/HighLevelTypes.swift b/Sources/Atomics/autogenerated/HighLevelTypes.swift index 654acdf..ff584b7 100644 --- a/Sources/Atomics/autogenerated/HighLevelTypes.swift +++ b/Sources/Atomics/autogenerated/HighLevelTypes.swift @@ -20,7 +20,8 @@ /// An unsafe reference type holding an atomic value, requiring manual memory /// management of the underlying storage representation. @frozen -public struct UnsafeAtomic { +public struct UnsafeAtomic +where Value._AtomicValue == Value { public typealias Storage = Value.AtomicRepresentation @usableFromInline @@ -77,7 +78,8 @@ extension UnsafeAtomic: @unchecked Sendable where Value: Sendable {} /// A reference type holding an atomic value, with automatic memory management. @_fixed_layout -public class ManagedAtomic { +public class ManagedAtomic +where Value._AtomicValue == Value { @usableFromInline internal var _storage: Value.AtomicRepresentation