swiftlang / swift

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

Crash when wrapping type containing Mutex from another file: "Global is external, but doesn't have external or weak linkage!" #75903

Open Jnosh opened 4 weeks ago

Jnosh commented 4 weeks ago

Description

The following example crashes the compiler. I can only reproduce the crash if the two type definitions are located in separate files.

Reproduction

// ProtectedState.swift
import Synchronization

struct ProtectedState: ~Copyable {
    private struct State {}
    private let protectedState = Mutex(State())
}
// Wrapper.swift
struct Wrapper: ~Copyable {
    private let state = ProtectedState()
}

Stack dump

Global is external, but doesn't have external or weak linkage!
ptr @"$s4main14ProtectedStateV0C033_435AFF9E5BE074265989542110161723LLVMn"
<unknown>:0: error: fatal error encountered during compilation; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
<unknown>:0: note: Broken module found, compilation aborted!
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c ProtectedState.swift -primary-file Wrapper.swift -target arm64-apple-macosx15.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -color-diagnostics -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name main -disable-clang-spi -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/mk/5k70fbgj28g370g9tzg97n_80000gn/T/TemporaryDirectory.WfNAgh/Wrapper-1.o
1.  Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1)
2.  Compiling with effective version 5.10
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           0x000000010a0ed194 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010a0eb3e8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010a0ed760 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x0000000189138184 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000189102f70 pthread_kill + 288
5  libsystem_c.dylib        0x000000018900f908 abort + 128
6  swift-frontend           0x0000000104e57ed4 createDispatchingDiagnosticConsumerIfNeeded(swift::FrontendInputsAndOutputs const&, llvm::function_ref<std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_delete<swift::DiagnosticConsumer>> (swift::InputFile const&)>) + 0
7  swift-frontend           0x000000010a05cf18 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x000000010a05ce00 llvm::report_fatal_error(llvm::Twine const&, bool) + 0
9  swift-frontend           0x00000001050977f0 llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::printPipeline(llvm::raw_ostream&, llvm::function_ref<llvm::StringRef (llvm::StringRef)>) + 0
10 swift-frontend           0x0000000109e24b70 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) + 224
11 swift-frontend           0x000000010508c22c swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*, llvm::raw_pwrite_stream*) + 3764
12 swift-frontend           0x000000010508de68 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, llvm::vfs::OutputBackend&, swift::UnifiedStatsReporter*) + 2352
13 swift-frontend           0x0000000104aa517c generateCode(swift::CompilerInstance&, llvm::StringRef, llvm::Module*, llvm::GlobalVariable*) + 368
14 swift-frontend           0x0000000104aa0614 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*) + 2316
15 swift-frontend           0x0000000104a9f474 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 984
16 swift-frontend           0x0000000104aa2728 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1680
17 swift-frontend           0x0000000104aa1458 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
18 swift-frontend           0x0000000104a2850c swift::mainEntry(int, char const**) + 3680
19 dyld                     0x0000000188d80274 start + 2840

Expected behavior

The example should compiler successfully.

Environment

Xcode 16.0 beta 5, macOS 15.0 Beta 6 (24A5320a)

swift-driver version: 1.113 Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1) Target: arm64-apple-macosx15.0

Additional information

Sample code

Compile with swiftc ProtectedState.swift Wrapper.swift to reproduce.

Azoy commented 3 weeks ago

@aschwaighofer does your PR solve this issue as well? https://github.com/swiftlang/swift/pull/75967

Azoy commented 3 weeks ago

In the meantime, can you try not making the state and property private and promote them to internal to see if this resolves the issue?

Jnosh commented 3 weeks ago

Yes, making State (just the type, not even the property) internal instead of private works!

aschwaighofer commented 2 weeks ago

Yes, this is a dupe.