Skip to content

Commit f66e8be

Browse files
author
Harlan Haskins
authored
Merge pull request swiftlang#229 from harlanhaskins/what-goes-up-must-come-down
Avoid conforming String to Error in this module
2 parents bb1f34a + b400e3e commit f66e8be

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

Sources/Exercises/Participant.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,25 @@ public protocol Participant {
2929
// ...
3030
}
3131

32+
// Errors that may be thrown from default implementations
33+
private enum ParticipantError: Error {
34+
case unsupported
35+
}
36+
3237
// Default impls
3338
extension Participant {
34-
static var unsupported: Error { "Unsupported" }
35-
3639
// Produce a function that will parse a grapheme break entry from a line
3740
public static func graphemeBreakProperty() throws -> (String) -> GraphemeBreakEntry? {
38-
throw unsupported
41+
throw ParticipantError.unsupported
3942
}
4043

4144
// Produce a function that will extract the bodies of C-style comments from its input
4245
public static func cComments() throws -> (String) -> [Substring] {
43-
throw unsupported
46+
throw ParticipantError.unsupported
4447
}
4548

4649
// Produce a function that will extract the bodies of Swift-style comments from its input
4750
public static func swiftComments() throws -> (String) -> [Substring] {
48-
throw unsupported
51+
throw ParticipantError.unsupported
4952
}
5053
}

Sources/_MatchingEngine/Regex/Parse/Parse.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,6 @@ extension ParsingContext {
122122

123123
// Diagnostics
124124
extension Parser {
125-
mutating func report(
126-
_ str: String, _ function: String = #function, _ line: Int = #line
127-
) throws -> Never {
128-
throw """
129-
ERROR: \(str)
130-
(error detected in parser at \(function):\(line))
131-
"""
132-
}
133-
134125
fileprivate func loc(
135126
_ start: Source.Position
136127
) -> SourceLocation {
@@ -529,5 +520,3 @@ public func parseWithDelimiters<S: StringProtocol>(
529520
let (contents, delim) = droppingRegexDelimiters(String(regex))
530521
return try parse(contents, delim.defaultSyntaxOptions)
531522
}
532-
533-
extension String: Error {}

Tests/RegexTests/MatchTests.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import XCTest
1313
@testable import _MatchingEngine
1414
@testable import _StringProcessing
1515

16+
struct MatchError: Error {
17+
var message: String
18+
init(_ message: String) {
19+
self.message = message
20+
}
21+
}
22+
1623
extension Executor {
1724
func _firstMatch(
1825
_ regex: String, input: String,
@@ -31,7 +38,7 @@ extension Executor {
3138
let caps = result.rawCaptures.slices(from: input)
3239
return (input[result.range], caps)
3340
} else if start == input.endIndex {
34-
throw "match not found for \(regex) in \(input)"
41+
throw MatchError("match not found for \(regex) in \(input)")
3542
} else {
3643
input.formIndex(after: &start)
3744
}
@@ -76,27 +83,29 @@ func flatCaptureTest(
7683
if expect == nil {
7784
continue
7885
} else {
79-
throw "Match failed"
86+
throw MatchError("Match failed")
8087
}
8188
}
8289
guard let expect = expect else {
83-
throw "Match of \(test) succeeded where failure expected in \(regex)"
90+
throw MatchError("""
91+
Match of \(test) succeeded where failure expected in \(regex)
92+
""")
8493
}
8594
let capStrs = caps.map { $0 == nil ? nil : String($0!) }
8695
guard expect.count == capStrs.count else {
87-
throw """
96+
throw MatchError("""
8897
Capture count mismatch:
8998
\(expect)
9099
\(capStrs)
91-
"""
100+
""")
92101
}
93102

94103
guard expect.elementsEqual(capStrs) else {
95-
throw """
104+
throw MatchError("""
96105
Capture mismatch:
97106
\(expect)
98107
\(capStrs)
99-
"""
108+
""")
100109
}
101110
} catch {
102111
if !xfail {

0 commit comments

Comments
 (0)