Skip to content

firstMatch of any RegexComponent: SwiftCompile failed #84137

@stefanspringer1

Description

@stefanspringer1

Description

The following code leads to the message Command SwiftCompile failed with a nonzero exit code in Xcode:

struct X {
    
    nonisolated(unsafe) let pattern: any RegexComponent
    
    func f() {
        let _ = "hello".contains(pattern) // OK
        let _ = "hello".firstMatch(of: pattern) // Command SwiftCompile failed with a nonzero exit code
    }
    
}

The strange thing is that "hello".contains(pattern) compiles (and works), but "hello".firstMatch(of: pattern) does not.

Reproduction

Try to compile the code contained in the above description.

Stack dump

0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/stefan/Projekte/SPMTest/Sources/main.swift -emit-dependencies-path /Users/stefan/Projekte/SPMTest/.build/arm64-apple-macosx/debug/SPMTest.build/main.d -emit-reference-dependencies-path /Users/stefan/Projekte/SPMTest/.build/arm64-apple-macosx/debug/SPMTest.build/main.swiftdeps -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk -I /Users/stefan/Projekte/SPMTest/.build/arm64-apple-macosx/debug/Modules -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/PrivateFrameworks -color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /Users/stefan/Projekte/SPMTest/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -entry-point-function-name SPMTest_main -enable-upcoming-feature ConciseMagicFile -enable-upcoming-feature BareSlashRegexLiterals -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/stefan/Projekte/SPMTest -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/PrivateFrameworks -Xcc -fPIC -Xcc -g -module-name SPMTest -package-name spmtest -disable-clang-spi -target-sdk-version 15.4 -target-sdk-name macosx15.4 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -in-process-plugin-server-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /Users/stefan/Projekte/SPMTest/.build/arm64-apple-macosx/debug/SPMTest.build/main.swift.o -index-store-path /Users/stefan/Projekte/SPMTest/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.	Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
2.	Compiling with effective version 5.10
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/stefan/Projekte/SPMTest/Sources/main.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(SPMTest.(file).X.f()@/Users/stefan/Projekte/SPMTest/Sources/main.swift:7:10)
5.	While type-checking statement at [/Users/stefan/Projekte/SPMTest/Sources/main.swift:7:14 - line:10:5] RangeText="{
        let _ = "hello".contains(pattern) // OK
        let _ = "hello".firstMatch(of: pattern) // Command SwiftCompile failed with a nonzero exit code
    "
6.	While type-checking declaration 0x1231e3f18 (at /Users/stefan/Projekte/SPMTest/Sources/main.swift:9:9)
7.	While evaluating request PatternBindingEntryRequest((unknown decl)@/Users/stefan/Projekte/SPMTest/Sources/main.swift:9:9, 0)
8.	While type-checking expression at [/Users/stefan/Projekte/SPMTest/Sources/main.swift:9:17 - line:9:47] RangeText=""hello".firstMatch(of: pattern"
9.	While type-checking-target starting at /Users/stefan/Projekte/SPMTest/Sources/main.swift:9:25
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x00000001068c6c28 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001068c4a60 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001068c7264 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x00000001990656a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000019902b88c pthread_kill + 296
5  libsystem_c.dylib        0x0000000198f34a3c abort + 124
6  swift-frontend           0x000000010183ef50 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 10900
7  swift-frontend           0x000000010186c5fc (anonymous namespace)::ExprRewriter::coerceOptionalToOptional(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 708
8  swift-frontend           0x000000010183cba8 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 1772
9  swift-frontend           0x00000001018613e0 (anonymous namespace)::ExprRewriter::closeExistential(swift::Expr*&, swift::constraints::ConstraintLocatorBuilder, bool) + 420
10 swift-frontend           0x000000010185fa64 (anonymous namespace)::ExprRewriter::finishApply(swift::ApplyExpr*, swift::Type, swift::constraints::ConstraintLocatorBuilder, swift::constraints::ConstraintLocatorBuilder) + 10980
11 swift-frontend           0x0000000101865d54 (anonymous namespace)::ExprRewriter::visitApplyExpr(swift::ApplyExpr*) + 300
12 swift-frontend           0x000000010183fb2c (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 60
13 swift-frontend           0x0000000101839ddc (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 2832
14 swift-frontend           0x00000001018391d0 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 6564
15 swift-frontend           0x0000000101b1b96c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 792
16 swift-frontend           0x0000000101b1b530 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 420
17 swift-frontend           0x0000000101b1cd14 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 200
18 swift-frontend           0x0000000101b1cf30 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 292
19 swift-frontend           0x0000000101c2a904 swift::PatternBindingEntryRequest::evaluate(swift::Evaluator&, swift::PatternBindingDecl*, unsigned int) const + 3356
20 swift-frontend           0x0000000101f982e0 swift::PatternBindingEntryRequest::OutputType swift::Evaluator::getResultUncached<swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType)::'lambda'()>(swift::PatternBindingEntryRequest const&, swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType)::'lambda'()) + 628
21 swift-frontend           0x0000000101f59ed4 swift::PatternBindingDecl::getCheckedPatternBindingEntry(unsigned int) const + 140
22 swift-frontend           0x0000000101b77a84 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 3984
23 swift-frontend           0x0000000101c1b174 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 792
24 swift-frontend           0x0000000101c20a64 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 288
25 swift-frontend           0x0000000101c1e3fc bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 312
26 swift-frontend           0x0000000101c1c9cc swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1488
27 swift-frontend           0x00000001020195ec swift::TypeCheckFunctionBodyRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()>(swift::TypeCheckFunctionBodyRequest const&, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()) + 640
28 swift-frontend           0x0000000101f7c1a8 swift::AbstractFunctionDecl::getTypecheckedBody() const + 160
29 swift-frontend           0x0000000101c6e7e0 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 912
30 swift-frontend           0x0000000101c74b18 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 624
31 swift-frontend           0x0000000101c6e434 swift::performTypeChecking(swift::SourceFile&) + 308
32 swift-frontend           0x0000000100ad8018 swift::CompilerInstance::performSema() + 248
33 swift-frontend           0x000000010071b274 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 772
34 swift-frontend           0x0000000100719fd8 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3716
35 swift-frontend           0x000000010069e0bc swift::mainEntry(int, char const**) + 5428
36 dyld                     0x0000000198c8ab98 start + 6076

Expected behavior

The compiler should not crash, and as "hello".contains(pattern) in the above code works well, "hello".firstMatch(of: pattern) should work as well.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

There is also the Swift forums topic https://forums.swift.org/t/firstmatch-of-any-regexcomponent-swiftcompile-failed/81982.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwareexistentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesexpressionsFeature: expressionsgeneric constraintsFeature → generics: generic constraintsgenericsFeature: generic declarations and typesimplicit existential openingFeature → existentials: implicit opening of existentials when passed to parameters of generic typeswift 6.2type checkerArea → compiler: Semantic analysistypesFeature: types

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions