Skip to content

Compiler crash with if else expression in result builder (composable architecture reducer body) #71040

@KaiOelfke

Description

@KaiOelfke

Description

Using if else expressions of Swift 5.9 in a composable architecture reducer always fails to compile for me as the compiler crashes. The following minimal sample code reproduces this for the latest Xcode in a new and clean project.

TCAIfElseExpression.zip

Reproduction

import ComposableArchitecture

@Reducer
struct Feature {
  struct State {
    var value: Bool
  }
  enum Action {
    case dummy
  }
  var body: some ReducerOf<Self> {
    Reduce { state, _ in
      state.value = if true {
        true
      } else {
        false
      }
      return .none
    }
  }
}

Stack dump

0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/kaioelfke/Desktop/TCAIfElseExpression/TCAIfElseExpression/ContentView.swift /Users/kaioelfke/Desktop/TCAIfElseExpression/TCAIfElseExpression/TCAIfElseExpressionApp.swift /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/DerivedSources/GeneratedAssetSymbols.swift -emit-dependencies-path /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/Objects-normal/arm64/ContentView.d -emit-const-values-path /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/Objects-normal/arm64/ContentView.swiftconstvalues -emit-reference-dependencies-path /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/Objects-normal/arm64/ContentView.swiftdeps -serialize-diagnostics-path /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/Objects-normal/arm64/ContentView.dia -emit-localized-strings -emit-localized-strings-path /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/Objects-normal/arm64 -target arm64-apple-ios17.2 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk -I /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/PackageFrameworks -F /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos -no-color-diagnostics -enable-testing -g -module-cache-path /Users/kaioelfke/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -serialize-debugging-options -load-plugin-executable /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug/CasePathsMacros#CasePathsMacros -load-plugin-executable /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug/ComposableArchitectureMacros#ComposableArchitectureMacros -load-plugin-executable /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug/DependenciesMacrosPlugin#DependenciesMacrosPlugin -const-gather-protocols-file /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/Objects-normal/arm64/TCAIfElseExpression_const_extract_protocols.json -enable-bare-slash-regex -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/kaioelfke/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/kaioelfke/Desktop/TCAIfElseExpression -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -ivfsstatcache -Xcc /Users/kaioelfke/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/iphoneos17.2-21C52-884b7f60ac6761a492c03f282b824eb9.sdkstatcache -Xcc -I/Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/TCAIfElseExpression-generated-files.hmap -Xcc -I/Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/TCAIfElseExpression-own-target-headers.hmap -Xcc -I/Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/TCAIfElseExpression-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/TCAIfElseExpression-project-headers.hmap -Xcc -I/Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Products/Debug-iphoneos/include -Xcc -I/Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/DerivedSources-normal/arm64 -Xcc -I/Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/DerivedSources/arm64 -Xcc -I/Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/DerivedSources -Xcc -DDEBUG=1 -module-name TCAIfElseExpression -frontend-parseable-output -disable-clang-spi -target-sdk-version 17.2 -target-sdk-name iphoneos17.2 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk/usr/bin/swift-plugin-server -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 -o /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Build/Intermediates.noindex/TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/Objects-normal/arm64/ContentView.o -index-unit-output-path /TCAIfElseExpression.build/Debug-iphoneos/TCAIfElseExpression.build/Objects-normal/arm64/ContentView.o -index-store-path /Users/kaioelfke/Library/Developer/Xcode/DerivedData/TCAIfElseExpression-bmqctoidpaeivvearwtlubuaahfz/Index.noindex/DataStore -index-system-modules
1.	Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/kaioelfke/Desktop/TCAIfElseExpression/TCAIfElseExpression/ContentView.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(TCAIfElseExpression.(file).Feature._@/Users/kaioelfke/Desktop/TCAIfElseExpression/TCAIfElseExpression/ContentView.swift:11:34)
5.	While type-checking statement at [/Users/kaioelfke/Desktop/TCAIfElseExpression/TCAIfElseExpression/ContentView.swift:12:12 - line:19:5] RangeText="{ state, _ in
      state.value = if true {
        true
      } else {
        false
      }
      return .none
    "
