Skip to content

Commit ec5162a

Browse files
committed
[cxx-interop] Import NonCopyable* as OpaquePointer if noncopyable generics are disabled
C++ pointer type `T*` is generally imported as `Unsafe(Mutable)Pointer<T>`. However, if `T` is non-copyable in Swift (e.g. it has a deleted C++ copy constructor), using `UnsafePointer<T>` type requires noncopyable generics to be enabled. This was causing assertion failures when building SwiftCompilerSources in #72912.
1 parent a263bca commit ec5162a

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,18 @@ namespace {
498498
if (pointeeType && pointeeType->isForeignReferenceType())
499499
return {pointeeType, ImportHint::OtherPointer};
500500

501+
// If non-copyable generics are disabled, we cannot specify
502+
// UnsafePointer<T> with a non-copyable type T.
503+
if (pointeeType && pointeeType->isNoncopyable() &&
504+
!Impl.SwiftContext.LangOpts.hasFeature(
505+
Feature::NoncopyableGenerics)) {
506+
auto opaquePointerDecl = Impl.SwiftContext.getOpaquePointerDecl();
507+
if (!opaquePointerDecl)
508+
return Type();
509+
return {opaquePointerDecl->getDeclaredInterfaceType(),
510+
ImportHint::OtherPointer};
511+
}
512+
501513
// If the pointed-to type is unrepresentable in Swift, or its C
502514
// alignment is greater than the maximum Swift alignment, import as
503515
// OpaquePointer.

test/Interop/Cxx/class/move-only/Inputs/move-only-cxx-value-type.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@ struct NonCopyableHolderDerivedDerived: NonCopyableHolderDerived {
5353
}
5454
};
5555

56+
inline NonCopyable *getNonCopyablePtr() { return nullptr; }
57+
inline NonCopyableDerived *getNonCopyableDerivedPtr() { return nullptr; }
58+
5659
#endif // TEST_INTEROP_CXX_CLASS_MOVE_ONLY_VT_H
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=MoveOnlyCxxValueType -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -source-filename=x | %FileCheck %s --check-prefix=CHECK-NO-NCG
2+
// RUN: %target-swift-ide-test -print-module -module-to-print=MoveOnlyCxxValueType -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -source-filename=x -enable-experimental-feature NoncopyableGenerics | %FileCheck %s --check-prefix=CHECK-NCG
3+
4+
// CHECK-NO-NCG: func getNonCopyablePtr() -> OpaquePointer
5+
// CHECK-NO-NCG: func getNonCopyableDerivedPtr() -> OpaquePointer
6+
7+
// CHECK-NCG: func getNonCopyablePtr() -> UnsafeMutablePointer<NonCopyable>
8+
// CHECK-NCG: func getNonCopyableDerivedPtr() -> UnsafeMutablePointer<NonCopyableDerived>

0 commit comments

Comments
 (0)