From 046e02e5bf6c430368b1f50a38a68c68c385dc3e Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Sat, 13 Apr 2024 09:51:57 -0700 Subject: [PATCH 1/3] Update runtime warning for no liveValue. --- Package@swift-5.9.swift | 12 ++++++++++++ Sources/Dependencies/DependencyValues.swift | 13 ++++++++----- Sources/Dependencies/WithDependencies.swift | 14 ++++++++------ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Package@swift-5.9.swift b/Package@swift-5.9.swift index a8fa6b3a..ee564f88 100644 --- a/Package@swift-5.9.swift +++ b/Package@swift-5.9.swift @@ -43,6 +43,9 @@ let package = Package( .product(name: "CombineSchedulers", package: "combine-schedulers"), .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"), .product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"), + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") ] ), .testTarget( @@ -50,6 +53,9 @@ let package = Package( dependencies: [ "Dependencies", "DependenciesMacros", + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") ] ), .target( @@ -57,6 +63,9 @@ let package = Package( dependencies: [ "DependenciesMacrosPlugin", .product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"), + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") ] ), .macro( @@ -96,6 +105,9 @@ let package = Package( dependencies: [ "DependenciesMacrosPlugin", .product(name: "MacroTesting", package: "swift-macro-testing"), + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") ] ), ]) diff --git a/Sources/Dependencies/DependencyValues.swift b/Sources/Dependencies/DependencyValues.swift index 6b8cf023..ed05aecd 100644 --- a/Sources/Dependencies/DependencyValues.swift +++ b/Sources/Dependencies/DependencyValues.swift @@ -419,12 +419,15 @@ private final class CachedValues: @unchecked Sendable { \(dependencyDescription) - Every dependency registered with the library must conform to 'DependencyKey', and \ - that conformance must be visible to the running application. + To fix you can do one of two things: - To fix, make sure that '\(typeName(Key.self))' conforms to 'DependencyKey' by \ - providing a live implementation of your dependency, and make sure that the \ - conformance is linked with this current application. + * Conform '\(typeName(Key.self))' to the 'DependencyKey' protocol by providing \ + a live implementation of your dependency, and make sure that the conformance is \ + linked with this current application. + + * Override the implementation of '\(typeName(Key.self))' using 'withDependencies'. \ + This is typically done at the entry point of your application, but can be done \ + later too. """, file: DependencyValues.currentDependency.file ?? file, line: DependencyValues.currentDependency.line ?? line diff --git a/Sources/Dependencies/WithDependencies.swift b/Sources/Dependencies/WithDependencies.swift index 9746ed5e..43a8389e 100644 --- a/Sources/Dependencies/WithDependencies.swift +++ b/Sources/Dependencies/WithDependencies.swift @@ -170,9 +170,9 @@ public func withDependencies( else { runtimeWarn( """ - You are trying to propagate dependencies to a child model from a model with no \ - dependencies. To fix this, the given '\(Model.self)' must be returned from another \ - 'withDependencies' closure, or the class must hold at least one '@Dependency' property. + You are trying to propagate dependencies to a child model from a model with no dependencies. \ + To fix this, the given '\(Model.self)' must be returned from another 'withDependencies' \ + closure, or the class must hold at least one '@Dependency' property. """, file: file, line: line @@ -326,8 +326,8 @@ private class DependencyObjects: @unchecked Sendable { internal init() {} func store(_ object: AnyObject) { - self.storage.withValue { storage in - storage[ObjectIdentifier(object)] = DependencyObject( + self.storage.withValue { [id = ObjectIdentifier(object)] storage in + storage[id] = DependencyObject( object: object, dependencyValues: DependencyValues._current ) @@ -347,7 +347,9 @@ private class DependencyObjects: @unchecked Sendable { .compactMap({ $1 as? _HasInitialValues }) .first? .initialValues - ?? self.storage.withValue({ $0[ObjectIdentifier(object)]?.dependencyValues }) + ?? self.storage.withValue({ [id = ObjectIdentifier(object)] in + $0[id]?.dependencyValues + }) } } From aae215602c750c6dbb5edcb51a807f0ff42da31a Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 15 Apr 2024 13:38:48 -0700 Subject: [PATCH 2/3] wip --- Package@swift-5.9.swift | 12 ------------ Sources/Dependencies/WithDependencies.swift | 14 ++++++-------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/Package@swift-5.9.swift b/Package@swift-5.9.swift index ee564f88..a8fa6b3a 100644 --- a/Package@swift-5.9.swift +++ b/Package@swift-5.9.swift @@ -43,9 +43,6 @@ let package = Package( .product(name: "CombineSchedulers", package: "combine-schedulers"), .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"), .product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"), - ], - swiftSettings: [ - .enableExperimentalFeature("StrictConcurrency") ] ), .testTarget( @@ -53,9 +50,6 @@ let package = Package( dependencies: [ "Dependencies", "DependenciesMacros", - ], - swiftSettings: [ - .enableExperimentalFeature("StrictConcurrency") ] ), .target( @@ -63,9 +57,6 @@ let package = Package( dependencies: [ "DependenciesMacrosPlugin", .product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"), - ], - swiftSettings: [ - .enableExperimentalFeature("StrictConcurrency") ] ), .macro( @@ -105,9 +96,6 @@ let package = Package( dependencies: [ "DependenciesMacrosPlugin", .product(name: "MacroTesting", package: "swift-macro-testing"), - ], - swiftSettings: [ - .enableExperimentalFeature("StrictConcurrency") ] ), ]) diff --git a/Sources/Dependencies/WithDependencies.swift b/Sources/Dependencies/WithDependencies.swift index 43a8389e..9746ed5e 100644 --- a/Sources/Dependencies/WithDependencies.swift +++ b/Sources/Dependencies/WithDependencies.swift @@ -170,9 +170,9 @@ public func withDependencies( else { runtimeWarn( """ - You are trying to propagate dependencies to a child model from a model with no dependencies. \ - To fix this, the given '\(Model.self)' must be returned from another 'withDependencies' \ - closure, or the class must hold at least one '@Dependency' property. + You are trying to propagate dependencies to a child model from a model with no \ + dependencies. To fix this, the given '\(Model.self)' must be returned from another \ + 'withDependencies' closure, or the class must hold at least one '@Dependency' property. """, file: file, line: line @@ -326,8 +326,8 @@ private class DependencyObjects: @unchecked Sendable { internal init() {} func store(_ object: AnyObject) { - self.storage.withValue { [id = ObjectIdentifier(object)] storage in - storage[id] = DependencyObject( + self.storage.withValue { storage in + storage[ObjectIdentifier(object)] = DependencyObject( object: object, dependencyValues: DependencyValues._current ) @@ -347,9 +347,7 @@ private class DependencyObjects: @unchecked Sendable { .compactMap({ $1 as? _HasInitialValues }) .first? .initialValues - ?? self.storage.withValue({ [id = ObjectIdentifier(object)] in - $0[id]?.dependencyValues - }) + ?? self.storage.withValue({ $0[ObjectIdentifier(object)]?.dependencyValues }) } } From 8ae246d4d6d0df82592568c4cbca8d5fd6f60968 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 15 Apr 2024 13:40:52 -0700 Subject: [PATCH 3/3] wip --- .../DependencyValuesTests.swift | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Tests/DependenciesTests/DependencyValuesTests.swift b/Tests/DependenciesTests/DependencyValuesTests.swift index 72efc732..4ac3f23a 100644 --- a/Tests/DependenciesTests/DependencyValuesTests.swift +++ b/Tests/DependenciesTests/DependencyValuesTests.swift @@ -37,12 +37,14 @@ final class DependencyValuesTests: XCTestCase { Value: Int - Every dependency registered with the library must conform to 'DependencyKey', and that \ - conformance must be visible to the running application. + To fix you can do one of two things: - To fix, make sure that 'TestKey' conforms to 'DependencyKey' by providing a live \ - implementation of your dependency, and make sure that the conformance is linked with \ - this current application. + * Conform 'TestKey' to the 'DependencyKey' protocol by providing a live implementation \ + of your dependency, and make sure that the conformance is linked with this current \ + application. + + * Override the implementation of 'TestKey' using 'withDependencies'. This is typically \ + done at the entry point of your application, but can be done later too. """ } #endif @@ -71,12 +73,14 @@ final class DependencyValuesTests: XCTestCase { Value: Int - Every dependency registered with the library must conform to 'DependencyKey', and that \ - conformance must be visible to the running application. + To fix you can do one of two things: + + * Conform 'TestKey' to the 'DependencyKey' protocol by providing a live implementation \ + of your dependency, and make sure that the conformance is linked with this current \ + application. - To fix, make sure that 'TestKey' conforms to 'DependencyKey' by providing a live \ - implementation of your dependency, and make sure that the conformance is linked with \ - this current application. + * Override the implementation of 'TestKey' using 'withDependencies'. This is typically \ + done at the entry point of your application, but can be done later too. """ } #endif