Skip to content

Non-Copyable Struct Prevents Function Specialization, Crashing in Embedded Mode #72627

@filip-sakel

Description

@filip-sakel

Description

Compiling this code in embedded mode crashes the compiler; I think it's because some generic function is not specialized. When removing the non-copyability constraint, the program compiles just fine.

Reproduction

CollectionHelpers.swift

struct ListNode<Element: ~Copyable>: ~Copyable {
    typealias Link = Box<ListNode<Element>>?

    var element: Element
    var next: Link
}

struct List<Element: ~Copyable>: ~Copyable {
    typealias Link = Box<ListNode<Element>>?
  
    var head: Link = nil

    init(head: consuming Link = nil) {
        self.head = head
    }

    mutating func push(_ element: consuming Element) {
        self = Self(head: Box(ListNode(element: element, next: self.head)))
    }
}

main.swift:

@_expose(wasm, "main")
func main() {
    var myList = List<Int>()
    myList.push(1)
    let _ = consume myList
}

Stack dump

Assertion failed: (!f->isGeneric()), function addLazyFunction, file GenDecl.cpp, line 1579.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.      Program arguments: /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-03-26-a.xctoolchain/usr/bin/swift-frontend -frontend -c /pathto/EmbeddedWebApp/Sources/swift-audio/CollectionHelpers.swift /pathto/EmbeddedWebApp/Sources/swift-audio/DynamicProperty.swift /pathto/EmbeddedWebApp/Sources/swift-audio/JSString.swift /pathto/EmbeddedWebApp/Sources/swift-audio/main.swift /pathto/EmbeddedWebApp/Sources/swift-audio/stack_protector_shim.swift -supplementary-output-file-map /var/folders/v1/743s6n2503v7w2vhl8t5yctm0000gn/T/TemporaryDirectory.qBHUzf/supplementaryOutputs-1 -target wasm32-unknown-none-wasm -disable-objc-interop -cxx-interoperability-mode=default -I /pathto/EmbeddedWebApp/.build/wasm32-unknown-none-wasm/release/Modules -I /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /pathto/EmbeddedWebApp/.build/wasm32-unknown-none-wasm/release/ModuleCache -swift-version 5 -O -D SWIFT_PACKAGE -gnone -disable-round-trip-debug-types -enable-experimental-feature Embedded -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature MoveOnlyPartialConsumption -enable-experimental-feature BorrowingSwitch -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-03-26-a.xctoolchain/usr/lib/swift -file-compilation-dir /pathto/EmbeddedWebApp -Xcc -fmodule-map-file=/pathto/EmbeddedWebApp/.build/wasm32-unknown-none-wasm/release/dlmalloc.build/module.modulemap -Xcc -I -Xcc /pathto/EmbeddedWebApp/Sources/dlmalloc/include -Xcc -fdeclspec -Xcc --sysroot -Xcc /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -Xcc -F -Xcc /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC -Xcc -g -module-name swift_audio -package-name embeddedwebapp -disable-stack-protector -plugin-path /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-03-26-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-03-26-a.xctoolchain/usr/local/lib/swift/host/plugins -num-threads 8 -o /pathto/EmbeddedWebApp/.build/wasm32-unknown-none-wasm/release/swift_audio.build/CollectionHelpers.swift.o -o /pathtoEmbeddedWebApp/.build/wasm32-unknown-none-wasm/release/swift_audio.build/DynamicProperty.swift.o -o /pathto/EmbeddedWebApp/.build/wasm32-unknown-none-wasm/release/swift_audio.build/JSString.swift.o -o /pathto/EmbeddedWebApp/.build/wasm32-unknown-none-wasm/release/swift_audio.build/main.swift.o -o /pathtoEmbeddedWebApp/.build/wasm32-unknown-none-wasm/release/swift_audio.build/stack_protector_shim.swift.o
1.      Apple Swift version 6.0-dev (LLVM dee589f75475708, Swift ac473570830fe6d)
2.      Compiling with effective version 5.10
3.      While emitting IR SIL function "@$s11swift_audio4mainSiyF".
 for 'main()' (at /pathto/EmbeddedWebApp/Sources/swift-audio/main.swift:296:1)
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           0x000000010610a420 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106108bec llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010610aa68 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a7842a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a7813c28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a7721ae8 abort + 180
6  libsystem_c.dylib        0x00000001a7720e44 err + 0
7  swift-frontend           0x000000010617c008 llvm::SmallPtrSetImpl<swift::SILFunction*>::insert(swift::SILFunction*) (.cold.1) + 0
8  swift-frontend           0x00000001010adfb4 swift::irgen::IRGenerator::addLazyFunction(swift::SILFunction*) + 368
9  swift-frontend           0x00000001010b0aac swift::irgen::IRGenModule::getAddrOfSILFunction(swift::SILFunction*, swift::ForDefinition_t, bool, bool) + 1304
10 swift-frontend           0x00000001011a36a4 tryEmitDeinitCall(swift::irgen::IRGenFunction&, swift::SILType, llvm::function_ref<void (swift::irgen::Explosion&)>, llvm::function_ref<swift::irgen::Address ()>, llvm::function_ref<void ()>) + 240
11 swift-frontend           0x00000001011a3b38 swift::irgen::tryEmitDestroyUsingDeinit(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType) + 96
12 swift-frontend           0x0000000101187aa8 (anonymous namespace)::StructTypeInfoBase<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo>::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType, bool) const + 76
13 swift-frontend           0x00000001010e81a8 (anonymous namespace)::SinglePayloadEnumImplStrategy::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType, bool) const + 524
14 swift-frontend           0x00000001010ef2ec (anonymous namespace)::EnumTypeInfoBase<swift::irgen::LoadableTypeInfo>::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType, bool) const + 48
15 swift-frontend           0x0000000101189578 swift::irgen::RecordTypeInfoImpl<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true>::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType, bool) const + 396
16 swift-frontend           0x0000000101187ad4 (anonymous namespace)::StructTypeInfoBase<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo>::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType, bool) const + 120
17 swift-frontend           0x00000001012865e0 void llvm::function_ref<void (swift::irgen::IRGenFunction&)>::callback_fn<swift::irgen::IRGenModule::getOrCreateOutlinedDestroyFunction(swift::SILType, swift::irgen::TypeInfo const&, swift::irgen::OutliningMetadataCollector const&)::$_7>(long, swift::irgen::IRGenFunction&) + 300
18 swift-frontend           0x00000001010be684 swift::irgen::IRGenModule::getOrCreateHelperFunction(llvm::StringRef, llvm::Type*, llvm::ArrayRef<llvm::Type*>, llvm::function_ref<void (swift::irgen::IRGenFunction&)>, bool, bool, bool) + 348
19 swift-frontend           0x000000010128359c swift::irgen::IRGenModule::getOrCreateOutlinedDestroyFunction(swift::SILType, swift::irgen::TypeInfo const&, swift::irgen::OutliningMetadataCollector const&) + 496
20 swift-frontend           0x00000001012832d4 swift::irgen::OutliningMetadataCollector::emitCallToOutlinedDestroy(swift::irgen::Address, swift::SILType, swift::irgen::TypeInfo const&) const + 296
21 swift-frontend           0x0000000101286480 void llvm::function_ref<void (swift::irgen::OutliningMetadataCollector&)>::callback_fn<swift::irgen::TypeInfo::callOutlinedDestroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType) const::$_6>(long, swift::irgen::OutliningMetadataCollector&) + 120
22 swift-frontend           0x0000000101281e54 swift::irgen::TypeInfo::withWitnessableMetadataCollector(swift::irgen::IRGenFunction&, swift::SILType, swift::irgen::LayoutIsNeeded_t, swift::irgen::DeinitIsNeeded_t, llvm::function_ref<void (swift::irgen::OutliningMetadataCollector&)>) const + 352
23 swift-frontend           0x0000000101283158 swift::irgen::TypeInfo::callOutlinedDestroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType) const + 124
24 swift-frontend           0x00000001011894b8 swift::irgen::RecordTypeInfoImpl<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true>::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType, bool) const + 204
25 swift-frontend           0x0000000101187ad4 (anonymous namespace)::StructTypeInfoBase<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo>::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::SILType, bool) const + 120
26 swift-frontend           0x0000000101046b6c swift::irgen::emitBuiltinCall(swift::irgen::IRGenFunction&, swift::BuiltinInfo const&, swift::BuiltinInst*, llvm::ArrayRef<swift::SILType>, swift::irgen::Explosion&, swift::irgen::Explosion&) + 15852
27 swift-frontend           0x000000010121abf0 (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 64028
28 swift-frontend           0x000000010120a020 (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 9544
29 swift-frontend           0x000000010120757c swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1580
30 swift-frontend           0x00000001010abed4 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
31 swift-frontend           0x00000001011bae68 swift::performIRGeneration(swift::ModuleDecl*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, llvm::GlobalVariable**) + 1496
32 swift-frontend           0x0000000100cd4c30 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>>>) + 272
33 swift-frontend           0x0000000100cd0e64 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
34 swift-frontend           0x0000000100cd0530 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1164
35 swift-frontend           0x0000000100ce14ec withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
36 swift-frontend           0x0000000100cd2928 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
37 swift-frontend           0x0000000100cd1894 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2380
38 swift-frontend           0x0000000100aebc70 swift::mainEntry(int, char const**) + 3096
39 dyld                     0x00000001a74bbf28 start + 2236

Expected behavior

I expect all functions to be properly specialized and for the compiler to give me the resulting binary.

Environment

Apple Swift version 6.0-dev (LLVM dee589f75475708, Swift ac47357)
Target: arm64-apple-macosx13.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwareembeddedEmbedded Swiftnoncopyable struct/enumFeature → declarations: Noncopyable value type declarations

    Type

    No type

    Projects

    Status

    Verify

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions