swiftlang / swift

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

generic atomic closure crashes 6.x compiler up to 24-08-02 #75724

Open wti opened 3 months ago

wti commented 3 months ago

Description

Crash in SILGEN on closing over consuming atomic parameter when AtomicRepresentable is generic.

Reproduction

import Synchronization

public enum Bug {
  case a, b
  public typealias AR = AtomicRepresentable

  // avoids crash
  //public func writeOp(_ item: consuming Atomic<Int>) -> (Int) -> Void {
  // crash when generic
  public func writeOp<T: AR>(_ item: consuming Atomic<T>) -> (T) -> Void
  where T.AtomicRepresentation == _Atomic64BitStorage {
    { (item).store($0, ordering: .sequentiallyConsistent) }
  }
}
Bug.a.writeOp(Atomic(0))(3)

Stack dump

Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-02-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Volumes/beta/Users/wes/git/Atom/Sources/Bug/main.swift -emit-dependencies-path /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/Objects-normal/arm64/main.d -emit-const-values-path /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/Objects-normal/arm64/main.swiftconstvalues -emit-reference-dependencies-path /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/Objects-normal/arm64/main.swiftdeps -serialize-diagnostics-path /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/Objects-normal/arm64/main.dia -target arm64-apple-macos15.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -I /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Products/Debug -I /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Products/Debug -F /Applications/Xcode-beta.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/wes/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 6 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -const-gather-protocols-file /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/Objects-normal/arm64/Bug_const_extract_protocols.json -enable-experimental-feature DebugDescriptionMacro -enable-experimental-feature OpaqueTypeErasure -empty-abi-descriptor -plugin-path /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-02-a.xctoolchain/usr/lib/swift/host/plugins -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins/testing#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -validate-clang-modules-once -clang-build-session-file /Users/wes/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Volumes/beta/Users/wes/git/Atom/.swiftpm/xcode -resource-dir /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-02-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Volumes/beta/Users/wes/git/Atom/.swiftpm/xcode -Xcc -I/Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Products/Debug/include -Xcc -I/Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/DerivedSources-normal/arm64 -Xcc -I/Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/DerivedSources/arm64 -Xcc -I/Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name Bug -package-name atom -frontend-parseable-output -disable-clang-spi -plugin-path /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-02-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-02-a.xctoolchain/usr/local/lib/swift/host/plugins -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -o /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Build/Intermediates.noindex/Atom.build/Debug/Bug.build/Objects-normal/arm64/main.o -index-unit-output-path /Atom.build/Debug/Bug.build/Objects-normal/arm64/main.o -index-store-path /Users/wes/Library/Developer/Xcode/DerivedData/Atom-gkrfjgzmjjuyedctpecynbhudxyh/Index.noindex/DataStore -index-system-modules
1.  Apple Swift version 6.0-dev (LLVM 097782ee554d9fe, Swift 60bf038ce965551)
2.  Compiling with the current language version
3.  While evaluating request IRGenRequest(IR Generation for file "/Volumes/beta/Users/wes/git/Atom/Sources/Bug/main.swift")
4.  While emitting IR SIL function "@$s3BugAAO7writeOpyyxc15Synchronization6AtomicVyxGnAD0E13RepresentableRzAD19_Atomic64BitStorageV0E14RepresentationRtzlF".
 for 'writeOp(_:)' (at /Volumes/beta/Users/wes/git/Atom/Sources/Bug/main.swift:10: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           0x000000010a0be994 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010a0bd160 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010a0befdc SignalHandler(int) + 304
3  libsystem_platform.dylib 0x0000000186938184 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000186900f70 pthread_kill + 288
5  libsystem_c.dylib        0x000000018680d908 abort + 128
6  libsystem_c.dylib        0x000000018680cc1c err + 0
7  swift-frontend           0x000000010a112658 swift::irgen::emitArchetypeTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::ArchetypeType>, swift::irgen::DynamicMetadataRequest) (.cold.6) + 0
8  swift-frontend           0x0000000104f8a0c4 swift::irgen::emitArchetypeTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::ArchetypeType>, swift::irgen::DynamicMetadataRequest) + 1300
9  swift-frontend           0x00000001051c9508 emitDirectTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanType, swift::irgen::DynamicMetadataRequest) + 2464
10 swift-frontend           0x00000001051c83c8 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType, swift::irgen::DynamicMetadataRequest) + 360
11 swift-frontend           0x00000001050ba460 swift::irgen::emitGenericRequirementFromSubstitutions(swift::irgen::IRGenFunction&, swift::GenericRequirement, swift::MetadataState, swift::SubstitutionMap, bool) + 272
12 swift-frontend           0x00000001051ceb7c swift::irgen::GenericArguments::collect(swift::irgen::IRGenFunction&, swift::SubstitutionMap) + 148
13 swift-frontend           0x00000001051ce680 emitNominalMetadataRef(swift::irgen::IRGenFunction&, swift::NominalTypeDecl*, swift::CanType, swift::irgen::DynamicMetadataRequest) + 640
14 swift-frontend           0x00000001051ced54 (anonymous namespace)::EmitTypeMetadataRef::visitBoundGenericType(swift::CanTypeWrapper<swift::BoundGenericType>, swift::irgen::DynamicMetadataRequest) + 144
15 swift-frontend           0x00000001051c928c emitDirectTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanType, swift::irgen::DynamicMetadataRequest) + 1828
16 swift-frontend           0x00000001051c83c8 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType, swift::irgen::DynamicMetadataRequest) + 360
17 swift-frontend           0x00000001051cbe30 swift::irgen::IRGenFunction::emitTypeMetadataRefForLayout(swift::SILType, swift::irgen::DynamicMetadataRequest) + 296
18 swift-frontend           0x0000000105086d64 swift::irgen::IRGenFunction::emitValueWitnessTableRef(swift::SILType, swift::irgen::DynamicMetadataRequest, llvm::Value**) + 180
19 swift-frontend           0x0000000105086ca4 swift::irgen::IRGenFunction::emitValueWitnessTableRef(swift::SILType, llvm::Value**) + 32
20 swift-frontend           0x00000001050a1c14 swift::irgen::IRGenFunction::emitValueWitnessFunctionRef(swift::SILType, llvm::Value*&, swift::irgen::ValueWitness) + 260
21 swift-frontend           0x00000001050a4cec swift::irgen::emitDestroyCall(swift::irgen::IRGenFunction&, swift::SILType, swift::irgen::Address) + 96
22 swift-frontend           0x00000001050f44d8 tryEmitDeinitCall(swift::irgen::IRGenFunction&, swift::SILType, llvm::function_ref<void (swift::irgen::Explosion&)>, llvm::function_ref<swift::irgen::Address ()>, llvm::function_ref<void ()>) + 560
23 swift-frontend           0x00000001050f482c swift::irgen::tryEmitDestroyUsingDeinit(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType) + 96
24 swift-frontend           0x00000001050da980 (anonymous namespace)::StructTypeInfoBase<(anonymous namespace)::FixedStructTypeInfo, swift::irgen::IndirectTypeInfo<(anonymous namespace)::FixedStructTypeInfo, swift::irgen::FixedTypeInfo>, (anonymous namespace)::StructFieldInfo>::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType, bool) const + 76
25 swift-frontend           0x0000000105062380 swift::irgen::HeapLayout::getPrivateMetadata(swift::irgen::IRGenModule&, llvm::Constant*) const + 648
26 swift-frontend           0x000000010506e014 (anonymous namespace)::FixedBoxTypeInfoBase::allocate(swift::irgen::IRGenFunction&, swift::SILType, swift::GenericEnvironment*, llvm::Twine const&) const + 324
27 swift-frontend           0x0000000105066e78 swift::irgen::emitAllocateBox(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::SILBoxType>, swift::GenericEnvironment*, llvm::Twine const&) + 200
28 swift-frontend           0x000000010516ac7c (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 60452
29 swift-frontend           0x000000010515aebc (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 9204
30 swift-frontend           0x0000000105158534 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1592
31 swift-frontend           0x0000000104ff8d80 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&) + 740
32 swift-frontend           0x000000010510ab4c swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2172
33 swift-frontend           0x00000001051579f0 swift::GeneratedModule swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 200
34 swift-frontend           0x0000000105113208 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'()) + 212
35 swift-frontend           0x000000010510cbb4 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**) + 208
36 swift-frontend           0x0000000104c16430 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
37 swift-frontend           0x0000000104c12e48 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*) + 1416
38 swift-frontend           0x0000000104c122a8 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 656
39 swift-frontend           0x0000000104c1dfd0 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
40 swift-frontend           0x0000000104c1419c performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
41 swift-frontend           0x0000000104c13870 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2372
42 swift-frontend           0x0000000104a28bc8 swift::mainEntry(int, char const**) + 3096
43 dyld                     0x000000018657f274 start + 2840

Expected behavior

Working code

Environment

Additional information

Output prior to stack trace, after invocation

Metadata for archetype not bound in function.
  The metadata could be missing entirely because it needs to be passed to the function.
  Or the metadata is present and not bound in which case setScopedLocalTypeMetadata or similar must be called.
Archetype without metadata: τ_0_0
(primary_archetype_type address=0x11eb0e388 conforms_to="Synchronization.(file).AtomicRepresentable" name="\xCF\x84_0_0"
  (interface_type=generic_type_param_type depth=0 index=0))
Function:
define private swiftcc void @objectdestroy(ptr swiftself %0) #0 !dbg !6 {
  %2 = alloca i1, align 1
  %3 = getelementptr inbounds <{ %swift.refcounted, %T15Synchronization6AtomicV.0 }>, ptr %0, i32 0, i32 1, !dbg !10
}
No LocalTypeDataCache for this function!
Assertion failed: (archetype->getParent() && "Not a nested archetype"), function emitArchetypeTypeMetadataRef, file GenArchetype.cpp, line 99.
wti commented 3 months ago

Same result in Xcode Version 16.0 beta 6 (16A5230g) (swiftlang-6.0.0.9.10 clang-1600.0.26.2))