swiftlang / swift

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

Compiler crash when emitting SIL #64559

Open jpsim opened 1 year ago

jpsim commented 1 year ago

Description

The Swift compiler crashes when emitting SIL for the following Swift code:

// Logger.swift
final class StdoutOutputStream: TextOutputStream {
    func write(_ string: String) {}
}

final class DefaultLogger {
    private var stdout: TextOutputStream

    init(stdout: TextOutputStream = StdoutOutputStream()) {
        self.stdout = stdout
    }

    func logDebug(_ message: String) {
        print(message, to: &self.stdout)
    }
}

Steps to reproduce

swiftc Logger.swift

Expected behavior

Should compile successfully.

Environment

jpsim commented 1 year ago

With a recent trunk snapshot:

$ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-03-09-a.xctoolchain/usr/bin/swiftc Logger.swift
error: compile command failed due to signal 6 (use -v to see invocation)
Assertion failed: (def->getFunction() == &F && "def of root local archetype is in wrong function"), function addTo, file SILInstructions.cpp, line 118.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-03-09-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file Logger.swift -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -color-diagnostics -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-03-09-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-03-09-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-03-09-a.xctoolchain/usr/local/lib/swift/host/plugins -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-03-09-a.xctoolchain/usr/lib/swift -module-name Logger -o /var/folders/9w/qsshvl492tsfzlhz5bc26df80000gn/T/TemporaryDirectory.JDY7kR/Logger-1.o
1.  Apple Swift version 5.9-dev (LLVM b117a3b88f6fc9a, Swift 3211f68fbf352b1)
2.  Compiling with the current language version
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "Logger.swift")
4.  While silgen emitFunction SIL function "@$s6Logger07DefaultA0C8logDebugyySSF".
 for 'logDebug(_:)' (at Logger.swift:12:5)
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           0x0000000108f6f56c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000108f6e870 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x0000000108f6fbac SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000019e5d02a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000019e5a1cec pthread_kill + 288
5  libsystem_c.dylib        0x000000019e4da2c8 abort + 180
6  libsystem_c.dylib        0x000000019e4d9620 err + 0
7  swift-frontend           0x000000010926e0fc swift::OpenPackElementInst::getOpenedShapeClass() const (.cold.1) + 0
8  swift-frontend           0x0000000104f9ea94 (anonymous namespace)::TypeDependentOperandCollector::addTo(llvm::SmallVectorImpl<swift::SILValue>&, swift::SILFunction&) + 360
9  swift-frontend           0x0000000104f96088 void collectTypeDependentOperands<swift::CanType, swift::SubstitutionMap&>(llvm::SmallVectorImpl<swift::SILValue>&, swift::SILFunction&, swift::CanType&&, swift::SubstitutionMap&) + 228
10 swift-frontend           0x0000000104f95eb0 swift::ApplyInst::create(swift::SILDebugLocation, swift::SILValue, swift::SubstitutionMap, llvm::ArrayRef<swift::SILValue>, swift::OptionSet<swift::ApplyFlags, unsigned char>, llvm::Optional<swift::SILModuleConventions>, swift::SILFunction&, swift::GenericSpecializationInformation const*) + 372
11 swift-frontend           0x0000000104834760 emitRawApply(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SubstitutionMap, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::OptionSet<swift::ApplyFlags, unsigned char>, llvm::ArrayRef<swift::SILValue>, llvm::SmallVectorImpl<swift::SILValue>&, swift::Lowering::ExecutorBreadcrumb) + 2104
12 swift-frontend           0x0000000104834fe0 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>) + 1064
13 swift-frontend           0x000000010488512c swift::Lowering::SILGenFunction::emitApplyOfDefaultArgGenerator(swift::SILLocation, swift::ConcreteDeclRef, unsigned int, swift::CanType, swift::Lowering::SGFContext) + 800
14 swift-frontend           0x0000000104836b60 emitDelayedArguments(swift::Lowering::SILGenFunction&, llvm::MutableArrayRef<(anonymous namespace)::DelayedArgument>, llvm::MutableArrayRef<llvm::SmallVector<swift::Lowering::ManagedValue, 4u>>) + 736
15 swift-frontend           0x000000010484c7c8 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, swift::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 1184
16 swift-frontend           0x0000000104839d80 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2380
17 swift-frontend           0x000000010483821c swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 1860
18 swift-frontend           0x000000010489adf8 void llvm::function_ref<void (swift::Expr*)>::callback_fn<swift::Lowering::RValue swift::Lowering::SILGenFunction::emitOpenExistentialExpr<swift::Lowering::RValue, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_18>(swift::OpenExistentialExpr*, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_18)::'lambda'(swift::Expr*)>(long, swift::Expr*) + 36
19 swift-frontend           0x000000010488c37c swift::Lowering::SILGenFunction::emitOpenExistentialExprImpl(swift::OpenExistentialExpr*, llvm::function_ref<void (swift::Expr*)>) + 176
20 swift-frontend           0x000000010489ad84 swift::Lowering::RValue swift::Lowering::SILGenFunction::emitOpenExistentialExpr<swift::Lowering::RValue, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_18>(swift::OpenExistentialExpr*, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_18) + 60
21 swift-frontend           0x000000010488f648 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 8804
22 swift-frontend           0x0000000104885db8 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 944
23 swift-frontend           0x00000001048e9bc0 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 3896
24 swift-frontend           0x00000001048e8c7c swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 24
25 swift-frontend           0x00000001048a2d90 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 356
26 swift-frontend           0x0000000104828a08 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 8180
27 swift-frontend           0x000000010482a1d0 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef) + 288
28 swift-frontend           0x00000001048269fc swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 164
29 swift-frontend           0x00000001048f6c18 (anonymous namespace)::SILGenType::visitFuncDecl(swift::FuncDecl*) + 28
30 swift-frontend           0x00000001048f3b54 (anonymous namespace)::SILGenType::emitType() + 232
31 swift-frontend           0x00000001048f3a60 swift::Lowering::SILGenModule::visitNominalTypeDecl(swift::NominalTypeDecl*) + 24
32 swift-frontend           0x000000010482c174 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1028
33 swift-frontend           0x00000001048e875c 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&) + 148
34 swift-frontend           0x000000010483017c llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 400
35 swift-frontend           0x000000010482d260 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 100
36 swift-frontend           0x000000010432bdb8 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 532
37 swift-frontend           0x000000010433ad00 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
38 swift-frontend           0x000000010432dadc swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3216
39 swift-frontend           0x0000000104187c30 swift::mainEntry(int, char const**) + 3304
40 dyld                     0x000000019e277e50 start + 2544
error: fatalError
eeckstein commented 1 year ago

