From e0064b0e3c1eba3e811c8153e6ab8f9a4bed6321 Mon Sep 17 00:00:00 2001 From: Stuart Montgomery Date: Wed, 4 Sep 2024 16:31:46 -0500 Subject: [PATCH 1/3] Replace rethrows with typed throws in Graph --- Sources/Testing/Support/Graph.swift | 68 ++++++++++++++--------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/Sources/Testing/Support/Graph.swift b/Sources/Testing/Support/Graph.swift index ca179b768..d146526d6 100644 --- a/Sources/Testing/Support/Graph.swift +++ b/Sources/Testing/Support/Graph.swift @@ -393,7 +393,7 @@ extension Graph { /// path and leaf value of each node are passed to the closure. /// /// - Throws: Whatever is thrown by `body`. - private func _forEach(keyPath: [K], _ body: (Element) throws -> Void) rethrows -> Void { + private func _forEach(keyPath: [K], _ body: (Element) throws(E) -> Void) throws(E) where E: Error { try body((keyPath, value)) for (key, child) in children { var childKeyPath = keyPath @@ -411,7 +411,7 @@ extension Graph { /// key path and leaf value of each node are passed to the closure. /// /// - Throws: Whatever is thrown by `body`. - private func _forEach(keyPath: [K], _ body: (Element) async throws -> Void) async rethrows -> Void { + private func _forEach(keyPath: [K], _ body: (Element) async throws(E) -> Void) async throws(E) where E: Error { try await body((keyPath, value)) for (key, child) in children { var childKeyPath = keyPath @@ -429,9 +429,9 @@ extension Graph { /// - Throws: Whatever is thrown by `body`. /// /// This function iterates depth-first. - func forEach(_ body: (Element) throws -> Void) rethrows -> Void { - try _forEach(keyPath: []) { - try body(($0, $1)) + func forEach(_ body: (Element) throws(E) -> Void) throws(E) where E: Error { + try _forEach(keyPath: []) { (element) throws(E) in + try body(element) } } @@ -444,9 +444,9 @@ extension Graph { /// - Throws: Whatever is thrown by `body`. /// /// This function iterates depth-first. - func forEach(_ body: (Element) async throws -> Void) async rethrows -> Void { - try await _forEach(keyPath: []) { - try await body(($0, $1)) + func forEach(_ body: (Element) async throws(E) -> Void) async throws(E) where E: Error { + try await _forEach(keyPath: []) { (element) async throws(E) in + try await body(element) } } @@ -496,9 +496,9 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMapValues(_ transform: (Element) throws -> U?) rethrows -> Graph? { - try compactMapValues { - try transform($0).map { ($0, false) } + func compactMapValues(_ transform: (Element) throws(E) -> U?) throws(E) -> Graph? where E: Error { + try compactMapValues { (element) throws(E) in + try transform(element).map { ($0, false) } } } @@ -518,9 +518,9 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMapValues(_ transform: (Element) async throws -> U?) async rethrows -> Graph? { - try await compactMapValues { - try await transform($0).map { ($0, false) } + func compactMapValues(_ transform: (Element) async throws(E) -> U?) async throws(E) -> Graph? where E: Error { + try await compactMapValues { (element) async throws(E) in + try await transform(element).map { ($0, false) } } } @@ -543,9 +543,9 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMapValues(_ transform: (Element) throws -> (U, recursivelyApply: Bool)?) rethrows -> Graph? { - try _compactMapValues(keyPath: []) { - try transform(($0, $1)) + func compactMapValues(_ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph? where E: Error { + try _compactMapValues(keyPath: []) { (element) throws(E) in + try transform(element) } } @@ -563,7 +563,7 @@ extension Graph { /// child nodes are omitted from the new graph. /// /// - Throws: Whatever is thrown by `transform`. - private func _compactMapValues(keyPath: [K], _ transform: (Element) throws -> (U, recursivelyApply: Bool)?) rethrows -> Graph? { + private func _compactMapValues(keyPath: [K], _ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph? where E: Error { guard let (newValue, recursivelyApply) = try transform((keyPath, value)) else { return nil } @@ -603,9 +603,9 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMapValues(_ transform: (Element) async throws -> (U, recursivelyApply: Bool)?) async rethrows -> Graph? { - try await _compactMapValues(keyPath: []) { - try await transform(($0, $1)) + func compactMapValues(_ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph? where E: Error { + try await _compactMapValues(keyPath: []) { (element) async throws(E) in + try await transform(element) } } @@ -623,7 +623,7 @@ extension Graph { /// child nodes are omitted from the new graph. /// /// - Throws: Whatever is thrown by `transform`. - private func _compactMapValues(keyPath: [K], _ transform: (Element) async throws -> (U, recursivelyApply: Bool)?) async rethrows -> Graph? { + private func _compactMapValues(keyPath: [K], _ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph? where E: Error { guard let (newValue, recursivelyApply) = try await transform((keyPath, value)) else { return nil } @@ -658,7 +658,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func mapValues(_ transform: (Element) throws -> U) rethrows -> Graph { + func mapValues(_ transform: (Element) throws(E) -> U) throws(E) -> Graph where E: Error { try compactMapValues(transform)! } @@ -676,7 +676,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func mapValues(_ transform: (Element) async throws -> U) async rethrows -> Graph { + func mapValues(_ transform: (Element) async throws(E) -> U) async throws(E) -> Graph where E: Error { try await compactMapValues(transform)! } @@ -698,7 +698,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func mapValues(_ transform: (Element) throws -> (U, recursivelyApply: Bool)) rethrows -> Graph { + func mapValues(_ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)) throws(E) -> Graph where E: Error { try compactMapValues(transform)! } @@ -720,7 +720,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func mapValues(_ transform: (Element) async throws -> (U, recursivelyApply: Bool)) async rethrows -> Graph { + func mapValues(_ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)) async throws(E) -> Graph where E: Error { try await compactMapValues(transform)! } @@ -738,7 +738,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func map(_ transform: (Element) throws -> U) rethrows -> [U] { + func map(_ transform: (Element) throws(E) -> U) throws(E) -> [U] where E: Error { try compactMap(transform) } @@ -756,7 +756,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func map(_ transform: (Element) async throws -> U) async rethrows -> [U] { + func map(_ transform: (Element) async throws(E) -> U) async throws(E) -> [U] where E: Error { try await compactMap(transform) } @@ -776,10 +776,10 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMap(_ transform: (Element) throws -> U?) rethrows -> [U] { + func compactMap(_ transform: (Element) throws(E) -> U?) throws(E) -> [U] where E: Error { var result = [U]() - try forEach { keyPath, value in + try forEach { (keyPath, value) throws(E) in if let newValue = try transform((keyPath, value)) { result.append(newValue) } @@ -804,10 +804,10 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMap(_ transform: (Element) async throws -> U?) async rethrows -> [U] { + func compactMap(_ transform: (Element) async throws(E) -> U?) async throws(E) -> [U] where E: Error { var result = [U]() - try await forEach { keyPath, value in + try await forEach { (keyPath, value) async throws(E) in if let newValue = try await transform((keyPath, value)) { result.append(newValue) } @@ -829,7 +829,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func flatMap(_ transform: (Element) throws -> S) rethrows -> [S.Element] where S: Sequence { + func flatMap(_ transform: (Element) throws(E) -> S) throws(E) -> [S.Element] where S: Sequence, E: Error { try map(transform).flatMap { $0 } } @@ -846,7 +846,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func flatMap(_ transform: (Element) async throws -> S) async rethrows -> [S.Element] where S: Sequence { + func flatMap(_ transform: (Element) async throws(E) -> S) async throws(E) -> [S.Element] where S: Sequence, E: Error { try await map(transform).flatMap { $0 } } } From 48b80b8f2b5cbc046f0f833c64b0686e43c190e4 Mon Sep 17 00:00:00 2001 From: Stuart Montgomery Date: Wed, 4 Sep 2024 17:53:55 -0500 Subject: [PATCH 2/3] Remove 'where E: Error' which is implicit --- Sources/Testing/Support/Graph.swift | 36 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Sources/Testing/Support/Graph.swift b/Sources/Testing/Support/Graph.swift index d146526d6..e8e2d4628 100644 --- a/Sources/Testing/Support/Graph.swift +++ b/Sources/Testing/Support/Graph.swift @@ -393,7 +393,7 @@ extension Graph { /// path and leaf value of each node are passed to the closure. /// /// - Throws: Whatever is thrown by `body`. - private func _forEach(keyPath: [K], _ body: (Element) throws(E) -> Void) throws(E) where E: Error { + private func _forEach(keyPath: [K], _ body: (Element) throws(E) -> Void) throws(E) { try body((keyPath, value)) for (key, child) in children { var childKeyPath = keyPath @@ -411,7 +411,7 @@ extension Graph { /// key path and leaf value of each node are passed to the closure. /// /// - Throws: Whatever is thrown by `body`. - private func _forEach(keyPath: [K], _ body: (Element) async throws(E) -> Void) async throws(E) where E: Error { + private func _forEach(keyPath: [K], _ body: (Element) async throws(E) -> Void) async throws(E) { try await body((keyPath, value)) for (key, child) in children { var childKeyPath = keyPath @@ -429,7 +429,7 @@ extension Graph { /// - Throws: Whatever is thrown by `body`. /// /// This function iterates depth-first. - func forEach(_ body: (Element) throws(E) -> Void) throws(E) where E: Error { + func forEach(_ body: (Element) throws(E) -> Void) throws(E) { try _forEach(keyPath: []) { (element) throws(E) in try body(element) } @@ -444,7 +444,7 @@ extension Graph { /// - Throws: Whatever is thrown by `body`. /// /// This function iterates depth-first. - func forEach(_ body: (Element) async throws(E) -> Void) async throws(E) where E: Error { + func forEach(_ body: (Element) async throws(E) -> Void) async throws(E) { try await _forEach(keyPath: []) { (element) async throws(E) in try await body(element) } @@ -496,7 +496,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMapValues(_ transform: (Element) throws(E) -> U?) throws(E) -> Graph? where E: Error { + func compactMapValues(_ transform: (Element) throws(E) -> U?) throws(E) -> Graph? { try compactMapValues { (element) throws(E) in try transform(element).map { ($0, false) } } @@ -518,7 +518,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMapValues(_ transform: (Element) async throws(E) -> U?) async throws(E) -> Graph? where E: Error { + func compactMapValues(_ transform: (Element) async throws(E) -> U?) async throws(E) -> Graph? { try await compactMapValues { (element) async throws(E) in try await transform(element).map { ($0, false) } } @@ -543,7 +543,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMapValues(_ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph? where E: Error { + func compactMapValues(_ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph? { try _compactMapValues(keyPath: []) { (element) throws(E) in try transform(element) } @@ -563,7 +563,7 @@ extension Graph { /// child nodes are omitted from the new graph. /// /// - Throws: Whatever is thrown by `transform`. - private func _compactMapValues(keyPath: [K], _ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph? where E: Error { + private func _compactMapValues(keyPath: [K], _ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph? { guard let (newValue, recursivelyApply) = try transform((keyPath, value)) else { return nil } @@ -603,7 +603,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMapValues(_ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph? where E: Error { + func compactMapValues(_ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph? { try await _compactMapValues(keyPath: []) { (element) async throws(E) in try await transform(element) } @@ -623,7 +623,7 @@ extension Graph { /// child nodes are omitted from the new graph. /// /// - Throws: Whatever is thrown by `transform`. - private func _compactMapValues(keyPath: [K], _ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph? where E: Error { + private func _compactMapValues(keyPath: [K], _ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph? { guard let (newValue, recursivelyApply) = try await transform((keyPath, value)) else { return nil } @@ -658,7 +658,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func mapValues(_ transform: (Element) throws(E) -> U) throws(E) -> Graph where E: Error { + func mapValues(_ transform: (Element) throws(E) -> U) throws(E) -> Graph { try compactMapValues(transform)! } @@ -676,7 +676,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func mapValues(_ transform: (Element) async throws(E) -> U) async throws(E) -> Graph where E: Error { + func mapValues(_ transform: (Element) async throws(E) -> U) async throws(E) -> Graph { try await compactMapValues(transform)! } @@ -698,7 +698,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func mapValues(_ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)) throws(E) -> Graph where E: Error { + func mapValues(_ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)) throws(E) -> Graph { try compactMapValues(transform)! } @@ -720,7 +720,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func mapValues(_ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)) async throws(E) -> Graph where E: Error { + func mapValues(_ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)) async throws(E) -> Graph { try await compactMapValues(transform)! } @@ -738,7 +738,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func map(_ transform: (Element) throws(E) -> U) throws(E) -> [U] where E: Error { + func map(_ transform: (Element) throws(E) -> U) throws(E) -> [U] { try compactMap(transform) } @@ -756,7 +756,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func map(_ transform: (Element) async throws(E) -> U) async throws(E) -> [U] where E: Error { + func map(_ transform: (Element) async throws(E) -> U) async throws(E) -> [U] { try await compactMap(transform) } @@ -776,7 +776,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMap(_ transform: (Element) throws(E) -> U?) throws(E) -> [U] where E: Error { + func compactMap(_ transform: (Element) throws(E) -> U?) throws(E) -> [U] { var result = [U]() try forEach { (keyPath, value) throws(E) in @@ -804,7 +804,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func compactMap(_ transform: (Element) async throws(E) -> U?) async throws(E) -> [U] where E: Error { + func compactMap(_ transform: (Element) async throws(E) -> U?) async throws(E) -> [U] { var result = [U]() try await forEach { (keyPath, value) async throws(E) in From 0dd8d667e0f62a7d4e950a6e81babf9adf803072 Mon Sep 17 00:00:00 2001 From: Stuart Montgomery Date: Wed, 4 Sep 2024 22:00:53 -0500 Subject: [PATCH 3/3] Remove two more unnecessary generic constraints I overlooked --- Sources/Testing/Support/Graph.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Testing/Support/Graph.swift b/Sources/Testing/Support/Graph.swift index e8e2d4628..741efe06c 100644 --- a/Sources/Testing/Support/Graph.swift +++ b/Sources/Testing/Support/Graph.swift @@ -829,7 +829,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func flatMap(_ transform: (Element) throws(E) -> S) throws(E) -> [S.Element] where S: Sequence, E: Error { + func flatMap(_ transform: (Element) throws(E) -> S) throws(E) -> [S.Element] where S: Sequence { try map(transform).flatMap { $0 } } @@ -846,7 +846,7 @@ extension Graph { /// - Throws: Whatever is thrown by `transform`. /// /// This function iterates depth-first. - func flatMap(_ transform: (Element) async throws(E) -> S) async throws(E) -> [S.Element] where S: Sequence, E: Error { + func flatMap(_ transform: (Element) async throws(E) -> S) async throws(E) -> [S.Element] where S: Sequence { try await map(transform).flatMap { $0 } } }