swiftlang / swift

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

Compiler crash while working with boxed closure #60960

Open antonvmironov opened 2 years ago

antonvmironov commented 2 years ago

Describe the bug following test program crashes the compiler:

final class Box<Value> {
  var value: Value

  init(_ value: Value) {
    self.value = value
  }

  func perform<T>(_ block: (inout Value) -> T) -> T {
    block(&value)
  }
}

let boxOfBlock = Box<() -> Void> { print("hi") }

let oldBlock: () -> Void = boxOfBlock.perform {
  let oldBlock = $0
  $0 = { print("hi again") }
  return oldBlock
}

Crash:

% swiftc compiler_crash.swift                                         
error: compile command failed due to signal 6 (use -v to see invocation)
<unknown>:0: error: fatal error encountered during compilation; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
<unknown>:0: note: SmallVector unable to grow. Requested capacity (4294967296) is larger than maximum value for size type (4294967295)
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and 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 compiler_crash.swift -target arm64-apple-macosx12.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -color-diagnostics -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 compiler_crash -disable-clang-spi -target-sdk-version 13.0 -o /var/folders/rl/4d9fv1kx3ms49_mz__s92mbc0000gn/T/TemporaryDirectory.YIqPUW/compiler_crash-1.o
1.  Apple Swift version 5.7 (swiftlang-5.7.0.123.8 clang-1400.0.29.50)
2.  Compiling with the current language version
3.  While evaluating request IRGenRequest(IR Generation for file "compiler_crash.swift")
4.  While emitting IR SIL function "@$sytIegr_Ieg_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           0x0000000106b1ff80 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106b1ef84 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000106b20604 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x00000001bd77f4a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001bd767ee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001bd6a2340 abort + 168
6  swift-frontend           0x000000010217b000 PrettyStackTraceFrontend::~PrettyStackTraceFrontend() + 0
7  swift-frontend           0x0000000106a7ee98 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x0000000106acb190 report_at_maximum_capacity(unsigned long) + 0
9  swift-frontend           0x0000000106acafe4 llvm::SmallVectorBase<unsigned int>::grow_pod(void*, unsigned long, unsigned long) + 228
10 swift-frontend           0x00000001023f5f54 (anonymous namespace)::SyncCallEmission::setArgs(swift::irgen::Explosion&, bool, swift::irgen::WitnessMetadata*) + 412
11 swift-frontend           0x0000000102596a00 (anonymous namespace)::IRGenSILFunction::visitFullApplySite(swift::FullApplySite) + 4032
12 swift-frontend           0x0000000102576038 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 7772
13 swift-frontend           0x0000000102443d0c swift::irgen::IRGenerator::emitLazyDefinitions() + 1156
14 swift-frontend           0x00000001025440cc swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 3024
15 swift-frontend           0x0000000102573f10 swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 188
16 swift-frontend           0x00000001025503ec llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 744
17 swift-frontend           0x00000001025472e0 swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 236
18 swift-frontend           0x0000000102171ea8 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 2160
19 swift-frontend           0x0000000102173c34 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5932
20 swift-frontend           0x0000000102114720 swift::mainEntry(int, char const**) + 3940
21 dyld                     0x00000001092b908c start + 520
% 

Steps To Reproduce Steps to reproduce the behavior:

  1. paste the code above into a file compiler_crash.swift
  2. run swiftc compiler_crash.swift ...

Expected behavior The compiler should not crash.

Environment (please fill out the following information)

Additional context Add any other context about the problem here.

theblixguy commented 2 years ago

On main you get:

Assertion failed: (data.InnerResults.empty()), function plan, file SILGenPoly.cpp, line 1951.

samsonjs commented 2 years ago

I have a program that crashes in a similar way and I'm not sure if it's the same bug.

struct SackOfCallbacks {
    var callbacks = [() async throws -> Void]()
}

var sack = SackOfCallbacks()
sack.callbacks.append {}

Crash log:

% swiftc -v crash.swift
Apple Swift version 5.7.1 (swiftlang-5.7.1.134.3 clang-1400.0.29.51)
Target: arm64-apple-macosx13.0
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file crash.swift -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -color-diagnostics -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 crash -disable-clang-spi -target-sdk-version 13.0 -o /var/folders/v6/2_xws2vj4lz6zjtj7xtjh3_80000gn/T/TemporaryDirectory.iwGmul/crash-1.o
error: compile command failed due to signal 6 (use -v to see invocation)
<unknown>:0: error: fatal error encountered during compilation; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
<unknown>:0: note: SmallVector unable to grow. Requested capacity (4294967297) is larger than maximum value for size type (4294967295)
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and 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 crash.swift -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -color-diagnostics -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 crash -disable-clang-spi -target-sdk-version 13.0 -o /var/folders/v6/2_xws2vj4lz6zjtj7xtjh3_80000gn/T/TemporaryDirectory.iwGmul/crash-1.o
1.  Apple Swift version 5.7.1 (swiftlang-5.7.1.134.3 clang-1400.0.29.51)
2.  Compiling with the current language version
3.  While evaluating request IRGenRequest(IR Generation for file "crash.swift")
4.  While emitting IR SIL function "@$sytIegHr_yts5Error_pIegHrzo_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           0x0000000104fe3630 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000104fe2634 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000104fe3cb4 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x0000000197cf02a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000197cc1cec pthread_kill + 288
5  libsystem_c.dylib        0x0000000197bfb2c8 abort + 180
6  swift-frontend           0x0000000100638bc8 PrettyStackTraceFrontend::~PrettyStackTraceFrontend() + 0
7  swift-frontend           0x0000000104f42548 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x0000000104f8e840 report_at_maximum_capacity(unsigned long) + 0
9  swift-frontend           0x0000000104f8e694 llvm::SmallVectorBase<unsigned int>::grow_pod(void*, unsigned long, unsigned long) + 228
10 swift-frontend           0x00000001008b2704 (anonymous namespace)::AsyncCallEmission::setArgs(swift::irgen::Explosion&, bool, swift::irgen::WitnessMetadata*) + 468
11 swift-frontend           0x0000000100a54938 (anonymous namespace)::IRGenSILFunction::visitFullApplySite(swift::FullApplySite) + 4032
12 swift-frontend           0x0000000100a33f70 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 7860
13 swift-frontend           0x00000001009019a8 swift::irgen::IRGenerator::emitLazyDefinitions() + 1156
14 swift-frontend           0x0000000100a01fac swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 3024
15 swift-frontend           0x0000000100a31df0 swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 188
16 swift-frontend           0x0000000100a0e2cc llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 744
17 swift-frontend           0x0000000100a051c0 swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 236
18 swift-frontend           0x000000010062fa6c performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 2160
19 swift-frontend           0x00000001006317f8 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5932
20 swift-frontend           0x00000001005d22ec swift::mainEntry(int, char const**) + 3940
21 dyld                     0x0000000197997e50 start + 2544