6.	While type-checking expression at [/Users/kaioelfke/Desktop/TCAIfElseExpression/TCAIfElseExpression/ContentView.swift:13:7 - line:17:7] RangeText="state.value = if true {
        true
      } else {
        false
      "
7.	While type-checking-target starting at /Users/kaioelfke/Desktop/TCAIfElseExpression/TCAIfElseExpression/ContentView.swift:13:19
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           0x0000000106d55abc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001099bbcb0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109725054 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x0000000188681a24 _sigtramp + 56
4  swift-frontend           0x0000000106354030 (anonymous namespace)::SyntacticElementSolutionApplication::SyntacticElementSolutionApplication(swift::constraints::Solution&, (anonymous namespace)::SyntacticElementContext, std::__1::function<llvm::Optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>) + 556
5  swift-frontend           0x0000000106354030 (anonymous namespace)::SyntacticElementSolutionApplication::SyntacticElementSolutionApplication(swift::constraints::Solution&, (anonymous namespace)::SyntacticElementContext, std::__1::function<llvm::Optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>) + 556
6  swift-frontend           0x00000001084e2da0 swift::constraints::ConstraintSystem::applySolutionToSingleValueStmt(swift::constraints::Solution&, swift::SingleValueStmtExpr*, swift::DeclContext*, std::__1::function<llvm::Optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>) + 144
7  swift-frontend           0x00000001083f22a0 (anonymous namespace)::ExprWalker::walkToExprPre(swift::Expr*) + 3588
8  swift-frontend           0x0000000105a1ca2c (anonymous namespace)::Traversal::visitAssignExpr(swift::AssignExpr*) + 168
9  swift-frontend           0x00000001045d0020 (anonymous namespace)::Traversal::visit(swift::Expr*) (.llvm.16965159650233048978) + 1052
10 swift-frontend           0x00000001083e288c (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 2688
11 swift-frontend           0x00000001062dd79c swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 6956
12 swift-frontend           0x0000000106921b1c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 756
13 swift-frontend           0x00000001090bae4c swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 436
14 swift-frontend           0x00000001096e1460 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) (.llvm.3900483966656927864) + 396
15 swift-frontend           0x0000000106bd8840 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) (.llvm.3900483966656927864) + 296
16 swift-frontend           0x00000001096ec9d8 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) (.llvm.3900483966656927864) + 316
17 swift-frontend           0x0000000106bd1bbc swift::TypeChecker::typeCheckClosureBody(swift::ClosureExpr*) + 324
18 swift-frontend           0x00000001062dd8c0 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 7248
19 swift-frontend           0x00000001062c28cc swift::TypeChecker::applyResultBuilderBodyTransform(swift::FuncDecl*, swift::Type, bool) + 2672
20 swift-frontend           0x00000001096e8f10 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 404
21 swift-frontend           0x00000001072d4e58 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 884
22 swift-frontend           0x0000000106c829a8 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 608
23 swift-frontend           0x0000000106c88658 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 664
24 swift-frontend           0x0000000106c66a4c swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 228
25 swift-frontend           0x000000010733da8c swift::CompilerInstance::performSema() + 160
26 swift-frontend           0x0000000109298504 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 464
27 swift-frontend           0x000000010929c854 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
28 swift-frontend           0x0000000109303d44 swift::mainEntry(int, char const**) + 4408
29 dyld                     0x00000001882d10e0 start + 2360

Expected behavior

It should compile.

Environment

Xcode Version 15.2 (15C500b)
macOS 14.2.1 (23C71)

Additional information

No response

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.closuresFeature: closurescompilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwareexpressionsFeature: expressionsif elseFeature → statements: 'if' statementsresult buildersFeature: Result buildersswift 5.9type checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions