Swift 5.7 features on Linux or macOS 12 (Monterey)?

Hello

I am writing a command line tool to help me to automate some tasks and I would like to use the new Swift 5.7 Regex classes.

Apparently, despite being part of the Swift 5.7 programming language, it requires macOS 13 (Ventura) to work ???

I also tried to build on the official Swift 5.7 on Amazon Linux 2 and the build fails because it can not find the Regex classes.

So it is not possible to use this language feature (part of Swift 5.7) with Swift 5.7 on macOS 12 or Linux ?

 error: cannot find type 'Regex' in scope
    let end: Regex

Try adding

    platforms: [.macOS(.v13)],

in your Package.swift and try on Linux again.

For Linux or macOS command line you need to pass -enable-bare-slash-regex to swift build - Xcode does this automatically

1 Like

Thank you @0xTim

Compiling with

swift build -Xswiftc -enable-bare-slash-regex

Works on Linux, but not on macOS.

The platforms: [.macOS(.v13)], does not seem to change the compiler behavior however

Xcode 14.0 ships with the old macOS SDK and therefore not with the Swift 5.7 standard library. Try on iOS or with an Xcode 14 beta (Xcode 14.1 works but also older 14.0 betas because they contain the macOS beta SDK).

Sorry about that, see other more useful comments - really looking forward to swift-evolution/0362-piecemeal-future-features.md at main · apple/swift-evolution · GitHub !

Thank you all for your help.
Based on your answers, I found some solutions.

On Linux, using Swift 5.7 : the below works (compile and run)

swift --version 
Swift version 5.7 (swift-5.7-RELEASE)
Target: aarch64-unknown-linux-gnu

swift build -Xswiftc -enable-bare-slash-regex
Building for debugging...
[4/4] Emitting module mm
Build complete! (0.60s)

On macOS 12, it is not enough, I also add to install Xcode 14 beta 3

Compiling from Xcode does not work. Compiling from the command line using the above command works too.

swift --version 
swift-driver version: 1.62.15 Apple Swift version 5.7.1 (swiftlang-5.7.1.134.3 clang-1400.0.29.51)
Target: arm64-apple-macosx12.0

swift build -Xswiftc -enable-bare-slash-regex
Building for debugging...
[3/3] Linking mm
Build complete! (0.59s)

Runnin on macOS 12 does not work :

swift run -Xswiftc -enable-bare-slash-regex                                        
Building for debugging...
Build complete! (0.13s)

dyld[31783]: Library not loaded: '/usr/lib/swift/libswift_StringProcessing.dylib'
  Referenced from: '/Users/stormacq/Documents/amazon/code/amplify/amplify-ios-workshop/migrate_markdown/.build/arm64-apple-macosx/debug/mm'
  Reason: tried: '/usr/lib/swift/libswift_StringProcessing.dylib' (no such file), '/usr/local/lib/libswift_StringProcessing.dylib' (no such file), '/usr/lib/libswift_StringProcessing.dylib' (no such file)
[1]    31783 abort      swift run -Xswiftc -enable-bare-slash-regex

Swift's Regex facilities are OS limited, so you'll need to wait for Ventura to use them on macOS.

1 Like

Right. The Regex type is in the standard library, which is distributed as part of the OS, so it's available only when targeting macOS 13.

1 Like

Hello All,

Now that Xcode 14.1 and macOS 13 Ventura are available : I still can not get Xcode 14.1 to compile and run code with regex literals. This is a Swift PM only project - no .xcodeproj file, only Package.swift

Code :

    let markdownFiles = /.md$/

Xcode Error : '/' is not a postfix unary operator

Package.swift :

    platforms: [
       .macOS(.v13)
    ],

Building and running from the command line, using -Xswiftc -enable-bare-slash-regex works.

How to get Xcode 14.1 recognise the new syntax ?

You mean you opened the Package in Xcode? In that case since you can't set the setting in Xcode like a normal project, you have to set it in the package by passing what you passed at the command line as an unsafe flags in your Package.

I'm not sure if you're aware of it, but there is also the #/.../# syntax which works without any flags. That is what I would personally recommend for as long as your package needs to continue building on 5.7.

IMO, the rollout of bare-slash regexes has been less than successful. The efforts to try to mitigate source-breakage appear to have resulted in worse incompatibilities and greater confusion than we would have had to suffer in the first place.

Thank you @Jon_Shier and @Karl
I read and understand the constraints with simple / ... / syntax

I switched to the #/ ... /# syntax which is natively recognised. Thank you.

Unfortunately, most blog outlets picked up the / ... / syntax when Swift 5.7 went out. And none (that I found) are sharing details about additional command line switches required to make their examples compile.

1 Like