Skip to content

Commit b7fb965

Browse files
authored
Replace opaque results with generic parameters in Algorithms.swift (#367)
Work around the compiler bug rdar://92459215 by using generic parameters instead of opaque result types for a couple of methods.
1 parent 40c177f commit b7fb965

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Sources/RegexBuilder/Algorithms.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
import _StringProcessing
1313

14+
// FIXME(rdar://92459215): We should be using 'some RegexComponent' instead of
15+
// <R: RegexComponent> for the methods below that don't impose any additional
16+
// requirements on the type. Currently the generic parameter is needed to work
17+
// around a compiler issue.
18+
1419
extension BidirectionalCollection where SubSequence == Substring {
1520
/// Matches a regex in its entirety, where the regex is created by
1621
/// the given closure.
@@ -94,10 +99,10 @@ extension BidirectionalCollection where SubSequence == Substring {
9499
/// - Returns: A collection of substrings, split from this collection's
95100
/// elements.
96101
@available(SwiftStdlib 5.7, *)
97-
public func split(
102+
public func split<R: RegexComponent>(
98103
maxSplits: Int = Int.max,
99104
omittingEmptySubsequences: Bool = true,
100-
@RegexComponentBuilder separator: () -> some RegexComponent
105+
@RegexComponentBuilder separator: () -> R
101106
) -> [SubSequence] {
102107
split(separator: separator(), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
103108
}
@@ -191,11 +196,11 @@ where Self: BidirectionalCollection, SubSequence == Substring {
191196
/// - Returns: A new collection in which all matches for regex in `subrange`
192197
/// are replaced by `replacement`, using `content` to create the regex.
193198
@available(SwiftStdlib 5.7, *)
194-
public func replacing<Replacement: Collection>(
199+
public func replacing<R: RegexComponent, Replacement: Collection>(
195200
with replacement: Replacement,
196201
subrange: Range<Index>,
197202
maxReplacements: Int = .max,
198-
@RegexComponentBuilder content: () -> some RegexComponent
203+
@RegexComponentBuilder content: () -> R
199204
) -> Self where Replacement.Element == Element {
200205
replacing(content(), with: replacement, subrange: subrange, maxReplacements: maxReplacements)
201206
}
@@ -213,10 +218,10 @@ where Self: BidirectionalCollection, SubSequence == Substring {
213218
/// - Returns: A new collection in which all matches for regex in `subrange`
214219
/// are replaced by `replacement`, using `content` to create the regex.
215220
@available(SwiftStdlib 5.7, *)
216-
public func replacing<Replacement: Collection>(
221+
public func replacing<R: RegexComponent, Replacement: Collection>(
217222
with replacement: Replacement,
218223
maxReplacements: Int = .max,
219-
@RegexComponentBuilder content: () -> some RegexComponent
224+
@RegexComponentBuilder content: () -> R
220225
) -> Self where Replacement.Element == Element {
221226
replacing(content(), with: replacement, maxReplacements: maxReplacements)
222227
}
@@ -232,10 +237,10 @@ where Self: BidirectionalCollection, SubSequence == Substring {
232237
/// - content: A closure that returns the collection to search for
233238
/// and replace.
234239
@available(SwiftStdlib 5.7, *)
235-
public mutating func replace<Replacement: Collection>(
240+
public mutating func replace<R: RegexComponent, Replacement: Collection>(
236241
with replacement: Replacement,
237242
maxReplacements: Int = .max,
238-
@RegexComponentBuilder content: () -> some RegexComponent
243+
@RegexComponentBuilder content: () -> R
239244
) where Replacement.Element == Element {
240245
replace(content(), with: replacement, maxReplacements: maxReplacements)
241246
}

0 commit comments

Comments
 (0)