mirror of
https://github.com/apple/swift-atomics
synced 2026-11-28 04:54:21 +00:00
Fix support for non-final classes in AtomicReference
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<Derived?>(nil)
// before: OK
// after: 'ManagedAtomic' requires the types 'Derived' and 'Base' be equivalent
```
This commit is contained in:
6 files changed
+28
-17
No files matched your search
@@ -17,9 +17,10 @@
|
||||
public protocol AtomicOptionalWrappable: AtomicValue {
|
||||
/// The atomic storage representation for `Optional<Self>`.
|
||||
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
|
||||
}
|
||||
@@ -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<Self>
|
||||
}
|
||||
|
||||
@@ -18,7 +24,11 @@ extension RawRepresentable where Self: AtomicValue, RawValue: AtomicValue {
|
||||
/// type whose `RawValue` conforms to `AtomicValue`.
|
||||
@frozen
|
||||
public struct AtomicRawRepresentableStorage<Value>: 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
|
||||
|
||||
@@ -15,15 +15,10 @@ import _AtomicsShims
|
||||
/// A class type that supports atomic strong references.
|
||||
public protocol AtomicReference: AnyObject, AtomicOptionalWrappable
|
||||
where
|
||||
AtomicRepresentation == AtomicReferenceStorage<Self>,
|
||||
AtomicOptionalRepresentation == AtomicOptionalReferenceStorage<Self>
|
||||
_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<Self>
|
||||
override associatedtype AtomicOptionalRepresentation =
|
||||
AtomicOptionalReferenceStorage<Self>
|
||||
}
|
||||
|
||||
/// The maximum number of other threads that can start accessing a
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<Value: AtomicValue> {
|
||||
public struct UnsafeAtomic<Value: AtomicValue>
|
||||
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<Value: AtomicValue> {
|
||||
public class ManagedAtomic<Value: AtomicValue>
|
||||
where Value._AtomicValue == Value {
|
||||
@usableFromInline
|
||||
internal var _storage: Value.AtomicRepresentation
|
||||
|
||||
|
||||
@@ -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<Value: AtomicValue> {
|
||||
public struct UnsafeAtomic<Value: AtomicValue>
|
||||
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<Value: AtomicValue> {
|
||||
public class ManagedAtomic<Value: AtomicValue>
|
||||
where Value._AtomicValue == Value {
|
||||
@usableFromInline
|
||||
internal var _storage: Value.AtomicRepresentation
|
||||
|
||||
|
||||
Reference in new issue
Block a user