Skip to content

[cxx-interop] Fix passing optional CoreFoundation types from Swift to C++ #83609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ static bool isOptionalForeignReferenceType(Type ty) {
if (auto obj = ty->getOptionalObjectType()) {
if (const auto *cd =
dyn_cast_or_null<ClassDecl>(obj->getNominalOrBoundGenericNominal()))
return cd->isForeignReferenceType();
return cd->isForeignReferenceType() ||
cd->getForeignClassKind() == ClassDecl::ForeignKind::CFType;
}
return false;
}
Expand Down Expand Up @@ -1780,7 +1781,8 @@ bool DeclAndTypeClangFunctionPrinter::hasKnownOptionalNullableCxxMapping(
return typeInfo->canBeNullable;
}
if (const auto *cd = dyn_cast<ClassDecl>(nominal))
if (cd->isForeignReferenceType())
if (cd->isForeignReferenceType() ||
cd->getForeignClassKind() == ClassDecl::ForeignKind::CFType)
return true;
return isa_and_nonnull<clang::ObjCInterfaceDecl>(nominal->getClangDecl());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public func foobar(_ a: CFData) -> Bool {
true
}

public func returnsCFDate() -> CFDate? {
nil
}

public func takesCFDate(x: CFDate?) {}

public func networkThing() -> in_addr? {
return nil
}
Expand All @@ -25,4 +31,9 @@ public enum MyEnum {
}

// CHECK: SWIFT_EXTERN bool $s17UseCoreFoundation6foobarySbSo9CFDataRefaF(CFDataRef _Nonnull a) SWIFT_NOEXCEPT SWIFT_CALL; // foobar(_:)
// CHECK: SWIFT_EXTERN CFDateRef _Nullable $s17UseCoreFoundation13returnsCFDateSo0E3RefaSgyF(void) SWIFT_NOEXCEPT SWIFT_CALL; // returnsCFDate()
// CHECK: SWIFT_EXTERN void $s17UseCoreFoundation11takesCFDate1xySo0E3RefaSg_tF(CFDateRef _Nullable x) SWIFT_NOEXCEPT SWIFT_CALL; // takesCFDate(x:)

// CHECK: SWIFT_INLINE_THUNK swift::Optional<in_addr> networkThing() noexcept SWIFT_SYMBOL("s:17UseCoreFoundation12networkThingSo7in_addrVSgyF") SWIFT_WARN_UNUSED_RESULT {
// CHECK: SWIFT_INLINE_THUNK CFDateRef _Nullable returnsCFDate() noexcept SWIFT_SYMBOL("s:17UseCoreFoundation13returnsCFDateSo0E3RefaSgyF") SWIFT_WARN_UNUSED_RESULT {
// CHECK: SWIFT_INLINE_THUNK void takesCFDate(CFDateRef _Nullable x) noexcept SWIFT_SYMBOL("s:17UseCoreFoundation11takesCFDate1xySo0E3RefaSg_tF") {