Skip to content

[swift/main] Availability fixes for literalPattern and ignoring... #721

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

Merged
merged 2 commits into from
Feb 16, 2024
Merged
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
12 changes: 10 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ let availabilityDefinition = PackageDescription.SwiftSetting.unsafeFlags([
"-Xfrontend",
"-define-availability",
"-Xfrontend",
"SwiftStdlib 5.9:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
"SwiftStdlib 5.9:macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0",
"-Xfrontend",
"-define-availability",
"-Xfrontend",
"SwiftStdlib 5.10:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
"-Xfrontend",
"-define-availability",
"-Xfrontend",
"SwiftStdlib 5.11:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
])

/// Swift settings for building a private stdlib-like module that is to be used
Expand Down Expand Up @@ -89,7 +97,7 @@ let package = Package(
name: "RegexTests",
dependencies: ["_StringProcessing", "TestSupport"],
swiftSettings: [
.unsafeFlags(["-Xfrontend", "-disable-availability-checking"]),
availabilityDefinition
]),
.testTarget(
name: "RegexBuilderTests",
Expand Down
6 changes: 3 additions & 3 deletions Sources/RegexBuilder/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ extension _RegexFactory {
_ left: some RegexComponent,
ignoringOutputTypeOf right: some RegexComponent
) -> Regex<Output> {
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) {
return accumulate(left, ignoreCapturesInTypedOutput(right))
}
return accumulate(left, right)
Expand All @@ -549,7 +549,7 @@ extension _RegexFactory {
ignoringOutputTypeOf left: some RegexComponent,
_ right: some RegexComponent
) -> Regex<Output> {
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) {
return accumulate(ignoreCapturesInTypedOutput(left), right)
}
return accumulate(left, right)
Expand All @@ -563,7 +563,7 @@ extension _RegexFactory {
ignoringOutputTypeOf left: some RegexComponent,
andAlso right: some RegexComponent
) -> Regex<Output> {
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) {
return accumulate(
ignoreCapturesInTypedOutput(left), ignoreCapturesInTypedOutput(right))
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/_StringProcessing/LiteralPrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@_implementationOnly import _RegexParser

@available(SwiftStdlib 5.9, *)
@available(SwiftStdlib 5.7, *)
extension Regex {
/// The literal pattern for this regex.
///
Expand All @@ -33,6 +33,7 @@ extension Regex {
/// the `CustomConsumingRegexComponent` protocol, this property is `nil`.
///
/// The value of this property may change between different releases of Swift.
@available(SwiftStdlib 5.11, *)
public var _literalPattern: String? {
var gen = LiteralPrinter(options: MatchingOptions())
gen.outputNode(self.program.tree.root)
Expand Down
2 changes: 1 addition & 1 deletion Sources/_StringProcessing/Utility/RegexFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct _RegexFactory {
@_spi(RegexBuilder)
public init() {}

@available(SwiftStdlib 5.8, *)
@available(SwiftStdlib 5.9, *)
public func ignoreCapturesInTypedOutput(
_ child: some RegexComponent
) -> Regex<Substring> {
Expand Down
7 changes: 4 additions & 3 deletions Tests/RegexTests/LiteralPrinterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import XCTest
import _StringProcessing
import RegexBuilder

@available(SwiftStdlib 5.11, *)
extension RegexTests {
func testPrintableRegex() throws {
let regexString = #"([a-fGH1-9[^\D]]+)?b*cd(e.+)\2\w\S+?"#
Expand Down Expand Up @@ -55,7 +56,7 @@ extension RegexTests {
// MARK: - PrintableRegex

// Demonstration of a guaranteed Codable/Sendable regex type.
@available(macOS 9999, *)
@available(SwiftStdlib 5.11, *)
struct PrintableRegex: RegexComponent, @unchecked Sendable {
var pattern: String
var regex: Regex<AnyRegexOutput>
Expand All @@ -76,7 +77,7 @@ struct PrintableRegex: RegexComponent, @unchecked Sendable {
}
}

@available(macOS 9999, *)
@available(SwiftStdlib 5.11, *)
extension PrintableRegex: Codable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
Expand All @@ -90,7 +91,7 @@ extension PrintableRegex: Codable {
}
}

@available(macOS 9999, *)
@available(SwiftStdlib 5.11, *)
extension PrintableRegex: CustomStringConvertible {
var description: String {
pattern
Expand Down
5 changes: 3 additions & 2 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct MatchError: Error {

// This just piggy-backs on the existing match testing to validate that
// literal patterns round trip correctly.
@available(SwiftStdlib 5.11, *)
func _roundTripLiteral(
_ regexStr: String,
syntax: SyntaxOptions
Expand Down Expand Up @@ -87,7 +88,7 @@ func _firstMatch(
}
}

do {
if #available(SwiftStdlib 5.11, *) {
let roundTripRegex = try? _roundTripLiteral(regexStr, syntax: syntax)
let roundTripResult = try? roundTripRegex?
.matchingSemantics(semanticLevel)
Expand Down Expand Up @@ -137,7 +138,7 @@ func _firstMatch(
}

if validateOptimizations {
assert(regex._forceAction(.addOptions(.disableOptimizations)))
precondition(regex._forceAction(.addOptions(.disableOptimizations)))
let unoptResult = try regex.firstMatch(in: input)
if result != nil && unoptResult == nil {
throw MatchError("match not found for unoptimized \(regexStr) in \(input)")
Expand Down
3 changes: 2 additions & 1 deletion Tests/RegexTests/ParseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ func parseTest(
}
serializedCaptures.deallocate()

if !unsupported && expectedErrors.isEmpty,
if #available(SwiftStdlib 5.11, *),
!unsupported && expectedErrors.isEmpty,
let pattern = Regex<AnyRegexOutput>(ast: ast)._literalPattern
{
let reparsedAST = parseWithRecovery(pattern, syntax)
Expand Down