Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on IRGen with isolated parameter from protocol requirement #76087

Open
khlopko opened this issue Aug 26, 2024 · 3 comments
Open

Crash on IRGen with isolated parameter from protocol requirement #76087

khlopko opened this issue Aug 26, 2024 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@khlopko
Copy link

khlopko commented Aug 26, 2024

Description

Unfortunately, I cannot share the whole code that reproduces the issue, because so far I was able to reproduce it within the main project, and pulling out components that involved into the issue doesn't reproduce the crash. I had to strip down everything from the crash as well that relates to the project code.

Reproduction

This is the main parts that are involved into a crash, they don't give a crash as standalone, but replicate most of the original code. Implementation of delegate functions are almost identical here. Compiler crashes for didComplete function in implementation.

protocol OperationDelegate<Value>: AnyObject {
    associatedtype Value

    func didFail(for value: Value, with error: any Error)
    func didComplete(with value: Value, in isolation: isolated any Actor)
}

final class SomeClass: OperationDelegate {
    struct SomeValue {
    }

    typealias Value = SomeValue

    private var completedItems: [Value] = []
    private var errors: [any Error] = []

    func didFail(for value: Value, with error: any Error) {
        errors.append(error)
        print(error)
    }

    func didComplete(with value: Value, in isolation: isolated any Actor) {
        completedItems.append(value)
    }
}

Stack dump

1.	Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
2.	Compiling with the current language version
3.	While evaluating request IRGenRequest(IR Generation for file)
4.	While emitting IR SIL function "...OperationDelegateA2aDP11didComplete3for2in_y6EntityQz_So11NSOperationCScA_pYitYaKFTW".
 for 'didComplete(for:in:_:)'
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           0x00000001063070fc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106305350 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001063076c8 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x000000019c8dd184 _sigtramp + 56
4  swift-frontend           0x00000001012fb2d8 (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 37740
5  swift-frontend           0x00000001012f0c50 (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 13220
6  swift-frontend           0x00000001012ed408 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 2088
7  swift-frontend           0x000000010113b0a4 swift::irgen::IRGenerator::emitLazyDefinitions() + 4872
8  swift-frontend           0x00000001012a4d4c swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 4964
9  swift-frontend           0x00000001012ec954 swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 176
10 swift-frontend           0x00000001012adb3c swift::IRGenRequest::OutputType swift::Evaluator::getResultUncached<swift::IRGenRequest, swift::IRGenRequest::OutputType swift::evaluateOrFatal<swift::IRGenRequest>(swift::Evaluator&, swift::IRGenRequest)::'lambda'()>(swift::IRGenRequest const&, swift::IRGenRequest::OutputType swift::evaluateOrFatal<swift::IRGenRequest>(swift::Evaluator&, swift::IRGenRequest)::'lambda'()) + 784
11 swift-frontend           0x00000001012a7574 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**) + 180
12 swift-frontend           0x0000000100cb685c generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, llvm::GlobalVariable*&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>) + 156
13 swift-frontend           0x0000000100cb2088 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*) + 2176
14 swift-frontend           0x0000000100cb0f74 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 984
15 swift-frontend           0x0000000100cb4228 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1680
16 swift-frontend           0x0000000100cb2f58 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
17 swift-frontend           0x0000000100c3a01c swift::mainEntry(int, char const**) + 3680
18 dyld                     0x000000019c523274 start + 2840

Expected behavior

No crash.

Environment

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0

Additional information

No response

@khlopko khlopko added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Aug 26, 2024
@scottasoutherland
Copy link

I'm not sure if it's exactly the same issue, but i have a reproduce-able case involving protocols restrained to actors. Specifically if we create a new xcode project, and use the Xcode 16 RC1.

public protocol EventUploadCadence: Actor {
    init(minimumInterval: Double, maximumInterval: Double) async
    func restart(asyncCallback: @escaping @Sendable () async -> Void)
    func stop()
    func enqueueUnscheduledEvent()
}

then we create mock class inside a XCTest target such as:

actor EventUploadCadenceMock: EventUploadCadence {
    private var asyncCallback: ( @Sendable () async -> Void)?

    private var onStartClosure: ( @Sendable () -> Void)?
    private var onStopClosure: ( @Sendable () -> Void)?
    private var onEnqueueUnscheduledEventClosure: ( @Sendable () -> Void)?

    func onEnqueueUnscheduledEvent(_ closure: @escaping @Sendable () -> Void) {
        onEnqueueUnscheduledEventClosure = closure
    }

    init(
        minimumInterval: Double,
        maximumInterval: Double
    ) {  }
    func stop() { onStopClosure?() }
    func enqueueUnscheduledEvent() { onEnqueueUnscheduledEventClosure?() }

    func restart(asyncCallback: @escaping @Sendable () async -> Void) {
        self.asyncCallback = asyncCallback
        onStartClosure?()
    }

    func triggerAsyncCallback() async {
        await asyncCallback?()
    }
}

Then the compiler will crash with:

While emitting IR SIL function "@$s22TestCompilerCrashTests22EventUploadCadenceMockC0abC00efG0AadEP15minimumInterval07maximumJ0xSd_SdtYacfCTW".
 for 'init(minimumInterval:maximumInterval:)'

Here is a sample xcode project:

TestCompilerCrash.zip

@scottasoutherland
Copy link

I'll note that making EventUploadCadence constrained to AnyObject and changing EventUploadCadenceMock to be a class does make the crash no longer happen.

@scottasoutherland
Copy link

scottasoutherland commented Sep 17, 2024

After looking at this some more, i discovered my issue was that the protocol init was marked as async but the implementation was not marked as async. Not sure why this compiled in Xcode 15. But an error would be nice rather than a crash. I don't think it's this same issue anymore, so i'll file a separate bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

4 participants
@scottasoutherland @khlopko and others