CXX interop

Hi! Can you explain me how it's should works:

I've a simple SPM example with swift unsafe flags:

"-Xfrontend",
"-enable-cxx-interop",
"-Xfrontend",
"-enable-objc-interop",

Also I've main.swift file with inheritance from NSView:

import AppKit

class MyView: NSView {
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

let view = MyView()

And when I tried to compile it, I got the next error:

Compile error
[Sandbox] swift run
Building for debugging...
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSURLSession.h:679:66: warning: imported declaration 'NSURLSessionMultipathServiceType' could not be mapped to 'URLSessionConfiguration.MultipathServiceType'
} API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos) NS_SWIFT_NAME(URLSessionConfiguration.MultipathServiceType);
                                                                 ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSURLSession.h:679:66: note: please report this issue to the owners of 'Foundation'
} API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos) NS_SWIFT_NAME(URLSessionConfiguration.MultipathServiceType);
                                                                 ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSURLSession.h:833:3: warning: imported declaration 'NSURLSessionDelayedRequestDisposition' could not be mapped to 'URLSession.DelayedRequestDisposition'
} NS_SWIFT_NAME(URLSession.DelayedRequestDisposition) API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSURLSession.h:833:3: note: please report this issue to the owners of 'Foundation'
} NS_SWIFT_NAME(URLSession.DelayedRequestDisposition) API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
  ^
<unknown>:0: error: fatal error encountered while reading from module 'Foundation'; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/v.prusakov/Developer/AdaEngine/Sandbox/Sources/Sandbox/main.swift -emit-dependencies-path /Users/v.prusakov/Developer/AdaEngine/Sandbox/.build/arm64-apple-macosx/debug/Sandbox.build/main.d -emit-reference-dependencies-path /Users/v.prusakov/Developer/AdaEngine/Sandbox/.build/arm64-apple-macosx/debug/Sandbox.build/main.swiftdeps -target arm64-apple-macosx10.10 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/v.prusakov/Developer/AdaEngine/Sandbox/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/v.prusakov/Developer/AdaEngine/Sandbox/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -entry-point-function-name Sandbox_main -enable-cxx-interop -enable-objc-interop -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name Sandbox -target-sdk-version 12.3 -o /Users/v.prusakov/Developer/AdaEngine/Sandbox/.build/arm64-apple-macosx/debug/Sandbox.build/main.swift.o -index-store-path /Users/v.prusakov/Developer/AdaEngine/Sandbox/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.	Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)
2.	Compiling with the current language version
3.	While evaluating request LookupAllConformancesInContextRequest(extension of Foundation.(file).IndexPath)
4.	While loading conformances for 'IndexPath' (in module 'Foundation')
5.	*** DESERIALIZATION FAILURE ***
module 'Foundation' with full misc version '5.6(5.6)/Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)'
Could not deserialize type for 'ReferenceConvertible'
Caused by: result is ambiguous
Cross-reference to module 'ObjectiveC'
... NSObject


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           0x0000000107456f88 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107455f9c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107457618 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x00000001867634a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018674bee0 pthread_kill + 288
5  libsystem_c.dylib        0x0000000186686340 abort + 168
6  swift-frontend           0x0000000103294f38 swift::ModuleFileSharedCore::outputDiagnosticInfo(llvm::raw_ostream&) const + 0
7  swift-frontend           0x000000010322fae4 swift::ModuleFile::fatal(llvm::Error) const + 420
8  swift-frontend           0x000000010324de58 swift::ModuleFile::loadAllConformances(swift::Decl const*, unsigned long long, llvm::SmallVectorImpl<swift::ProtocolConformance*>&) + 1144
9  swift-frontend           0x0000000103dc83b8 swift::ConformanceLookupTable::updateLookupTable(swift::NominalTypeDecl*, swift::ConformanceLookupTable::ConformanceStage) + 284
10 swift-frontend           0x0000000103dc84c4 swift::ConformanceLookupTable::updateLookupTable(swift::NominalTypeDecl*, swift::ConformanceLookupTable::ConformanceStage) + 552
11 swift-frontend           0x0000000103dc85e8 swift::ConformanceLookupTable::updateLookupTable(swift::NominalTypeDecl*, swift::ConformanceLookupTable::ConformanceStage) + 844
12 swift-frontend           0x0000000103dcc6ac swift::ConformanceLookupTable::lookupConformances(swift::NominalTypeDecl*, swift::DeclContext*, std::__1::vector<swift::ProtocolConformance*, std::__1::allocator<swift::ProtocolConformance*> >*, llvm::SmallVectorImpl<swift::ConformanceDiagnostic>*) + 56
13 swift-frontend           0x0000000103f5e838 swift::LookupAllConformancesInContextRequest::evaluate(swift::Evaluator&, swift::IterableDeclContext const*) const + 404
14 swift-frontend           0x0000000103f5dad4 swift::IterableDeclContext::getLocalConformances(swift::ConformanceLookupKind) const + 2472
15 swift-frontend           0x0000000103023b18 (anonymous namespace)::IndexSwiftASTWalker::startEntity(swift::Decl*, swift::index::IndexSymbol&, bool) + 212
16 swift-frontend           0x0000000103022094 (anonymous namespace)::IndexSwiftASTWalker::walkToDeclPre(swift::Decl*, swift::CharSourceRange) + 5372
17 swift-frontend           0x000000010301ac00 (anonymous namespace)::SemaAnnotator::walkToDeclPre(swift::Decl*) + 2672
18 swift-frontend           0x0000000103d91498 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 236
19 swift-frontend           0x0000000103f0dbd8 swift::FileUnit::walk(swift::ASTWalker&) + 160
20 swift-frontend           0x000000010301864c swift::SourceEntityWalker::walk(swift::ModuleDecl&) + 224
21 swift-frontend           0x0000000103020a18 (anonymous namespace)::IndexSwiftASTWalker::visitModule(swift::ModuleDecl&) + 312
22 swift-frontend           0x0000000103020b6c swift::index::indexModule(swift::ModuleDecl*, swift::index::IndexDataConsumer&) + 320
23 swift-frontend           0x00000001030294a4 addModuleDependencies(llvm::ArrayRef<swift::ImportedModule>, llvm::StringRef, bool, bool, llvm::StringRef, clang::CompilerInstance const&, swift::DiagnosticEngine&, clang::index::IndexUnitWriter&, (anonymous namespace)::StringScratchSpace&, swift::SourceFile*) + 2532
24 swift-frontend           0x0000000103028244 recordSourceFileUnit(swift::SourceFile*, llvm::StringRef, llvm::StringRef, bool, bool, bool, llvm::StringRef, llvm::ArrayRef<clang::FileEntry const*>, clang::CompilerInstance const&, swift::DiagnosticEngine&) + 716
25 swift-frontend           0x0000000103027f28 swift::index::indexAndRecord(swift::SourceFile*, llvm::StringRef, llvm::StringRef, bool, bool, bool, llvm::StringRef, swift::DependencyTracker const&) + 308
26 swift-frontend           0x0000000102d07330 emitIndexDataForSourceFile(swift::SourceFile*, swift::CompilerInstance const&) + 644
27 swift-frontend           0x0000000102cff330 performEndOfPipelineActions(swift::CompilerInstance&) + 5868
28 swift-frontend           0x0000000102d05414 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*) + 3204
29 swift-frontend           0x0000000102cf8840 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5344
30 swift-frontend           0x0000000102cc0130 swift::mainEntry(int, char const**) + 808
31 dyld                     0x0000000109b4508c start + 520

