swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.66k stars 10.38k forks source link

Generic result builder usage fails to type check and crashes the Swift 6 compiler #75790

Open hyperspacemark opened 3 months ago

hyperspacemark commented 3 months ago

Description

Usage of an if/else expression in a generic result builder that implements buildEither(first:) and buildEither(second:) fails to typecheck and crashes the Swift 6 compiler. It compiles fine on the Swift 5 compiler.

Reproduction

@resultBuilder
public enum ValueBuilder<Value> {
    public static func buildBlock(_ value: Value) -> Value {
        value
    }

    public static func buildEither(first value: Value) -> Value {
        value
    }

    public static func buildEither(second value: Value) -> Value {
        value
    }
}

func build<Value>(@ValueBuilder<Value> _ build: () -> Value) -> Value {
    build()
}

let result = build {
    if 32.isMultiple(of: 2) {
        "hello"
    } else {
        "world"
    }
}

Stack dump

error: compile command failed due to signal 11 (use -v to see invocation)
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file ValueBuilderCrash.swift -target arm64-apple-macosx14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -new-driver-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name ValueBuilderCrash -disable-clang-spi -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/sg/x3hw724n4sdc0vdy6n0gt5q40000gq/T/TemporaryDirectory.mfiBJb/ValueBuilderCrash-1.o
1.  Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1)
2.  Compiling with effective version 5.10
3.  While evaluating request TypeCheckSourceFileRequest(source_file "ValueBuilderCrash.swift")
4.  While type-checking statement at [ValueBuilderCrash.swift:20:1 - line:26:1] RangeText="let result = build {
    if 32.isMultiple(of: 2) {
        "hello"
    } else {
        "world"
    }
"
5.  While type-checking declaration 0x1439d8bf8 (at ValueBuilderCrash.swift:20:1)
6.  While evaluating request PatternBindingEntryRequest((unknown decl), 0)
7.  While type-checking expression at [ValueBuilderCrash.swift:20:14 - line:26:1] RangeText="build {
    if 32.isMultiple(of: 2) {
        "hello"
    } else {
        "world"
    }
"
8.  While type-checking-target starting at ValueBuilderCrash.swift:20:14
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           0x000000010a1f1194 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010a1ef3e8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010a1f1760 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x00000001944d2584 _sigtramp + 56
4  swift-frontend           0x0000000105d0f764 matchCallArguments(swift::constraints::ConstraintSystem&, swift::FunctionType*, swift::ArgumentList*, llvm::ArrayRef<swift::AnyFunctionType::Param>, llvm::ArrayRef<swift::AnyFunctionType::Param>, swift::constraints::ConstraintKind, swift::constraints::ConstraintLocatorBuilder, std::__1::optional<swift::constraints::TrailingClosureMatching>, llvm::SmallVectorImpl<std::__1::pair<swift::TypeVariableType*, swift::OpenedArchetypeType*>>&) + 9252
5  swift-frontend           0x0000000105d0c67c swift::constraints::ConstraintSystem::simplifyApplicableFnConstraint(swift::Type, swift::Type, std::__1::optional<swift::constraints::TrailingClosureMatching>, swift::optionset::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 4172
6  swift-frontend           0x0000000105d16ed8 swift::constraints::ConstraintSystem::addConstraintImpl(swift::constraints::ConstraintKind, swift::Type, swift::Type, swift::constraints::ConstraintLocatorBuilder, bool) + 1612
7  swift-frontend           0x0000000105ce37d8 swift::constraints::ConstraintSystem::addConstraint(swift::constraints::ConstraintKind, swift::Type, swift::Type, swift::constraints::ConstraintLocatorBuilder, bool) + 80
8  swift-frontend           0x0000000105cca3fc (anonymous namespace)::ConstraintGenerator::visitApplyExpr(swift::ApplyExpr*) + 740
9  swift-frontend           0x0000000105cb8dbc (anonymous namespace)::ConstraintWalker::walkToExprPost(swift::Expr*) + 740
10 swift-frontend           0x00000001062f7788 (anonymous namespace)::Traversal::visitTypeJoinExpr(swift::TypeJoinExpr*) + 304
11 swift-frontend           0x00000001062f5540 (anonymous namespace)::Traversal::visit(swift::Expr*) + 1232
12 swift-frontend           0x0000000105cb2b4c swift::constraints::ConstraintSystem::generateConstraints(swift::Expr*, swift::DeclContext*) + 156
13 swift-frontend           0x0000000105cb20ac swift::constraints::ConstraintSystem::generateConstraints(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 1776
14 swift-frontend           0x0000000105ca5ebc swift::constraints::ConstraintSystem::simplifySyntacticElementConstraint(swift::ASTNode, swift::constraints::ContextualTypeInfo, bool, swift::optionset::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 916
15 swift-frontend           0x0000000105d18680 swift::constraints::ConstraintSystem::simplifyConstraint(swift::constraints::Constraint const&) + 1876
16 swift-frontend           0x0000000105d34160 swift::constraints::ConjunctionElement::attempt(swift::constraints::ConstraintSystem&) const + 248
17 swift-frontend           0x0000000105d4118c swift::constraints::ConjunctionStep::attempt(swift::constraints::ConjunctionElement const&) + 284
18 swift-frontend           0x0000000105d42db4 swift::constraints::BindingStep<swift::constraints::ConjunctionElementProducer>::take(bool) + 852
19 swift-frontend           0x0000000105d2cc88 swift::constraints::ConstraintSystem::solveImpl(llvm::SmallVectorImpl<swift::constraints::Solution>&) + 500
20 swift-frontend           0x0000000105d30214 swift::constraints::ConstraintSystem::solve(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 60
21 swift-frontend           0x0000000105d2f37c swift::constraints::ConstraintSystem::solve(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 1096
22 swift-frontend           0x0000000105f1845c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 596
23 swift-frontend           0x0000000105f180e4 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 416
24 swift-frontend           0x0000000105f19720 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 172
25 swift-frontend           0x0000000105f199b0 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 356
26 swift-frontend           0x00000001060123a0 swift::PatternBindingEntryRequest::evaluate(swift::Evaluator&, swift::PatternBindingDecl*, unsigned int) const + 3264
27 swift-frontend           0x000000010639e2f4 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'()) + 624
28 swift-frontend           0x00000001063619d8 swift::PatternBindingDecl::getCheckedPatternBindingEntry(unsigned int) const + 144
29 swift-frontend           0x0000000105f6c30c (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 2552
30 swift-frontend           0x0000000105f6b904 swift::TypeChecker::typeCheckDecl(swift::Decl*) + 152
31 swift-frontend           0x0000000106008544 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 288
32 swift-frontend           0x0000000106005e84 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 316
33 swift-frontend           0x0000000106006060 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 148
34 swift-frontend           0x0000000106052970 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 696
35 swift-frontend           0x0000000106059fdc 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'()) + 620
36 swift-frontend           0x000000010605269c swift::performTypeChecking(swift::SourceFile&) + 328
37 swift-frontend           0x0000000104f77fe8 swift::CompilerInstance::performSema() + 260
38 swift-frontend           0x0000000104ba6690 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1528
39 swift-frontend           0x0000000104ba5458 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
40 swift-frontend           0x0000000104b2c50c swift::mainEntry(int, char const**) + 3680
41 dyld                     0x0000000194117154 start + 2476

Expected behavior

Usage of an if/else expression in the result builder should compile.

Environment

swift-driver version: 1.113 Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1) Target: arm64-apple-macosx14.0

Additional information

No response

xedin commented 3 months ago

Crash while trying to type-check synthesized type_join expression.