swiftlang / swift

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

Escaping closure taking a `Self` value of a class type crashes the compiler #60884

Closed fwcd closed 2 years ago

fwcd commented 2 years ago

Describe the bug The following program crashes the compiler:

class X {
    static func doStuff(continuation: @escaping (Self) -> Void) {}

    static func doStuff() async throws -> Self {
        try await withCheckedThrowingContinuation { continuation in
            doStuff { value in
                continuation.resume(returning: value)
            }
        }
    }
}

Compiling throws the following stack trace (click below to expand):

``` error: compile command failed due to signal 6 (use -v to see invocation) Assertion failed: (SelfValue && "no local self metadata"), function getDynamicSelfMetadata, file GenHeap.cpp, line 1778. Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace. Stack dump: 0. Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-08-29-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file self-class-crash.swift -target arm64-apple-macosx12.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -color-diagnostics -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-08-29-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-08-29-a.xctoolchain/usr/lib/swift -module-name main -o /var/folders/k2/hxsy4q4x32x_m8_3qj04sdv80000gn/T/TemporaryDirectory.6IAxjn/self-class-crash-1.o 1. Apple Swift version 5.8-dev (LLVM 03bd949eeee4761, Swift 2c40a850df6f409) 2. Compiling with the current language version 3. While evaluating request IRGenRequest(IR Generation for file "self-class-crash.swift") 4. While emitting IR SIL function "@$s4main1XC7doStuffACXDyYaKFZyScCyACXDs5Error_pGXEfU_". for expression at [self-class-crash.swift:5:51 - line:9:9] RangeText="{ continuation in doStuff { value in continuation.resume(returning: value) } " 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 0x00000001093ef78c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56 1 swift-frontend 0x00000001093eea10 llvm::sys::RunSignalHandlers() + 128 2 swift-frontend 0x00000001093efdcc SignalHandler(int) + 304 3 libsystem_platform.dylib 0x000000018156f4a4 _sigtramp + 56 4 libsystem_pthread.dylib 0x0000000181557ee0 pthread_kill + 288 5 libsystem_c.dylib 0x0000000181492340 abort + 168 6 libsystem_c.dylib 0x0000000181491754 err + 0 7 swift-frontend 0x00000001094aa790 swift::irgen::IRGenFunction::getDynamicSelfMetadata() (.cold.2) + 0 8 swift-frontend 0x0000000104ee5bd0 swift::irgen::IRGenFunction::getDynamicSelfMetadata() + 392 9 swift-frontend 0x0000000104fec2a0 swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRef, swift::irgen::MetadataResponse, swift::irgen::DynamicMetadataRequest>::visit(swift::CanType, swift::irgen::DynamicMetadataRequest) + 1788 10 swift-frontend 0x0000000104fe9760 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType, swift::irgen::DynamicMetadataRequest) + 536 11 swift-frontend 0x0000000104feead8 swift::irgen::GenericArguments::collect(swift::irgen::IRGenFunction&, swift::SubstitutionMap)::'lambda'(unsigned int, swift::CanType, swift::ProtocolConformanceRef)::operator()(unsigned int, swift::CanType, swift::ProtocolConformanceRef) const + 124 12 swift-frontend 0x0000000104f260b0 swift::irgen::GenericTypeRequirements::enumerateFulfillments(swift::irgen::IRGenModule&, swift::SubstitutionMap, llvm::function_ref) + 244 13 swift-frontend 0x0000000104fee9f0 swift::irgen::GenericArguments::collect(swift::irgen::IRGenFunction&, swift::SubstitutionMap) + 112 14 swift-frontend 0x0000000104fee444 emitNominalMetadataRef(swift::irgen::IRGenFunction&, swift::NominalTypeDecl*, swift::CanType, swift::irgen::DynamicMetadataRequest) + 320 15 swift-frontend 0x0000000104feebf8 (anonymous namespace)::EmitTypeMetadataRef::visitBoundGenericType(swift::CanTypeWrapper, swift::irgen::DynamicMetadataRequest) + 148 16 swift-frontend 0x0000000104fec18c swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRef, swift::irgen::MetadataResponse, swift::irgen::DynamicMetadataRequest>::visit(swift::CanType, swift::irgen::DynamicMetadataRequest) + 1512 17 swift-frontend 0x0000000104fe9760 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType, swift::irgen::DynamicMetadataRequest) + 536 18 swift-frontend 0x0000000104fe9dd4 swift::irgen::IRGenFunction::emitTypeMetadataRefForLayout(swift::SILType, swift::irgen::DynamicMetadataRequest) + 284 19 swift-frontend 0x0000000104f01c8c swift::irgen::IRGenFunction::emitValueWitnessTableRef(swift::SILType, swift::irgen::DynamicMetadataRequest, llvm::Value**) + 196 20 swift-frontend 0x0000000104f01bbc swift::irgen::IRGenFunction::emitValueWitnessTableRef(swift::SILType, llvm::Value**) + 32 21 swift-frontend 0x0000000104f18680 swift::irgen::emitLoadOfAlignmentMask(swift::irgen::IRGenFunction&, swift::SILType) + 60 22 swift-frontend 0x0000000104ee06c8 swift::irgen::HeapNonFixedOffsets::HeapNonFixedOffsets(swift::irgen::IRGenFunction&, swift::irgen::HeapLayout const&) + 828 23 swift-frontend 0x0000000104ee0c78 swift::irgen::HeapLayout::getPrivateMetadata(swift::irgen::IRGenModule&, llvm::Constant*) const + 464 24 swift-frontend 0x0000000104ee11cc swift::irgen::IRGenFunction::emitUnmanagedAlloc(swift::irgen::HeapLayout const&, llvm::Twine const&, llvm::Constant*, swift::irgen::HeapNonFixedOffsets const*) + 68 25 swift-frontend 0x0000000104ed4ef4 swift::irgen::emitFunctionPartialApplication(swift::irgen::IRGenFunction&, swift::SILFunction&, swift::irgen::FunctionPointer const&, llvm::Value*, swift::irgen::Explosion&, llvm::ArrayRef, swift::SubstitutionMap, swift::CanTypeWrapper, swift::CanTypeWrapper, swift::CanTypeWrapper, swift::irgen::Explosion&, bool) + 2676 26 swift-frontend 0x0000000104fbce4c swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 64324 27 swift-frontend 0x0000000104fa8b44 (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 7260 28 swift-frontend 0x0000000104fa6954 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 896 29 swift-frontend 0x0000000104e8773c swift::irgen::IRGenerator::emitLazyDefinitions() + 1400 30 swift-frontend 0x0000000104f69868 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2020 31 swift-frontend 0x0000000104fa60c8 swift::GeneratedModule swift::SimpleRequest::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence) const + 196 32 swift-frontend 0x0000000104f79430 llvm::Expected swift::Evaluator::getResultUncached(swift::IRGenRequest const&) + 372 33 swift-frontend 0x0000000104f6ba2c swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 212 34 swift-frontend 0x0000000104bcbd2c performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr >, llvm::PointerUnion, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1664 35 swift-frontend 0x0000000104bcb108 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 576 36 swift-frontend 0x0000000104bd9cf4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref, bool) + 160 37 swift-frontend 0x0000000104bcce2c swift::performFrontend(llvm::ArrayRef, char const*, void*, swift::FrontendObserver*) + 3256 38 swift-frontend 0x0000000104a3b9f0 swift::mainEntry(int, char const**) + 3136 39 dyld 0x00000001120bd08c start + 520 ```

Steps To Reproduce Steps to reproduce the behavior:

  1. Compile the program with Swift 5.6 or the most recent snapshot toolchain

Expected behavior Not crashing.

Environment (please fill out the following information)

Additional context Note that the crash neither seems to occur if the closure is not @escaping nor if we use X instead of Self.

theblixguy commented 2 years ago

Duplicate of https://github.com/apple/swift/issues/56757

fwcd commented 2 years ago

Good catch, so I think we can close this?