I've no idea, how to mix Darwin frameworks and CXX interop together. Can you explain me how it's should works?

My prefs

Link to project: Sandbox.zip - Google Drive
Xcode version: 13.3.1 (13E500a)
Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)

Looks like you’re using too old a version of swift. This looks like a bug we encountered when importing NSObject protocol that Me Daniel Rodriguez and Alex Langford fixed back in March/April.

3 Likes

Yeah, I switched to swift 5.7 version and inheritance works, but I noticed, that OptionSet generated from objc headers (UIButton.State or NSDate.ReadingOptions) is broken.

Could you help me with that?

1 Like

That problem is probably fixed in main, but not in 5.7 ([C++-Interop] Fix EffectiveClangContext for NS_OPTIONS EnumDecl lookup. by plotfi · Pull Request #59042 · apple/swift · GitHub), however, it needs from changes in the Apple SDK headers to be complete, which has not happen yet (as far as I know, and i think the last Xcode 14 beta included no change in the SDKs).

2 Likes

Yeah Daniel is right. We’ve spent quite a bit of time between Daniel, Alex Langford, Zoe and myself on this. There are still some issues with applying attributes to the typedef and enums that I am working on in clang but the general case should work so long as you modify your CFAvailability.h in Foundation. @zoecarver is working in getting the change into Foundation.

I will post the change to the header you need shortly once I get to my desk.

1 Like

you want to change the

CF_OPTIONS line to

#define CF_OPTIONS(_type, _name) _type __attribute__((availability(swift, unavailable))) _name; enum __CF_OPTIONS_ATTRIBUTES : _name

in

/Applications/Xcode.app/Contents/Developer/Platforms/<PLATFORM_NAME>.platform/Developer/SDKs/<PLATFORM_NAME>.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFAvailability.h

you would want the block of code to look like this:

#if (__cplusplus)
#define CF_OPTIONS(_type, _name) _type __attribute__((availability(swift, unavailable))) _name; enum __CF_OPTIONS_ATTRIBUTES : _name
#else
#define CF_OPTIONS(_type, _name) enum __CF_OPTIONS_ATTRIBUTES _name : _type _name; enum _name : _type
#endif

At this point I have tested all the headers in Foundation and UIKit and C++-Interop matches the swift-ide-test output almost exactly with only a few exceptions. But this header does have to be altered.

1 Like