From 9e935a6e75a8875ab22a2d4c413b1ed59a3170e6 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 10 Jun 2024 18:37:13 -0700 Subject: [PATCH] Fix retroactive conformance warnings --- Realm/Swift/RLMSupport.swift | 9 ++++++++- RealmSwift/App.swift | 7 ++++++- RealmSwift/Combine.swift | 16 ++++++++++++++-- RealmSwift/Error.swift | 4 ++++ RealmSwift/Impl/ObjcBridgeable.swift | 7 ++++++- RealmSwift/Sync.swift | 10 +++++++++- .../Tests/ObjectSchemaInitializationTests.swift | 12 ++++++++++++ RealmSwift/Tests/RealmTests.swift | 9 +++++++++ 8 files changed, 68 insertions(+), 6 deletions(-) diff --git a/Realm/Swift/RLMSupport.swift b/Realm/Swift/RLMSupport.swift index ab3c840ab9..ab7544100c 100644 --- a/Realm/Swift/RLMSupport.swift +++ b/Realm/Swift/RLMSupport.swift @@ -66,7 +66,7 @@ extension RLMObject { } /// A protocol defining iterator support for RLMArray, RLMSet & RLMResults. -public protocol _RLMCollectionIterator { +public protocol _RLMCollectionIterator: Sequence { /** Returns a `RLMCollectionIterator` that yields successive elements in the collection. This enables support for sequence-style enumeration of `RLMObject` subclasses in Swift. @@ -97,10 +97,17 @@ extension _RLMDictionaryIterator where Self: RLMCollection { // Sequence conformance for RLMArray, RLMDictionary, RLMSet and RLMResults is provided by RLMCollection's // `makeIterator()` implementation. +#if compiler(<6.0) extension RLMArray: Sequence, _RLMCollectionIterator { } extension RLMDictionary: Sequence, _RLMDictionaryIterator {} extension RLMSet: Sequence, _RLMCollectionIterator {} extension RLMResults: Sequence, _RLMCollectionIterator {} +#else +extension RLMArray: @retroactive Sequence, _RLMCollectionIterator { } +extension RLMDictionary: @retroactive Sequence, _RLMDictionaryIterator {} +extension RLMSet: @retroactive Sequence, _RLMCollectionIterator {} +extension RLMResults: @retroactive Sequence, _RLMCollectionIterator {} +#endif /** This struct enables sequence-style enumeration for RLMObjects in Swift via `RLMCollection.makeIterator` diff --git a/RealmSwift/App.swift b/RealmSwift/App.swift index 8acdca293b..b10f97d2bb 100644 --- a/RealmSwift/App.swift +++ b/RealmSwift/App.swift @@ -397,7 +397,7 @@ public struct AppPublisher: Publisher, @unchecked Sendable { // DispatchQueue } @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -extension App: ObservableObject { +extension App { /// A publisher that emits Void each time the app changes. /// /// Despite the name, this actually emits *after* the app has changed. @@ -405,6 +405,11 @@ extension App: ObservableObject { return AppPublisher(self, scheduler: DispatchQueue.main) } } +#if compiler(>=6) +extension App: @retroactive ObservableObject {} +#else +extension App: ObservableObject {} +#endif @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) internal func promisify(_ fn: @escaping (@escaping @Sendable (Error?) -> Void) -> Void) -> Future { diff --git a/RealmSwift/Combine.swift b/RealmSwift/Combine.swift index 5b219efce2..a55a5ee77c 100644 --- a/RealmSwift/Combine.swift +++ b/RealmSwift/Combine.swift @@ -612,7 +612,7 @@ extension Realm { // MARK: - Object @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -extension Object: ObservableObject { +extension Object { /// A publisher that emits Void each time the object changes. /// /// Despite the name, this actually emits *after* the object has changed. @@ -621,7 +621,7 @@ extension Object: ObservableObject { } } @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -extension EmbeddedObject: ObservableObject { +extension EmbeddedObject { /// A publisher that emits Void each time the object changes. /// /// Despite the name, this actually emits *after* the embedded object has changed. @@ -647,6 +647,18 @@ extension ObjectBase: RealmSubscribable { } } +#if compiler(>=6) +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +extension Object: @retroactive ObservableObject {} +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +extension EmbeddedObject: @retroactive ObservableObject {} +#else +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +extension Object: ObservableObject {} +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +extension EmbeddedObject: ObservableObject {} +#endif + // MARK: - List @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) diff --git a/RealmSwift/Error.swift b/RealmSwift/Error.swift index f9d8d9275e..5028edb100 100644 --- a/RealmSwift/Error.swift +++ b/RealmSwift/Error.swift @@ -48,7 +48,11 @@ extension Realm.Error { // MARK: Equatable +#if compiler(>=6) +extension Realm.Error: @retroactive Equatable {} +#else extension Realm.Error: Equatable {} +#endif // FIXME: we should not be defining this but it's a breaking change to remove /// Returns a Boolean indicating whether the errors are identical. diff --git a/RealmSwift/Impl/ObjcBridgeable.swift b/RealmSwift/Impl/ObjcBridgeable.swift index 8b17dba00a..5f1cfb6d5f 100644 --- a/RealmSwift/Impl/ObjcBridgeable.swift +++ b/RealmSwift/Impl/ObjcBridgeable.swift @@ -202,11 +202,16 @@ extension ResultsSection: BuiltInObjcBridgeable { } } -extension RLMSwiftCollectionBase: Equatable { +extension RLMSwiftCollectionBase { public static func == (lhs: RLMSwiftCollectionBase, rhs: RLMSwiftCollectionBase) -> Bool { return lhs.isEqual(rhs) } } +#if compiler(>=6) +extension RLMSwiftCollectionBase: @retroactive Equatable {} +#else +extension RLMSwiftCollectionBase: Equatable {} +#endif extension Projection: BuiltInObjcBridgeable { public static func _rlmFromObjc(_ value: Any) -> Self? { diff --git a/RealmSwift/Sync.swift b/RealmSwift/Sync.swift index b09f2e5e65..417d9805e0 100644 --- a/RealmSwift/Sync.swift +++ b/RealmSwift/Sync.swift @@ -1098,7 +1098,7 @@ public class UserPublisher: Publisher { } @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -extension User: ObservableObject { +extension User { /// A publisher that emits Void each time the user changes. /// /// Despite the name, this actually emits *after* the user has changed. @@ -1107,6 +1107,14 @@ extension User: ObservableObject { } } +#if compiler(>=6) +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +extension User: @retroactive ObservableObject {} +#else +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +extension User: ObservableObject {} +#endif + public extension User { // NEXT-MAJOR: This function returns the incorrect type. It should be Document // rather than `[AnyHashable: Any]` diff --git a/RealmSwift/Tests/ObjectSchemaInitializationTests.swift b/RealmSwift/Tests/ObjectSchemaInitializationTests.swift index b1be6656d6..23843f0bc3 100644 --- a/RealmSwift/Tests/ObjectSchemaInitializationTests.swift +++ b/RealmSwift/Tests/ObjectSchemaInitializationTests.swift @@ -806,6 +806,7 @@ class SwiftObjectWithNonOptionalLinkProperty: SwiftFakeObject { @objc dynamic var objectCol = SwiftBoolObject() } +#if compiler(<6) extension Set: RealmOptionalType { public static func _rlmFromObjc(_ value: Any, insideOptional: Bool) -> Set? { fatalError() @@ -815,6 +816,17 @@ extension Set: RealmOptionalType { fatalError() } } +#else +extension Set: @retroactive RealmOptionalType { + public static func _rlmFromObjc(_ value: Any, insideOptional: Bool) -> Set? { + fatalError() + } + + public var _rlmObjcValue: Any { + fatalError() + } +} +#endif @available(*, deprecated) // Silence deprecation warnings for RealmOptional class SwiftObjectWithNonRealmOptionalType: SwiftFakeObject { diff --git a/RealmSwift/Tests/RealmTests.swift b/RealmSwift/Tests/RealmTests.swift index 1da2f85e27..992f6f8dfc 100644 --- a/RealmSwift/Tests/RealmTests.swift +++ b/RealmSwift/Tests/RealmTests.swift @@ -1894,12 +1894,21 @@ extension RealmTests { static var shared = CustomGlobalActor() } +#if compiler(<6) @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) extension CancellationError: Equatable { public static func == (lhs: CancellationError, rhs: CancellationError) -> Bool { true } } +#else +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +extension CancellationError: @retroactive Equatable { + public static func == (lhs: CancellationError, rhs: CancellationError) -> Bool { + true + } +} +#endif // Helper extension LogLevel {