Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preparing for Swift 6 #1348

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Catchable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public extension CatchMixin {
on.async(flags: flags) {
do {
let rv = try body(error)
guard rv !== rp else { throw PMKError.returnedSelf }
guard rv !== rp else { throw PMKError<Void>.returnedSelf }
rv.pipe(to: rp.box.seal)
} catch {
rp.box.seal(.rejected(error))
Expand Down
4 changes: 4 additions & 0 deletions Sources/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public struct PMKConfiguration {
}

/// Modify this as soon as possible in your application’s lifetime
#if swift(>=6.0)
nonisolated(unsafe) public var conf = PMKConfiguration()
#else
public var conf = PMKConfiguration()
#endif
10 changes: 5 additions & 5 deletions Sources/Error.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum PMKError: Error {
public enum PMKError<T: Sendable>: Error {
/**
The completionHandler with form `(T?, Error?)` was called with `(nil, nil)`.
This is invalid as per Cocoa/Apple calling conventions.
Expand All @@ -24,7 +24,7 @@ public enum PMKError: Error {
case flatMap(Any, Any.Type)

/// `nil` was returned from `compactMap`
case compactMap(Any, Any.Type)
case compactMap(T)

/**
The lastValue or firstValue of a sequence was requested but the sequence was empty.
Expand All @@ -42,8 +42,8 @@ extension PMKError: CustomDebugStringConvertible {
switch self {
case .flatMap(let obj, let type):
return "Could not `flatMap<\(type)>`: \(obj)"
case .compactMap(let obj, let type):
return "Could not `compactMap<\(type)>`: \(obj)"
case .compactMap(let obj):
return "Could not `compactMap<>`: \(obj)"
case .invalidCallingConvention:
return "A closure was called with an invalid calling convention, probably (nil, nil)"
case .returnedSelf:
Expand Down Expand Up @@ -79,7 +79,7 @@ extension Error {
public var isCancelled: Bool {
do {
throw self
} catch PMKError.cancelled {
} catch PMKError<Void>.cancelled {
return true
} catch let error as CancellableError {
return error.isCancelled
Expand Down
2 changes: 1 addition & 1 deletion Sources/Resolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public extension Resolver {
} else if let obj = obj {
fulfill(obj)
} else {
reject(PMKError.invalidCallingConvention)
reject(PMKError<Void>.invalidCallingConvention)
}
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/Thenable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public extension Thenable {
on.async(flags: flags) {
do {
let rv = try body(value)
guard rv !== rp else { throw PMKError.returnedSelf }
guard rv !== rp else { throw PMKError<Void>.returnedSelf }
rv.pipe(to: rp.box.seal)
} catch {
rp.box.seal(.rejected(error))
Expand Down Expand Up @@ -136,7 +136,7 @@ public extension Thenable {
if let rv = try transform(value) {
rp.box.seal(.fulfilled(rv))
} else {
throw PMKError.compactMap(value, U.self)
throw PMKError.compactMap(value)
}
} catch {
rp.box.seal(.rejected(error))
Expand Down Expand Up @@ -167,7 +167,7 @@ public extension Thenable {
if let rv = value[keyPath: keyPath] {
rp.box.seal(.fulfilled(rv))
} else {
throw PMKError.compactMap(value, U.self)
throw PMKError.compactMap(value)
}
} catch {
rp.box.seal(.rejected(error))
Expand Down Expand Up @@ -498,7 +498,7 @@ public extension Thenable where T: Collection {
if let a1 = aa.first {
return a1
} else {
throw PMKError.emptySequence
throw PMKError<Void>.emptySequence
}
}
}
Expand All @@ -508,15 +508,15 @@ public extension Thenable where T: Collection {
for x in $0 where test(x) {
return x
}
throw PMKError.emptySequence
throw PMKError<Void>.emptySequence
}
}

/// - Returns: a promise fulfilled with the last value of this `Collection` or, if empty, a promise rejected with PMKError.emptySequence.
var lastValue: Promise<T.Iterator.Element> {
return map(on: nil) { aa in
if aa.isEmpty {
throw PMKError.emptySequence
throw PMKError<Void>.emptySequence
} else {
let i = aa.index(aa.endIndex, offsetBy: -1)
return aa[i]
Expand Down
6 changes: 3 additions & 3 deletions Sources/race.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public func race<U: Thenable>(_ thenables: U...) -> Promise<U.T> {
*/
public func race<U: Thenable>(_ thenables: [U]) -> Promise<U.T> {
guard !thenables.isEmpty else {
return Promise(error: PMKError.badInput)
return Promise(error: PMKError<Void>.badInput)
}
return _race(thenables)
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public func race<T>(_ guarantees: Guarantee<T>...) -> Guarantee<T> {
public func race<U: Thenable>(fulfilled thenables: [U]) -> Promise<U.T> {
var countdown = thenables.count
guard countdown > 0 else {
return Promise(error: PMKError.badInput)
return Promise(error: PMKError<Void>.badInput)
}

let rp = Promise<U.T>(.pending)
Expand All @@ -87,7 +87,7 @@ public func race<U: Thenable>(fulfilled thenables: [U]) -> Promise<U.T> {
guard rp.isPending else { return }
countdown -= 1
if countdown == 0 {
rp.box.seal(.rejected(PMKError.noWinner))
rp.box.seal(.rejected(PMKError<Void>.noWinner))
}
case .fulfilled(let value):
guard rp.isPending else { return }
Expand Down
2 changes: 1 addition & 1 deletion Sources/when.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public func when<U: Thenable, V: Thenable, W: Thenable, X: Thenable, Y: Thenable
public func when<It: IteratorProtocol>(fulfilled promiseIterator: It, concurrently: Int) -> Promise<[It.Element.T]> where It.Element: Thenable {

guard concurrently > 0 else {
return Promise(error: PMKError.badInput)
return Promise(error: PMKError<Void>.badInput)
}

var generator = promiseIterator
Expand Down
10 changes: 8 additions & 2 deletions Tests/A+/2.3.1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class Test231: XCTestCase {
return promise
}
promise.catch { err in
if case PMKError.returnedSelf = err {
switch err {
case PMKError<Void>.returnedSelf:
expectation.fulfill()
default:
break
}
}
}
Expand All @@ -21,8 +24,11 @@ class Test231: XCTestCase {
return promise
}
promise.catch { err in
if case PMKError.returnedSelf = err {
switch err {
case PMKError<Void>.returnedSelf:
expectation.fulfill()
default:
break
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreObjC/AnyPromiseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AnyPromiseTests: XCTestCase {
}

func testRejectedResult() {
switch AnyPromise(Promise<Int>(error: PMKError.badInput)).result {
switch AnyPromise(Promise<Int>(error: PMKError<Void>.badInput)).result {
case .rejected(let err)?:
print(err)
break
Expand Down
2 changes: 1 addition & 1 deletion Tests/CorePromise/CancellableErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class CancellationTests: XCTestCase {

#if swift(>=3.2)
func testIsCancelled() {
XCTAssertTrue(PMKError.cancelled.isCancelled)
XCTAssertTrue(PMKError<Void>.cancelled.isCancelled)
XCTAssertTrue(URLError.cancelled.isCancelled)
XCTAssertTrue(CocoaError.cancelled.isCancelled)
XCTAssertFalse(CocoaError(_nsError: NSError(domain: NSCocoaErrorDomain, code: CocoaError.Code.coderInvalidValue.rawValue)).isCancelled)
Expand Down
24 changes: 12 additions & 12 deletions Tests/CorePromise/ErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import XCTest

class PMKErrorTests: XCTestCase {
func testCustomStringConvertible() {
XCTAssertNotNil(PMKError.invalidCallingConvention.errorDescription)
XCTAssertNotNil(PMKError.returnedSelf.errorDescription)
XCTAssertNotNil(PMKError.badInput.errorDescription)
XCTAssertNotNil(PMKError.cancelled.errorDescription)
XCTAssertNotNil(PMKError.compactMap(1, Int.self).errorDescription)
XCTAssertNotNil(PMKError.emptySequence.errorDescription)
XCTAssertNotNil(PMKError<Void>.invalidCallingConvention.errorDescription)
XCTAssertNotNil(PMKError<Void>.returnedSelf.errorDescription)
XCTAssertNotNil(PMKError<Void>.badInput.errorDescription)
XCTAssertNotNil(PMKError<Void>.cancelled.errorDescription)
XCTAssertNotNil(PMKError<Int>.compactMap(1).errorDescription)
XCTAssertNotNil(PMKError<Void>.emptySequence.errorDescription)
}

func testCustomDebugStringConvertible() {
XCTAssertFalse(PMKError.invalidCallingConvention.debugDescription.isEmpty)
XCTAssertFalse(PMKError.returnedSelf.debugDescription.isEmpty)
XCTAssertNotNil(PMKError.badInput.debugDescription.isEmpty)
XCTAssertFalse(PMKError.cancelled.debugDescription.isEmpty)
XCTAssertFalse(PMKError.compactMap(1, Int.self).debugDescription.isEmpty)
XCTAssertFalse(PMKError.emptySequence.debugDescription.isEmpty)
XCTAssertFalse(PMKError<Void>.invalidCallingConvention.debugDescription.isEmpty)
XCTAssertFalse(PMKError<Void>.returnedSelf.debugDescription.isEmpty)
XCTAssertNotNil(PMKError<Void>.badInput.debugDescription.isEmpty)
XCTAssertFalse(PMKError<Void>.cancelled.debugDescription.isEmpty)
XCTAssertFalse(PMKError<Int>.compactMap(1).debugDescription.isEmpty)
XCTAssertFalse(PMKError<Void>.emptySequence.debugDescription.isEmpty)
}
}
23 changes: 19 additions & 4 deletions Tests/CorePromise/RaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ class RaceTests: XCTestCase {
let ex = expectation(description: "")
let empty = [Promise<Int>]()
race(empty).catch {
guard case PMKError.badInput = $0 else { return XCTFail() }
switch $0 {
case PMKError<Void>.badInput:
break
default:
XCTFail()
}
ex.fulfill()
}
wait(for: [ex], timeout: 10)
Expand All @@ -67,7 +72,12 @@ class RaceTests: XCTestCase {
let ex = expectation(description: "")
let empty = [Promise<Int>]()
race(fulfilled: empty).catch {
guard case PMKError.badInput = $0 else { return XCTFail() }
switch $0 {
case PMKError<Void>.badInput:
break
default:
return XCTFail()
}
ex.fulfill()
}
wait(for: [ex], timeout: 10)
Expand All @@ -81,8 +91,13 @@ class RaceTests: XCTestCase {
XCTFail()
ex.fulfill()
}.catch {
guard let pmkError = $0 as? PMKError else { return XCTFail() }
guard case .noWinner = pmkError else { return XCTFail() }
guard let pmkError = $0 as? PMKError<Void> else { return XCTFail() }
switch pmkError {
case .noWinner:
break
default:
return XCTFail()
}
guard pmkError.debugDescription == "All thenables passed to race(fulfilled:) were rejected" else { return XCTFail() }
ex.fulfill()
}
Expand Down
5 changes: 4 additions & 1 deletion Tests/CorePromise/RegressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ class RegressionTests: XCTestCase {
let promise1 = Promise<Void>(error: Error.dummy)
let promise2 = promise1.recover(on: nil) { _ in promise1 }
promise2.catch(on: nil) { err in
if case PMKError.returnedSelf = err {
switch err {
case PMKError<Void>.returnedSelf:
XCTFail()
default:
break
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Tests/CorePromise/ResolverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ class WrapTests: XCTestCase {
kittenFetcher.fetchWithCompletionBlock(block: seal.resolve)
}.catch { error in
defer { ex.fulfill() }
guard case PMKError.invalidCallingConvention = error else {
switch error {
case PMKError<Void>.invalidCallingConvention:
break
default:
return XCTFail()
}
}
Expand Down
9 changes: 6 additions & 3 deletions Tests/CorePromise/ThenableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ class ThenableTests: XCTestCase {
Promise.value("a").compactMap {
Int($0)
}.catch {
if case PMKError.compactMap = $0 {} else {
XCTFail()
switch $0 {
case PMKError<String>.compactMap:
break
default:
return XCTFail()
}
ex.fulfill()
}
Expand Down Expand Up @@ -214,7 +217,7 @@ class ThenableTests: XCTestCase {
// extensive use of `done` in A+ tests since PMK 5

let ex = expectation(description: "")
Promise<Int>(error: PMKError.badInput).then { x -> Promise<Int> in
Promise<Int>(error: PMKError<Void>.badInput).then { x -> Promise<Int> in
XCTFail()
return .value(x)
}.catch { _ in
Expand Down
8 changes: 6 additions & 2 deletions Tests/CorePromise/WhenConcurrentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ class WhenConcurrentTestCase_Swift: XCTestCase {

guard let e1 = p1.error else { return XCTFail() }
guard let e2 = p2.error else { return XCTFail() }
guard case PMKError.badInput = e1 else { return XCTFail() }
guard case PMKError.badInput = e2 else { return XCTFail() }
switch (e1, e2) {
case (PMKError<Void>.badInput, PMKError<Void>.badInput):
break
default:
return XCTFail()
}
}

func testStopsDequeueingOnceRejected() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/CorePromise/ZalgoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ZalgoTests: XCTestCase {

p1.catch { err in
defer{ ex.1.fulfill() }
guard case PMKError.returnedSelf = err else { return XCTFail() }
guard case PMKError<Void>.returnedSelf = err else { return XCTFail() }
}

waitForExpectations(timeout: 1)
Expand Down
2 changes: 1 addition & 1 deletion Tests/DeprecationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class DeprecationTests: XCTestCase {
}

func testPMKErrorFlatMap() {
XCTAssertNotNil(PMKError.flatMap(1, Int.self).errorDescription)
XCTAssertNotNil(PMKError<Int>.flatMap(1, Int.self).errorDescription)
}
}

Expand Down
Loading