rdar://99763675

AnthonyLatsis commented 1 year ago

Self-contained presumably minimal reproducer:

protocol P {}

func foo<T: P>(a: Void..., b: Void = (), c: inout T) {}

func crash(p: any P) {
  var p = p
  foo(c: &p)
}

Observations:

Stack dump:

Assertion failed: (def->getFunction() == &F && "def of root local archetype is in wrong function"), function addTo, file SILInstructions.cpp, line 118.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Users/mac/Desktop/swift-project/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swift-frontend -frontend -emit-silgen -primary-file /Users/mac/Desktop/test.swift -target x86_64-apple-darwin21.6.0 -enable-objc-interop -color-diagnostics -plugin-path /Users/mac/Desktop/swift-project/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/lib/swift/host/plugins -plugin-path /Users/mac/Desktop/swift-project/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/local/lib/swift/host/plugins -module-name test -o -
1.  Swift version 5.9-dev (LLVM 91080e65df791d6, Swift c1d5118c21da49a)
2.  Compiling with the current language version
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/mac/Desktop/test.swift")
4.  While silgen emitFunction SIL function "@$s4testAA1pyAA1P_p_tF".
 for 'test(p:)' (at /Users/mac/Desktop/test.swift:409:1)
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           0x0000000112fab847 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x0000000112faab25 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x0000000112fabe90 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007ff816c4bdfd _sigtramp + 29
4  libsystem_platform.dylib 0x0000000100007fcc _sigtramp + 18446603374493942252
5  libsystem_c.dylib        0x00007ff816b81d24 abort + 123
6  libsystem_c.dylib        0x00007ff816b810cb err + 0
7  swift-frontend           0x0000000113510c41 (anonymous namespace)::TypeDependentOperandCollector::addTo(llvm::SmallVectorImpl<swift::SILValue>&, swift::SILFunction&) (.cold.5) + 33
8  swift-frontend           0x000000010e35f1a3 (anonymous namespace)::TypeDependentOperandCollector::addTo(llvm::SmallVectorImpl<swift::SILValue>&, swift::SILFunction&) + 371
9  swift-frontend           0x000000010e352789 void collectTypeDependentOperands<swift::CanType, swift::SubstitutionMap&>(llvm::SmallVectorImpl<swift::SILValue>&, swift::SILFunction&, swift::CanType&&, swift::SubstitutionMap&) + 217
10 swift-frontend           0x000000010e352596 swift::ApplyInst::create(swift::SILDebugLocation, swift::SILValue, swift::SubstitutionMap, llvm::ArrayRef<swift::SILValue>, swift::OptionSet<swift::ApplyFlags, unsigned char>, llvm::Optional<swift::SILModuleConventions>, swift::SILFunction&, swift::GenericSpecializationInformation const*) + 390
11 swift-frontend           0x000000010db234a5 emitRawApply(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SubstitutionMap, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::OptionSet<swift::ApplyFlags, unsigned char>, llvm::ArrayRef<swift::SILValue>, llvm::SmallVectorImpl<swift::SILValue>&, swift::Lowering::ExecutorBreadcrumb) + 2325
12 swift-frontend           0x000000010db23fa4 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>) + 1332
13 swift-frontend           0x000000010db84308 swift::Lowering::SILGenFunction::emitApplyOfDefaultArgGenerator(swift::SILLocation, swift::ConcreteDeclRef, unsigned int, swift::CanType, swift::Lowering::SGFContext) + 1144
14 swift-frontend           0x000000010db26103 emitDelayedArguments(swift::Lowering::SILGenFunction&, llvm::MutableArrayRef<(anonymous namespace)::DelayedArgument>, llvm::MutableArrayRef<llvm::SmallVector<swift::Lowering::ManagedValue, 4u>>) + 851
15 swift-frontend           0x000000010db415b6 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, swift::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 1622
16 swift-frontend           0x000000010db2a09c (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2972
17 swift-frontend           0x000000010db27b11 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 1905
18 swift-frontend           0x000000010db8e523 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 179
19 swift-frontend           0x000000010db9ec76 void llvm::function_ref<void (swift::Expr*)>::callback_fn<swift::Lowering::RValue swift::Lowering::SILGenFunction::emitOpenExistentialExpr<swift::Lowering::RValue, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_18>(swift::OpenExistentialExpr*, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_18)::'lambda'(swift::Expr*)>(long, swift::Expr*) + 38
20 swift-frontend           0x000000010db8d110 swift::Lowering::SILGenFunction::emitOpenExistentialExprImpl(swift::OpenExistentialExpr*, llvm::function_ref<void (swift::Expr*)>) + 176
21 swift-frontend           0x000000010db9ebf5 swift::Lowering::RValue swift::Lowering::SILGenFunction::emitOpenExistentialExpr<swift::Lowering::RValue, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_18>(swift::OpenExistentialExpr*, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_18) + 69
22 swift-frontend           0x000000010db91269 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 11769
23 swift-frontend           0x000000010db85156 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 1142
24 swift-frontend           0x000000010dc03fda swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 5322
25 swift-frontend           0x000000010dc02b05 swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 21
26 swift-frontend           0x000000010dba8c77 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 439
27 swift-frontend           0x000000010db15133 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 10739
28 swift-frontend           0x000000010db169a7 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef) + 231
29 swift-frontend           0x000000010db12728 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 168
30 swift-frontend           0x000000010db193fb swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 2283
31 swift-frontend           0x000000010dc025d8 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&) + 120
32 swift-frontend           0x000000010db1e423 llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 387
33 swift-frontend           0x000000010db1a7fa swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 106
34 swift-frontend           0x000000010d4f5974 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 676
35 swift-frontend           0x000000010d4f8d00 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 784
36 swift-frontend           0x000000010d4f7b20 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3232
37 swift-frontend           0x000000010d4bd0b3 swift::mainEntry(int, char const**) + 1203
38 dyld                     0x00000001300b452e start + 462
Abort trap: 6