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

Compiler crash with strict concurrency and referring to another @MainActor class #72285

Open
lyamamot opened this issue Mar 13, 2024 · 0 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

@lyamamot
Copy link

lyamamot commented Mar 13, 2024

Description

The code snippet has a class that constructs another class, which constructs another class in its initializer. These are all classes annotated with @MainActor.

Changing things about the last object in the chain can prevent the crash.

Reproduction

This crashes with swiftc -strict-concurrency=complete

// Removing @MainActor here prevents the crash.
@MainActor
class AnotherModel {
    // Commenting this next line also prevents the crash.
    init() {}
}

@MainActor
class AppViewModel {
    init(anotherModel: AnotherModel = AnotherModel()) {}
}

@MainActor
struct AppView {
    private var model = AppViewModel()
}

Stack dump

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 IRGenRequest(IR Generation for file "Crashy.swift")
4.	While emitting IR SIL function "@$s6Crashy7AppViewV5model33_DCA0530D12908CEF73E9453845BB263DLLAA0bC5ModelCvpfi".
 for expression at [Crashy.swift:15:25 - line:15:38] RangeText="AppViewModel("
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           0x0000000105d07f3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000105d070f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000105d08544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x000000018844da24 _sigtramp + 56
4  swift-frontend           0x000000010069879c llvm::IRBuilderBase::CreateAlignedLoad(llvm::Type*, llvm::Value*, llvm::MaybeAlign, bool, llvm::Twine const&) + 88
5  swift-frontend           0x000000010069879c llvm::IRBuilderBase::CreateAlignedLoad(llvm::Type*, llvm::Value*, llvm::MaybeAlign, bool, llvm::Twine const&) + 88
6  swift-frontend           0x00000001009f1704 swift::irgen::IRGenFunction::emitSuspensionPoint(swift::irgen::Explosion&, llvm::Value*) + 984
7  swift-frontend           0x0000000100b3a9a8 (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 68520
8  swift-frontend           0x0000000100b284fc swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 11556
9  swift-frontend           0x000000010097c430 swift::irgen::IRGenerator::emitGlobalTopLevel(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&) + 2472
10 swift-frontend           0x0000000100adb940 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 4016
11 swift-frontend           0x0000000100b2554c swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 176
12 swift-frontend           0x0000000100ae8264 llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 836
13 swift-frontend           0x0000000100ade438 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**) + 264
14 swift-frontend           0x000000010060c0b8 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
15 swift-frontend           0x0000000100606294 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*) + 1892
16 swift-frontend           0x0000000100605444 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 608
17 swift-frontend           0x0000000100609694 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1448
18 swift-frontend           0x00000001006076d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
19 swift-frontend           0x0000000100596e8c swift::mainEntry(int, char const**) + 2612
20 dyld                     0x000000018809d0e0 start + 2360
error: fatalError

Expected behavior

It should not crash. The code looks reasonable to me.

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

Without -strict-concurrency=complete, the compiler reports an error:

crash.swift:10:39: error: call to main actor-isolated initializer 'init()' in a synchronous nonisolated context
    init(anotherModel: AnotherModel = AnotherModel()) {}
                                      ^
crash.swift:5:5: note: calls to initializer 'init()' from outside of its actor context are implicitly asynchronous
    init() {}
    ^
error: fatalError
@lyamamot lyamamot 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 Mar 13, 2024
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

1 participant