swiftlang / swift

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

Compiler crash when using closure default value involving generics #75320

Open theblixguy opened 3 months ago

theblixguy commented 3 months ago

Description

This happened with some code I wrote using Combine, but below is a reproducer without a dependency on it.

Reproduction

protocol MyPublisher {
  associatedtype Output
  associatedtype Failure: Error
}

struct MyAnyPublisher<T, U: Error> {}

struct MyEmpty<T, U: Error>: MyPublisher {
  typealias Output = T
  typealias Failure = U
}

struct MyJust<T>: MyPublisher {
    typealias Output = T
    typealias Failure = Never

    init(_ value: T) {}
}

extension MyPublisher where Output == Bool {
  func switchPublisher<P: MyPublisher>(
    ifTrue makeTruePublisher: @escaping () -> P = { MyEmpty<P.Output, Failure>() },
    ifFalse makeFalsePublisher: @escaping () -> P = { MyEmpty<P.Output, Failure>() }
  ) -> MyAnyPublisher<P.Output, Failure> where P.Failure == Failure {
    fatalError("unimplemented")
  }
}

MyJust(true)
 .switchPublisher(ifTrue: { MyJust(10) })

Stack dump

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 /Users/suyash.x.srijan/Documents/Personal_Projects/Test/Test/ContentView.swift /Users/suyash.x.srijan/Documents/Personal_Projects/Test/Test/TestApp.swift -primary-file /Users/suyash.x.srijan/Documents/Personal_Projects/Test/Test/Test.swift /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/DerivedSources/GeneratedAssetSymbols.swift -emit-dependencies-path /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/arm64/Test.d -emit-const-values-path /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/arm64/Test.swiftconstvalues -emit-reference-dependencies-path /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/arm64/Test.swiftdeps -serialize-diagnostics-path /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/arm64/Test.dia -emit-localized-strings -emit-localized-strings-path /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/arm64 -target arm64-apple-ios17.4-simulator -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.5.sdk -I /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Products/Debug-iphonesimulator -F /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Products/Debug-iphonesimulator/PackageFrameworks -F /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Products/Debug-iphonesimulator -no-color-diagnostics -enable-testing -g -module-cache-path /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -serialize-debugging-options -const-gather-protocols-file /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/arm64/Test_const_extract_protocols.json -enable-experimental-feature FullTypedThrows -enable-bare-slash-regex -strict-concurrency=complete -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/suyash.x.srijan/Documents/Personal_Projects/Test -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -ivfsstatcache -Xcc /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/iphonesimulator17.5-21F77-c098706a9f71eba4e76ae92ab367209a.sdkstatcache -Xcc -I/Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Test-generated-files.hmap -Xcc -I/Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Test-own-target-headers.hmap -Xcc -I/Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Test-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Test-project-headers.hmap -Xcc -I/Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/DerivedSources-normal/arm64 -Xcc -I/Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/DerivedSources/arm64 -Xcc -I/Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/DerivedSources -Xcc -DDEBUG=1 -module-name Test -frontend-parseable-output -disable-clang-spi -target-sdk-version 17.5 -target-sdk-name iphonesimulator17.5 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.5.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.5.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/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Build/Intermediates.noindex/Test.build/Debug-iphonesimulator/Test.build/Objects-normal/arm64/Test.o -index-unit-output-path /Test.build/Debug-iphonesimulator/Test.build/Objects-normal/arm64/Test.o -index-store-path /Users/suyash.x.srijan/Library/Developer/Xcode/DerivedData/Test-deialraovnnbmlfntwdrfpyjipnb/Index.noindex/DataStore -index-system-modules
1.  Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
2.  Compiling with the current language version
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/suyash.x.srijan/Documents/Personal_Projects/Test/Test/Test.swift")
4.  While silgen emitFunction SIL function "@$s4Test3fooyyF".
 for 'foo()' (at /Users/suyash.x.srijan/Documents/Personal_Projects/Test/Test/Test.swift:39:1)
5.  While emitting reabstraction thunk in SIL function "@$s4Test7MyEmptyVySis5NeverOGIegd_AA0B4JustVySiGIegd_TR".
 for <<debugloc at "<compiler-generated>":0:0>>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           0x00000001081d3f3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001081d30f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001081d4544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x0000000194aa3584 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000194a72c20 pthread_kill + 288
