Skip to content

Commit d60615a

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 d60615a

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
@@ -509,6 +509,18 @@ namespace {
509509
return importFunctionPointerLikeType(*type, pointeeType);
510510
}
511511

512+
// If non-copyable generics are disabled, we cannot specify
513+
// UnsafePointer<T> with a non-copyable type T.
514+
if (pointeeType && pointeeType->isNoncopyable() &&
515+
!Impl.SwiftContext.LangOpts.hasFeature(
516+
Feature::NoncopyableGenerics)) {
517+
auto opaquePointerDecl = Impl.SwiftContext.getOpaquePointerDecl();
518+
if (!opaquePointerDecl)
519+
return Type();
520+
return {opaquePointerDecl->getDeclaredInterfaceType(),
521+
ImportHint::OtherPointer};
522+
}
523+
512524
PointerTypeKind pointerKind;
513525
if (quals.hasConst()) {
514526
pointerKind = PTK_UnsafePointer;

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)