Skip to content

Commit

Permalink
[VER] Bump up Swift compiler version to 5.7.1 to follow Apple's policy (
Browse files Browse the repository at this point in the history
#140)

* [VER] Bump up Swift compiler version to 5.7.1

* [MOD] Modified swiftwasm version to use over 5.7.0 package
  • Loading branch information
x-0o0 authored Dec 11, 2023
1 parent 341b551 commit 5901e46
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 245 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: echo "${{ matrix.toolchain }}" > .swift-version
- uses: swiftwasm/swiftwasm-action@v5.7
- uses: swiftwasm/swiftwasm-action@v5.8
with:
shell-action: carton test --environment node

Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.6
// swift-tools-version: 5.7.1

import PackageDescription

Expand Down
8 changes: 1 addition & 7 deletions Sources/Dependencies/DependencyKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,7 @@ public protocol DependencyKey: TestDependencyKey {
/// See ``DependencyKey`` to define a static, default value for the live application.
public protocol TestDependencyKey {
/// The associated type representing the type of the dependency key's value.
#if swift(>=5.7.1)
associatedtype Value: Sendable = Self
#else
// NB: Can't constrain to `Sendable` on earlier Swift versions due to this bug:
// https://github.com/apple/swift/issues/60649
associatedtype Value = Self
#endif
associatedtype Value: Sendable = Self

/// The preview value for the dependency key.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/Dependencies/DependencyValues/Clocks.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if swift(>=5.7) && (canImport(RegexBuilder) || !os(macOS) && !targetEnvironment(macCatalyst))
#if (canImport(RegexBuilder) || !os(macOS) && !targetEnvironment(macCatalyst))
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
extension DependencyValues {
/// The current clock that features should use when a `ContinuousClock` would be appropriate.
Expand Down
30 changes: 3 additions & 27 deletions Sources/Dependencies/Internal/OpenExistential.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
#if swift(>=5.7)
// MARK: swift(>=5.7)

func _liveValue(_ key: Any.Type) -> Any? {
(key as? any DependencyKey.Type)?.liveValue
}
#else
// MARK: -
// MARK: swift(<5.7)

private enum Witness<T> {}

func _liveValue(_ key: Any.Type) -> Any? {
func open<T>(_: T.Type) -> Any? {
(Witness<T>.self as? AnyDependencyKey.Type)?.liveValue
}
return _openExistential(key, do: open)
}

protocol AnyDependencyKey {
static var liveValue: Any { get }
}

extension Witness: AnyDependencyKey where T: DependencyKey {
static var liveValue: Any { T.liveValue }
}
#endif
func _liveValue(_ key: Any.Type) -> Any? {
(key as? any DependencyKey.Type)?.liveValue
}
275 changes: 100 additions & 175 deletions Sources/Dependencies/WithDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,67 +39,45 @@ public func withDependencies<R>(
}
}

#if swift(>=5.7)
/// Updates the current dependencies for the duration of an asynchronous operation.
///
/// Any mutations made to ``DependencyValues`` inside `updateValuesForOperation` will be visible
/// to everything executed in the operation. For example, if you wanted to force the
/// ``DependencyValues/date`` dependency to be a particular date, you can do:
///
/// ```swift
/// await withDependencies {
/// $0.date.now = Date(timeIntervalSince1970: 1234567890)
/// } operation: {
/// // References to date in here are pinned to 1234567890.
/// }
/// ```
///
/// - Parameters:
/// - updateValuesForOperation: A closure for updating the current dependency values for the
/// duration of the operation.
/// - operation: An operation to perform wherein dependencies have been overridden.
/// - Returns: The result returned from `operation`.
@_unsafeInheritExecutor
@discardableResult
public func withDependencies<R>(
_ updateValuesForOperation: (inout DependencyValues) async throws -> Void,
operation: () async throws -> R
) async rethrows -> R {
try await isSetting(true) {
var dependencies = DependencyValues._current
try await updateValuesForOperation(&dependencies)
return try await DependencyValues.$_current.withValue(dependencies) {
try await isSetting(false) {
let result = try await operation()
if R.self is AnyClass {
dependencyObjects.store(result as AnyObject)
}
return result
}
}
}
}
#else
@discardableResult
public func withDependencies<R>(
_ updateValuesForOperation: (inout DependencyValues) async throws -> Void,
operation: () async throws -> R
) async rethrows -> R {
try await isSetting(true) {
var dependencies = DependencyValues._current
try await updateValuesForOperation(&dependencies)
return try await DependencyValues.$_current.withValue(dependencies) {
try await isSetting(false) {
let result = try await operation()
if R.self is AnyClass {
dependencyObjects.store(result as AnyObject)
}
return result
/// Updates the current dependencies for the duration of an asynchronous operation.
///
/// Any mutations made to ``DependencyValues`` inside `updateValuesForOperation` will be visible
/// to everything executed in the operation. For example, if you wanted to force the
/// ``DependencyValues/date`` dependency to be a particular date, you can do:
///
/// ```swift
/// await withDependencies {
/// $0.date.now = Date(timeIntervalSince1970: 1234567890)
/// } operation: {
/// // References to date in here are pinned to 1234567890.
/// }
/// ```
///
/// - Parameters:
/// - updateValuesForOperation: A closure for updating the current dependency values for the
/// duration of the operation.
/// - operation: An operation to perform wherein dependencies have been overridden.
/// - Returns: The result returned from `operation`.
@_unsafeInheritExecutor
@discardableResult
public func withDependencies<R>(
_ updateValuesForOperation: (inout DependencyValues) async throws -> Void,
operation: () async throws -> R
) async rethrows -> R {
try await isSetting(true) {
var dependencies = DependencyValues._current
try await updateValuesForOperation(&dependencies)
return try await DependencyValues.$_current.withValue(dependencies) {
try await isSetting(false) {
let result = try await operation()
if R.self is AnyClass {
dependencyObjects.store(result as AnyObject)
}
return result
}
}
}
#endif
}

/// Updates the current dependencies for the duration of a synchronous operation by taking the
/// dependencies tied to a given object.
Expand Down Expand Up @@ -168,129 +146,76 @@ public func withDependencies<Model: AnyObject, R>(
)
}

#if swift(>=5.7)
/// Updates the current dependencies for the duration of an asynchronous operation by taking the
/// dependencies tied to a given object.
///
/// - Parameters:
/// - model: An object with dependencies. The given model should have at least one `@Dependency`
/// property, or should have been initialized and returned from a `withDependencies`
/// operation.
/// - updateValuesForOperation: A closure for updating the current dependency values for the
/// duration of the operation.
/// - operation: The operation to run with the updated dependencies.
/// - Returns: The result returned from `operation`.
@_unsafeInheritExecutor
@discardableResult
public func withDependencies<Model: AnyObject, R>(
from model: Model,
_ updateValuesForOperation: (inout DependencyValues) async throws -> Void,
operation: () async throws -> R,
file: StaticString? = nil,
line: UInt? = nil
) async rethrows -> R {
guard let values = dependencyObjects.values(from: model)
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.
""",
file: file,
line: line
)
return try await operation()
}
return try await withDependencies {
$0 = values.merging(DependencyValues._current)
try await updateValuesForOperation(&$0)
} operation: {
let result = try await operation()
if R.self is AnyClass {
dependencyObjects.store(result as AnyObject)
}
return result
}
}
#else
@discardableResult
public func withDependencies<Model: AnyObject, R>(
from model: Model,
_ updateValuesForOperation: (inout DependencyValues) async throws -> Void,
operation: () async throws -> R,
file: StaticString? = nil,
line: UInt? = nil
) async rethrows -> R {
guard let values = dependencyObjects.values(from: model)
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.
""",
file: file,
line: line
)
return try await operation()
}
return try await withDependencies {
$0 = values.merging(DependencyValues._current)
try await updateValuesForOperation(&$0)
} operation: {
let result = try await operation()
if R.self is AnyClass {
dependencyObjects.store(result as AnyObject)
}
return result
}
}
#endif

#if swift(>=5.7)
/// Updates the current dependencies for the duration of an asynchronous operation by taking the
/// dependencies tied to a given object.
///
/// - Parameters:
/// - model: An object with dependencies. The given model should have at least one `@Dependency`
/// property, or should have been initialized and returned from a `withDependencies`
/// operation.
/// - operation: The operation to run with the updated dependencies.
/// - Returns: The result returned from `operation`.
@_unsafeInheritExecutor
@discardableResult
public func withDependencies<Model: AnyObject, R>(
from model: Model,
operation: () async throws -> R,
file: StaticString? = nil,
line: UInt? = nil
) async rethrows -> R {
try await withDependencies(
from: model,
{ _ in },
operation: operation,
/// Updates the current dependencies for the duration of an asynchronous operation by taking the
/// dependencies tied to a given object.
///
/// - Parameters:
/// - model: An object with dependencies. The given model should have at least one `@Dependency`
/// property, or should have been initialized and returned from a `withDependencies`
/// operation.
/// - updateValuesForOperation: A closure for updating the current dependency values for the
/// duration of the operation.
/// - operation: The operation to run with the updated dependencies.
/// - Returns: The result returned from `operation`.
@_unsafeInheritExecutor
@discardableResult
public func withDependencies<Model: AnyObject, R>(
from model: Model,
_ updateValuesForOperation: (inout DependencyValues) async throws -> Void,
operation: () async throws -> R,
file: StaticString? = nil,
line: UInt? = nil
) async rethrows -> R {
guard let values = dependencyObjects.values(from: model)
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.
""",
file: file,
line: line
)
return try await operation()
}
#else
@discardableResult
public func withDependencies<Model: AnyObject, R>(
from model: Model,
operation: () async throws -> R,
file: StaticString? = nil,
line: UInt? = nil
) async rethrows -> R {
try await withDependencies(
from: model,
{ _ in },
operation: operation,
file: file,
line: line
)
return try await withDependencies {
$0 = values.merging(DependencyValues._current)
try await updateValuesForOperation(&$0)
} operation: {
let result = try await operation()
if R.self is AnyClass {
dependencyObjects.store(result as AnyObject)
}
return result
}
#endif
}

/// Updates the current dependencies for the duration of an asynchronous operation by taking the
/// dependencies tied to a given object.
///
/// - Parameters:
/// - model: An object with dependencies. The given model should have at least one `@Dependency`
/// property, or should have been initialized and returned from a `withDependencies`
/// operation.
/// - operation: The operation to run with the updated dependencies.
/// - Returns: The result returned from `operation`.
@_unsafeInheritExecutor
@discardableResult
public func withDependencies<Model: AnyObject, R>(
from model: Model,
operation: () async throws -> R,
file: StaticString? = nil,
line: UInt? = nil
) async rethrows -> R {
try await withDependencies(
from: model,
{ _ in },
operation: operation,
file: file,
line: line
)
}

/// Propagates the current dependencies to an escaping context.
///
Expand Down
Loading

0 comments on commit 5901e46

Please sign in to comment.