Skip to content
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

Equivalent guard-case expressions produce different results in Swift Testing’s #expect (compiler crash) #76765

Closed
yakovmanshin opened this issue Sep 28, 2024 · 2 comments
Assignees
Labels
availability The @available attribute and availability checking in general bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@yakovmanshin
Copy link

Description

Two seemingly equivalent guard-case expressions result in different outcomes when they’re used inside an #expect call in Swift Testing:

// Compiles fine:
guard case .keyNotFound(let key, _) = error as? DecodingError else { return false }

// “Command SwiftCompile failed with a nonzero exit code:”
guard case DecodingError.keyNotFound(let key, _) = error else { return false }

I reported this in the Swift Testing repo. @stmontgomery suggested this could be caused by a compiler crash.

Crash backtrace
SwiftCompile normal x86_64 Compiling\ SomeAppTests.swift /Users/UserName/path/to/SomeApp/SomeAppTests/SomeAppTests.swift (in target 'SomeAppTests' from project 'SomeApp')
    cd /Users/UserName/path/to/SomeApp
    builtin-swiftTaskExecution -- /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/UserName/path/to/SomeApp/SomeAppTests/SomeAppTests.swift -emit-dependencies-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.d -emit-const-values-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.swiftconstvalues -emit-reference-dependencies-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.swiftdeps -serialize-diagnostics-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.dia -target x86_64-apple-ios18.0-simulator -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator18.0.sdk -I /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Products/Debug-iphonesimulator -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/lib -F /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Products/Debug-iphonesimulator -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator18.0.sdk/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -debug-info-format\=dwarf -dwarf-version\=5 -module-cache-path /Users/UserName/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -profile-generate -profile-coverage-mapping -swift-version 5 -enforce-exclusivity\=checked -Onone -D DEBUG -serialize-debugging-options -const-gather-protocols-file /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests_const_extract_protocols.json -enable-experimental-feature DebugDescriptionMacro -enable-experimental-feature OpaqueTypeErasure -enable-bare-slash-regex -empty-abi-descriptor -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins/testing -validate-clang-modules-once -clang-build-session-file /Users/UserName/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/UserName/path/to/SomeApp -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/UserName/path/to/SomeApp -Xcc -D_LIBCPP_HARDENING_MODE\=_LIBCPP_HARDENING_MODE_DEBUG -Xcc -ivfsstatcache -Xcc /Users/UserName/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/iphonesimulator18.0-22A348-db63dc9361471f152f572502bdbfe70a.sdkstatcache -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/SomeAppTests-generated-files.hmap -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/SomeAppTests-own-target-headers.hmap -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/SomeAppTests-all-target-headers.hmap -Xcc -iquote -Xcc /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/SomeAppTests-project-headers.hmap -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/DerivedSources-normal/x86_64 -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/DerivedSources/x86_64 -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/DerivedSources -Xcc -DDEBUG\=1 -module-name SomeAppTests -frontend-parseable-output -disable-clang-spi -target-sdk-version 18.0 -target-sdk-name iphonesimulator18.0 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib/swift/host/plugins\#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/local/lib/swift/host/plugins\#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/swift-plugin-server -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 -parse-as-library -o /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.o -index-unit-output-path /SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.o -index-store-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Index.noindex/DataStore -index-system-modules

Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/UserName/path/to/SomeApp/SomeAppTests/SomeAppTests.swift -emit-dependencies-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.d -emit-const-values-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.swiftconstvalues -emit-reference-dependencies-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.swiftdeps -serialize-diagnostics-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.dia -target x86_64-apple-ios18.0-simulator -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator18.0.sdk -I /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Products/Debug-iphonesimulator -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/lib -F /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Products/Debug-iphonesimulator -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator18.0.sdk/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=5 -module-cache-path /Users/UserName/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -profile-generate -profile-coverage-mapping -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -serialize-debugging-options -const-gather-protocols-file /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests_const_extract_protocols.json -enable-experimental-feature DebugDescriptionMacro -enable-experimental-feature OpaqueTypeErasure -enable-bare-slash-regex -empty-abi-descriptor -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins/testing -validate-clang-modules-once -clang-build-session-file /Users/UserName/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/UserName/path/to/SomeApp -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/UserName/path/to/SomeApp -Xcc -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -Xcc -ivfsstatcache -Xcc /Users/UserName/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/iphonesimulator18.0-22A348-db63dc9361471f152f572502bdbfe70a.sdkstatcache -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/SomeAppTests-generated-files.hmap -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/SomeAppTests-own-target-headers.hmap -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/SomeAppTests-all-target-headers.hmap -Xcc -iquote -Xcc /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/SomeAppTests-project-headers.hmap -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/DerivedSources-normal/x86_64 -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/DerivedSources/x86_64 -Xcc -I/Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/DerivedSources -Xcc -DDEBUG=1 -module-name SomeAppTests -frontend-parseable-output -disable-clang-spi -target-sdk-version 18.0 -target-sdk-name iphonesimulator18.0 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/swift-plugin-server -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 -parse-as-library -o /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Build/Intermediates.noindex/SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.o -index-unit-output-path /SomeApp.build/Debug-iphonesimulator/SomeAppTests.build/Objects-normal/x86_64/SomeAppTests.o -index-store-path /Users/UserName/Library/Developer/Xcode/DerivedData/SomeApp-fluofojhgcixbzfjyazqcykrqoeb/Index.noindex/DataStore -index-system-modules
1.	Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
2.	Compiling with effective version 5.10
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/UserName/path/to/SomeApp/SomeAppTests/SomeAppTests.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(SomeAppTests.(file).SomeAppTests.someTest()@/Users/UserName/path/to/SomeApp/SomeAppTests/SomeAppTests.swift:9:16)
5.	While type-checking statement at [/Users/UserName/path/to/SomeApp/SomeAppTests/SomeAppTests.swift:9:27 - line:21:5] RangeText="{
        #expect {
            try someThrowingFunction()
        } throws: { error in
            // This works:
//            guard case .keyNotFound(let key, _) = error as? DecodingError else { return false }
            
            // This doesn’t:
            guard case DecodingError.keyNotFound(let key, _) = error else { return false }
            
            return key.stringValue == "expectedKey"
        }
    "
