Skip to content

Swift Compiler Crash When Creating Array of C++ Values in Swift #79210

Closed
@juraskrlec

Description

@juraskrlec

Description

The Swift compiler crashes when creating an array of C++ values in Swift, without passing it to a C++ back. This occurs when using Swift’s C++ interoperability, specifically when constructing an array of C++ objects inside Swift code.

Reproduction

C++ Settings (exposed to Swift)

struct CustomSettings
{
    std::optional< OptionEnum1 > option1;
    std::optional< OptionEnum2 > option2;
};

struct FullSettings 
{
    std::vector<CustomSettings> customSettings;
}

std::optional< OptionEnum1 > convertToOptionalCpp( OptionEnum1 option1 ) noexcept
{
    return std::make_optional( option1 );
}
struct CustomSettings: Sendable {
   var option1: OptionEnum1?
   var option2: OptionEnum1?
}

struct FullSettings: Sendable {
   var customSettings: Array<CustomSettings>?
}

func convertToCpp(_ settings: CustomSettings) -> NativeCPP.CustomSettings {
   var cppCustomSettings = NativeCPP.CustomSettings();

   if let option1 = setting.option1 { 
     cppCustomSettings.option1 = NativeCPP.convertToOptionalCpp(option1)
   }
   if let option2 = setting.option2 { 
     cppCustomSettings.option2 = NativeCPP..convertToOptionalCpp(option2)
   }

   return cppCustomSettings;

}

func convertSettings() -> NativeCPP.FullSettings {

   if let customSettings = self.fullSettings.customSettings {
       let cppCustomSettings = customSettings.map{ convertToCpp($0) } // -> Swift Compiler Crash
   }
}

Stack dump

1.	Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
2.	Compiling with the current language version
3.	While evaluating request IRGenRequest(IR Generation for file "/Users/juraskrlec/Documents/SDKs/DocumentVerification/sdk-document-verification-v2-ios/DocumentVerification/DocumentVerification/Capture/Swift/BlinkID/Session/BlinkIDSessionSettings.swift")
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           0x000000010847f0fc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010847d350 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010847f6c8 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x000000018e000184 _sigtramp + 56
4  swift-frontend           0x00000001032c33ec swift::irgen::IRGenModule::emitDirectRelativeReference(llvm::Constant*, llvm::GlobalValue*, llvm::ArrayRef<unsigned int>) + 72
5  swift-frontend           0x00000001032c33ec swift::irgen::IRGenModule::emitDirectRelativeReference(llvm::Constant*, llvm::GlobalValue*, llvm::ArrayRef<unsigned int>) + 72
6  swift-frontend           0x00000001032c4778 swift::irgen::IRGenModule::emitTypeMetadataRecords(bool)::$_1::operator()(llvm::ArrayRef<swift::GenericTypeDecl*>, llvm::StringRef) const + 480
7  swift-frontend           0x00000001032ad864 swift::irgen::IRGenModule::emitTypeMetadataRecords(bool) + 880
8  swift-frontend           0x000000010341cd78 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 5008
9  swift-frontend           0x0000000103464954 swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 176
10 swift-frontend           0x0000000103425b3c 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'()) + 784
11 swift-frontend           0x000000010341f574 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**) + 180
12 swift-frontend           0x0000000102e2e85c 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
13 swift-frontend           0x0000000102e2a088 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*) + 2176
14 swift-frontend           0x0000000102e28f74 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 984
15 swift-frontend           0x0000000102e2c228 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1680
16 swift-frontend           0x0000000102e2af58 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
17 swift-frontend           0x0000000102db201c swift::mainEntry(int, char const**) + 3680
18 dyld                     0x000000018dc48274 start + 2840

Expected behavior

The compiler should correctly handle the creation of an array of C++ values in Swift and allow normal usage.

Environment

Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2), arm64

Additional information

  • The crash does not occur when passing a single C++ value.
  • The crash does not occur when creating the array in C++ and passing it to Swift.
  • The issue appears to be related to Swift’s handling of C++ value semantics.

Would appreciate any insights or workarounds!

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.c++ interopFeature: Interoperability with C++crashBug: A crash, i.e., an abnormal termination of software

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions