swiftlang / swift

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

"Global is external, but doesn't have external or weak linkage!" crash from 6.0 variadic type #75405

Open vanvoorden opened 1 month ago

vanvoorden commented 1 month ago

Reproduction

I am seeing this crash while using the workaround from https://github.com/swiftlang/swift/issues/74609. Here are my current steps to repro:

  1. Create a Swift Package.
  2. Declare a Repeater variadic type in one file:
    
    fileprivate struct FnBox<T> {
    let fn: () -> T
    }

struct Repeater { private let _elements: (repeat FnBox)

var elements: (repeat () -> each Element) { return (repeat (each _elements).fn) }

init(elements element: repeat @escaping () -> each Element) { self._elements = (repeat FnBox(fn: each element)) } }


3. Declare a `Repeater` extension in a different file:
```swift
extension Repeater {
  func printElements() {
    print((repeat (each self.elements)()))
  }
}

This leads to a compiler crash. I see the compiler crash when FnBox is fileprivate or private.

There seem to be (at least) three workarounds:

  1. Declare the elements tuple directly as a parameter pack of closures without the FnBox helper:
struct Repeater<each Element> {
  var elements: (repeat () -> each Element)
}

This is suboptimal. The reason I want the workaround from https://github.com/swiftlang/swift/issues/74609 is because of a compiler crash from 5.10.

  1. Declare the Repeater extension in the same file. This might not be an available option for my use-case.

  2. Declare the FnBox helper to be internal:

internal struct FnBox<T> {
  let fn: () -> T
}

Stack dump

Global is external, but doesn't have external or weak linkage!
ptr @"$s9MyLibrary5FnBox33_4A6444129A9675BDC10B0CBB9EC84AB1LLVMa"
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode_16_beta_3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift -primary-file /Users/rick/Desktop/MyLibrary/Sources/MyLibrary/Repeater.swift -emit-dependencies-path /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/Objects-normal/arm64/Repeater.d -emit-const-values-path /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/Objects-normal/arm64/Repeater.swiftconstvalues -emit-reference-dependencies-path /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/Objects-normal/arm64/Repeater.swiftdeps -serialize-diagnostics-path /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/Objects-normal/arm64/Repeater.dia -target arm64-apple-macos15.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode_16_beta_3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -I /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Products/Debug -I /Applications/Xcode_16_beta_3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Products/Debug -F /Applications/Xcode_16_beta_3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=5 -module-cache-path /Users/rick/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -profile-generate -profile-coverage-mapping -swift-version 6 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -enable-experimental-opaque-type-erasure -const-gather-protocols-file /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/Objects-normal/arm64/MyLibrary_const_extract_protocols.json -enable-experimental-feature DebugDescriptionMacro -empty-abi-descriptor -plugin-path /Applications/Xcode_16_beta_3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins/testing -validate-clang-modules-once -clang-build-session-file /Users/rick/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/rick/Desktop/MyLibrary/.swiftpm/xcode -resource-dir /Applications/Xcode_16_beta_3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/rick/Desktop/MyLibrary/.swiftpm/xcode -Xcc -ivfsstatcache -Xcc /Users/rick/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.0-24A5289c-340415df322634e4d06d2a80bf8f7b40.sdkstatcache -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/swift-overrides.hmap -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Products/Debug/include -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/DerivedSources-normal/arm64 -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/DerivedSources/arm64 -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name MyLibrary -package-name mylibrary -frontend-parseable-output -disable-clang-spi -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode_16_beta_3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode_16_beta_3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode_16_beta_3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode_16_beta_3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode_16_beta_3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode_16_beta_3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Build/Intermediates.noindex/MyLibrary.build/Debug/MyLibrary.build/Objects-normal/arm64/Repeater.o -index-unit-output-path /MyLibrary.build/Debug/MyLibrary.build/Objects-normal/arm64/Repeater.o -index-store-path /Users/rick/Library/Developer/Xcode/DerivedData/MyLibrary-fszgispmxfgjgmfpdyjlofjzlfuf/Index.noindex/DataStore -index-system-modules
1.  Apple Swift version 6.0 (swiftlang-6.0.0.5.15 clang-1600.0.22.6)
2.  Compiling with the current language version
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           0x000000010623dda8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010623bffc llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010623e374 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x000000019fee7584 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000019feb6c20 pthread_kill + 288
5  libsystem_c.dylib        0x000000019fdc3a30 abort + 180
6  swift-frontend           0x0000000100c1076c PrettyStackTraceFrontend::~PrettyStackTraceFrontend() + 0
7  swift-frontend           0x00000001061ada7c llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x00000001061ad964 llvm::report_fatal_error(llvm::Twine const&, bool) + 0
9  swift-frontend           0x00000001011f3d58 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           0x0000000105f75fe0 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) + 224
11 swift-frontend           0x00000001011e8794 swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*, llvm::raw_pwrite_stream*) + 3764
12 swift-frontend           0x00000001011ea3d0 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           0x0000000100c0ac9c generateCode(swift::CompilerInstance&, llvm::StringRef, llvm::Module*, llvm::GlobalVariable*) + 368
14 swift-frontend           0x0000000100c04fdc 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           0x0000000100c03e3c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 984
16 swift-frontend           0x0000000100c0823c performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1680
17 swift-frontend           0x0000000100c060e4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4280
18 swift-frontend           0x0000000100b8cc84 swift::mainEntry(int, char const**) + 3680
19 dyld                     0x000000019fb2e0e0 start + 2360

Environment

swift-driver version: 1.111.2 Apple Swift version 6.0 (swiftlang-6.0.0.5.15 clang-1600.0.22.6) Target: arm64-apple-macosx14.0

vanvoorden commented 1 month ago

I am unblocked (for now) by declaring the FnBox as internal.