5  libsystem_c.dylib        0x000000019497fa30 abort + 180
6  swift-frontend           0x0000000102add6d8 PrettyStackTraceFrontend::~PrettyStackTraceFrontend() + 0
7  swift-frontend           0x000000010811ca18 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x000000010811c900 llvm::report_fatal_error(llvm::Twine const&, bool) + 0
9  swift-frontend           0x00000001031e3a78 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 6968
10 swift-frontend           0x00000001031f9878 (anonymous namespace)::ResultPlanner::execute(llvm::SmallVectorImpl<swift::SILValue>&, llvm::SmallVectorImpl<swift::SILValue>&)::$_24::operator()((anonymous namespace)::ResultPlanner::Operation&, bool, bool) const + 516
11 swift-frontend           0x00000001031e6cec (anonymous namespace)::ResultPlanner::execute(swift::SILValue, swift::CanTypeWrapper<swift::SILFunctionType>) + 1060
12 swift-frontend           0x00000001031ec978 buildThunkBody(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::CanType) + 1632
13 swift-frontend           0x00000001031ebffc createThunk(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 3484
14 swift-frontend           0x00000001031e251c (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 1500
15 swift-frontend           0x0000000103150864 swift::Lowering::Conversion::emit(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::SGFContext) const + 508
16 swift-frontend           0x00000001030e3d38 (anonymous namespace)::ScalarResultPlan::finish(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::Initialization*, swift::SILFunctionTypeRepresentation) + 616
17 swift-frontend           0x00000001030e39d4 (anonymous namespace)::ScalarResultPlan::finish(swift::Lowering::SILGenFunction&, swift::SILLocation, llvm::ArrayRef<swift::Lowering::ManagedValue>&, swift::SILValue) + 512
18 swift-frontend           0x00000001030ff98c swift::Lowering::SILGenFunction::emitApply(std::__1::unique_ptr<swift::Lowering::ResultPlan, std::__1::default_delete<swift::Lowering::ResultPlan>>&&, swift::Lowering::ArgumentScope&&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SubstitutionMap, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::Lowering::CalleeTypeInfo const&, swift::OptionSet<swift::ApplyFlags, unsigned char>, swift::Lowering::SGFContext, llvm::Optional<swift::ActorIsolation>) + 2224
19 swift-frontend           0x000000010316e3cc swift::Lowering::SILGenFunction::emitApplyOfDefaultArgGenerator(swift::SILLocation, swift::ConcreteDeclRef, unsigned int, swift::CanType, bool, swift::Lowering::SGFContext) + 1256
20 swift-frontend           0x0000000103117094 (anonymous namespace)::DelayedArgument::emit(swift::Lowering::SILGenFunction&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, unsigned long&) + 480
21 swift-frontend           0x00000001031013c0 emitDelayedArguments(swift::Lowering::SILGenFunction&, llvm::MutableArrayRef<(anonymous namespace)::DelayedArgument>, llvm::MutableArrayRef<llvm::SmallVector<swift::Lowering::ManagedValue, 4u>>) + 424
22 swift-frontend           0x000000010311da04 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, swift::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 1056
23 swift-frontend           0x0000000103105d68 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 4064
24 swift-frontend           0x00000001031037bc swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 3060
25 swift-frontend           0x000000010316f618 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 892
26 swift-frontend           0x0000000103207684 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 5032
27 swift-frontend           0x000000010319e538 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 632
28 swift-frontend           0x00000001030f2e48 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 8344
29 swift-frontend           0x00000001030f35bc swift::Lowering::SILGenModule::emitOrDelayFunction(swift::SILDeclRef) + 172
30 swift-frontend           0x00000001030f0d9c swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 292
31 swift-frontend           0x00000001030f6b54 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1668
32 swift-frontend           0x0000000103205f1c swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 196
33 swift-frontend           0x00000001030f9f44 llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 584
34 swift-frontend           0x0000000102ad1408 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 548
35 swift-frontend           0x0000000102ad5694 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1448
36 swift-frontend           0x0000000102ad36d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
37 swift-frontend           0x0000000102a62e8c swift::mainEntry(int, char const**) + 2612
38 dyld                     0x00000001946ea0e0 start + 2360
Command SwiftCompile failed with a nonzero exit code

Expected behavior

Code does not crash

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-macosx14.0

Additional information

No response

AnthonyLatsis commented 3 months ago

Reduction:

struct G<T> {}

struct Test {
  static func test<T>(_: T, _: () -> T = { G<T>() }) {}
//  static func test<T>(_: T, _: T = G<T>()) {} // A simpler undetected inference cycle
}

Test.test(false)