Skip to content

Commit bba4bdf

Browse files
authored
Implement an overload of confirmation() that takes an unbounded range. (#598)
Unbounded ranges are not meaningful when used with `confirmation()` because _any_ confirmation count matches. As well, the `...` operator produces an instance of `UnboundedRange` which is a non-nominal type and cannot conform to `RangeExpression`. It may be non-obvious to a developer why `...` doesn't work as the `expectedCount` argument to that function when other range operators work as expected. This PR implements a stub overload of `confirmation()` that takes an unbounded range. The stub overload is marked unavailable and cannot be called. Example usage: ```swift await confirmation("Stuff happens", expectedCount: ...) { stuff in // ... } ``` Generated diagnostic: > 🛑 'confirmation(\_:expectedCount:sourceLocation:\_:)' is unavailable: Unbounded range '...' has no effect when used with a confirmation. As a reminder, using a range expression with `confirmation()` is an experimental feature and has not been API-reviewed. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 8534698 commit bba4bdf

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Sources/Testing/Issues/Confirmation.swift

+16
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ public func confirmation<R>(
179179
return try await body(confirmation)
180180
}
181181

182+
/// An overload of ``confirmation(_:expectedCount:sourceLocation:_:)-9bfdc``
183+
/// that handles the unbounded range operator (`...`).
184+
///
185+
/// This overload is necessary because `UnboundedRange` does not conform to
186+
/// `RangeExpression`. It effectively always succeeds because any number of
187+
/// confirmations matches, so it is marked unavailable and is not implemented.
188+
@available(*, unavailable, message: "Unbounded range '...' has no effect when used with a confirmation.")
189+
public func confirmation<R>(
190+
_ comment: Comment? = nil,
191+
expectedCount: UnboundedRange,
192+
sourceLocation: SourceLocation = #_sourceLocation,
193+
_ body: (Confirmation) async throws -> R
194+
) async rethrows -> R {
195+
fatalError("Unsupported")
196+
}
197+
182198
@_spi(Experimental)
183199
extension Confirmation {
184200
/// A protocol that describes a range expression that can be used with

Sources/Testing/Support/Environment.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ enum Environment {
188188
return nil
189189
case let errorCode:
190190
let error = Win32Error(rawValue: errorCode)
191-
fatalError("unexpected error when getting environment variable '\(name)': \(error) (\(errorCode))")
191+
fatalError("Unexpected error when getting environment variable '\(name)': \(error) (\(errorCode))")
192192
}
193193
} else if count > buffer.count {
194194
// Try again with the larger count.

Sources/TestingMacros/Support/AvailabilityGuards.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private func _createAvailabilityTraitExpr(
118118
return ".__unavailable(message: \(message), sourceLocation: \(sourceLocationExpr))"
119119

120120
default:
121-
fatalError("Unsupported keyword \(whenKeyword) passed to \(#function)")
121+
fatalError("Unsupported keyword \(whenKeyword) passed to \(#function). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new")
122122
}
123123
}
124124

0 commit comments

Comments
 (0)