6.	While type-checking expression at [/Users/UserName/path/to/SomeApp/SomeAppTests/SomeAppTests.swift:10:9 - line:20:9] RangeText="#expect {
            try someThrowingFunction()
        } throws: { error in
            // This works:
//            guard case .keyNotFound(let key, _) = error as? DecodingError else { return false }
            
            // This doesn’t:
            guard case DecodingError.keyNotFound(let key, _) = error else { return false }
            
            return key.stringValue == "expectedKey"
        "
7.	While type-checking-target starting at /Users/UserName/path/to/SomeApp/SomeAppTests/SomeAppTests.swift:10:9
8.	While evaluating request ExpandMacroExpansionExprRequest(expression)
9.	While type checking expanded macro expression at [@__swiftmacro_12SomeAppTests0023SomeAppTestsswift_elFCffMX9_8_6expectfMf_.swift:1:1 - line:13:166] RangeText="Testing.__checkClosureCall(performing: {
            try someThrowingFunction()
        }, throws: { error in
            // This works:
//            guard case .keyNotFound(let key, _) = error as? DecodingError else { return false }

            // This doesn’t:
            guard case DecodingError.keyNotFound(let key, _) = error else {
                    return false
                }

            return key.stringValue == "expectedKey"
        }, expression: .__fromSyntaxNode("try someThrowingFunction()"), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected("
10.	While type-checking expression at [@__swiftmacro_12SomeAppTests0023SomeAppTestsswift_elFCffMX9_8_6expectfMf_.swift:1:1 - line:13:166] RangeText="Testing.__checkClosureCall(performing: {
            try someThrowingFunction()
        }, throws: { error in
            // This works:
//            guard case .keyNotFound(let key, _) = error as? DecodingError else { return false }

            // This doesn’t:
            guard case DecodingError.keyNotFound(let key, _) = error else {
                    return false
                }

            return key.stringValue == "expectedKey"
        }, expression: .__fromSyntaxNode("try someThrowingFunction()"), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected("
11.	While type-checking-target starting at @__swiftmacro_12SomeAppTests0023SomeAppTestsswift_elFCffMX9_8_6expectfMf_.swift:13:155
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           0x00000001099cd088 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 40
1  swift-frontend           0x00000001099cae59 llvm::sys::RunSignalHandlers() + 233
2  swift-frontend           0x00000001099cd646 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007ff81a538e9d _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603370139251072
5  swift-frontend           0x0000000104cb4c92 swift::ExportContext::forFunctionBody(swift::DeclContext*, swift::SourceLoc) + 258
6  swift-frontend           0x0000000104cc8b6c (anonymous namespace)::StmtAvailabilityWalker::walkToPatternPre(swift::Pattern*) + 60
7  swift-frontend           0x000000010521ad25 (anonymous namespace)::Traversal::doIt(llvm::MutableArrayRef<swift::StmtConditionElement> const&) + 197
8  swift-frontend           0x000000010521df9f (anonymous namespace)::Traversal::visit(swift::Stmt*) + 1327
9  swift-frontend           0x0000000104c2797f swift::performStmtDiagnostics(swift::Stmt const*, swift::DeclContext*) + 9695
10 swift-frontend           0x0000000104a402f9 (anonymous namespace)::SyntacticElementSolutionApplication::visit(swift::Stmt*, bool) + 7657
11 swift-frontend           0x0000000104a4003e (anonymous namespace)::SyntacticElementSolutionApplication::visit(swift::Stmt*, bool) + 6958
12 swift-frontend           0x0000000104a3c9c2 (anonymous namespace)::SyntacticElementSolutionApplication::apply() + 130
13 swift-frontend           0x0000000104a3c823 swift::constraints::ConstraintSystem::applySolutionToBody(swift::constraints::Solution&, swift::AnyFunctionRef, swift::DeclContext*&, std::__1::function<std::__1::optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>) + 419
14 swift-frontend           0x00000001049e01d7 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 8359
15 swift-frontend           0x0000000104d015b2 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 770
16 swift-frontend           0x0000000104d0117f swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 431
17 swift-frontend           0x0000000104daa466 swift::expandMacroExpr(swift::MacroExpansionExpr*) + 1526
18 swift-frontend           0x0000000104da9e37 swift::ExpandMacroExpansionExprRequest::evaluate(swift::Evaluator&, swift::MacroExpansionExpr*) const + 439
19 swift-frontend           0x00000001049fa0ed swift::ExpandMacroExpansionExprRequest::OutputType swift::Evaluator::getResultUncached<swift::ExpandMacroExpansionExprRequest, swift::ExpandMacroExpansionExprRequest::OutputType swift::evaluateOrDefault<swift::ExpandMacroExpansionExprRequest>(swift::Evaluator&, swift::ExpandMacroExpansionExprRequest, swift::ExpandMacroExpansionExprRequest::OutputType)::'lambda'()>(swift::ExpandMacroExpansionExprRequest const&, swift::ExpandMacroExpansionExprRequest::OutputType swift::evaluateOrDefault<swift::ExpandMacroExpansionExprRequest>(swift::Evaluator&, swift::ExpandMacroExpansionExprRequest, swift::ExpandMacroExpansionExprRequest::OutputType)::'lambda'()) + 733
20 swift-frontend           0x00000001049f9d57 swift::ExpandMacroExpansionExprRequest::OutputType swift::Evaluator::getResultCached<swift::ExpandMacroExpansionExprRequest, swift::ExpandMacroExpansionExprRequest::OutputType swift::evaluateOrDefault<swift::ExpandMacroExpansionExprRequest>(swift::Evaluator&, swift::ExpandMacroExpansionExprRequest, swift::ExpandMacroExpansionExprRequest::OutputType)::'lambda'(), (void*)0>(swift::ExpandMacroExpansionExprRequest const&, swift::ExpandMacroExpansionExprRequest::OutputType swift::evaluateOrDefault<swift::ExpandMacroExpansionExprRequest>(swift::Evaluator&, swift::ExpandMacroExpansionExprRequest, swift::ExpandMacroExpansionExprRequest::OutputType)::'lambda'()) + 311
21 swift-frontend           0x00000001049f77c1 (anonymous namespace)::ExprRewriter::walkToExprPost(swift::Expr*) + 47121
22 swift-frontend           0x00000001049e84d5 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 21
23 swift-frontend           0x00000001049e1595 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 4037
24 swift-frontend           0x00000001049e0072 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 8002
25 swift-frontend           0x0000000104d015b2 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 770
26 swift-frontend           0x0000000104d0117f swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 431
27 swift-frontend           0x0000000104e079cd (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 413
28 swift-frontend           0x0000000104e0d32c swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 252
29 swift-frontend           0x0000000104e0ac1a bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 314
30 swift-frontend           0x0000000104e094ae swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1646
31 swift-frontend           0x000000010534780d 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'()) + 781
32 swift-frontend           0x00000001052acec7 swift::AbstractFunctionDecl::getTypecheckedBody() const + 151
33 swift-frontend           0x0000000104e61109 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 969
34 swift-frontend           0x0000000104e68e44 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'()) + 772
35 swift-frontend           0x0000000104e60d21 swift::performTypeChecking(swift::SourceFile&) + 337
36 swift-frontend           0x0000000103bd0069 swift::CompilerInstance::performSema() + 281
37 swift-frontend           0x00000001037a6f86 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 918
38 swift-frontend           0x00000001037a5d58 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4392
39 swift-frontend           0x00000001037186a7 swift::mainEntry(int, char const**) + 8103
40 dyld                     0x00007ff81a1762cd start + 1805

Reproduction

I was able to reproduce the issue in multiple iOS projects, including a newly-created one.

The following code causes the crash:

#expect {
    try someThrowingFunction()
} throws: { error in
    guard case DecodingError.keyNotFound(let key, _) = error else { return false }
    return key.stringValue == "expectedKey"
}
Complete file
@testable import SomeApp

import Testing

func someThrowingFunction() throws { }

struct SomeAppTests {
    
    @Test func someTest() {
        #expect {
            try someThrowingFunction()
        } throws: { error in
            // This works:
//            guard case .keyNotFound(let key, _) = error as? DecodingError else { return false }
            
            // This doesn’t:
            guard case DecodingError.keyNotFound(let key, _) = error else { return false }
            
            return key.stringValue == "expectedKey"
        }
    }
    
}

Expected behavior

The two guard-case expressions can be used interchangeably.

Environment

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: x86_64-apple-macosx15.0
  • macOS 15.0 (24A335)
  • Xcode 16.0 (16A242)

Additional information

@yakovmanshin yakovmanshin added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Sep 28, 2024
@tshortli tshortli added the availability The @available attribute and availability checking in general label Nov 6, 2024
@tshortli tshortli self-assigned this Nov 6, 2024
@tshortli
Copy link
Contributor

tshortli commented Nov 6, 2024

Thanks for the report, I'm able to reproduce the issue.

@tshortli
Copy link
Contributor

tshortli commented Nov 6, 2024

This reproduces with swiftlang-6.0.0.9.10 but doesn't reproduce with a compiler built from main. I won't be able to identify to a PR that fixed it without more investigation, but there has been a lot of work in this area on main in general. If you have a chance, please try with a recent development snapshot of Swift.

@tshortli tshortli closed this as completed Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
availability The @available attribute and availability checking in general bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants