Skip to content

Commit a68e9c4

Browse files
committed
Revert "CxxInterop: remove checks for NoncopyableGenerics"
This reverts commit 9703319.
1 parent 6998295 commit a68e9c4

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5776,6 +5776,20 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
57765776
}
57775777

57785778
if (auto var = dyn_cast<VarDecl>(decl)) {
5779+
auto oldContext = var->getDeclContext();
5780+
auto oldTypeDecl = oldContext->getSelfNominalTypeDecl();
5781+
// If the base type is non-copyable, and non-copyable generics are disabled,
5782+
// we cannot synthesize the accessor, because its implementation would use
5783+
// `UnsafePointer<BaseTy>`.
5784+
// We cannot use `ty->isNoncopyable()` here because that would create a
5785+
// cyclic dependency between ModuleQualifiedLookupRequest and
5786+
// LookupConformanceInModuleRequest, so we check for the presence of
5787+
// move-only attribute that is implicitly added to non-copyable C++ types by
5788+
// ClangImporter.
5789+
if (oldTypeDecl->getAttrs().hasAttribute<MoveOnlyAttr>() &&
5790+
!context.LangOpts.hasFeature(Feature::NoncopyableGenerics))
5791+
return nullptr;
5792+
57795793
auto rawMemory = allocateMemoryForDecl<VarDecl>(var->getASTContext(),
57805794
sizeof(VarDecl), false);
57815795
auto out =

lib/ClangImporter/ImportType.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,26 @@ 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+
// We cannot use `ty->isNoncopyable()` here because that would create a
515+
// cyclic dependency between ModuleQualifiedLookupRequest and
516+
// LookupConformanceInModuleRequest, so we check for the presence of
517+
// move-only attribute that is implicitly added to non-copyable C++ types
518+
// by ClangImporter.
519+
if (pointeeType && pointeeType->getAnyNominal() &&
520+
pointeeType->getAnyNominal()
521+
->getAttrs()
522+
.hasAttribute<MoveOnlyAttr>() &&
523+
!Impl.SwiftContext.LangOpts.hasFeature(
524+
Feature::NoncopyableGenerics)) {
525+
auto opaquePointerDecl = Impl.SwiftContext.getOpaquePointerDecl();
526+
if (!opaquePointerDecl)
527+
return Type();
528+
return {opaquePointerDecl->getDeclaredInterfaceType(),
529+
ImportHint::OtherPointer};
530+
}
531+
512532
PointerTypeKind pointerKind;
513533
if (quals.hasConst()) {
514534
pointerKind = PTK_UnsafePointer;

test/Interop/Cxx/class/move-only/move-only-cxx-value-type-generics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift)
2-
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O -Xfrontend -sil-verify-none)
1+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics)
2+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics -O -Xfrontend -sil-verify-none)
33
//
44
// REQUIRES: executable_test
55
// REQUIRES: GH_ISSUE_70246
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +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-NCG
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
26

37
// CHECK-NCG: func getNonCopyablePtr() -> UnsafeMutablePointer<NonCopyable>
48
// CHECK-NCG: func getNonCopyableDerivedPtr() -> UnsafeMutablePointer<NonCopyableDerived>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift)
2+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
23
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9 -O)
34
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O)
5+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
46

57
// REQUIRES: executable_test
68

@@ -27,10 +29,12 @@ MoveOnlyCxxValueType.test("Test derived move only type member access") {
2729
var k = c.method(-3)
2830
expectEqual(k, -6)
2931
expectEqual(c.method(1), 2)
32+
#if HAS_NONCOPYABLE_GENERICS
3033
k = c.x
3134
expectEqual(k, 2)
3235
c.x = 11
3336
expectEqual(c.x, 11)
37+
#endif
3438
k = c.mutMethod(-13)
3539
expectEqual(k, -13)
3640
}
@@ -56,6 +60,7 @@ MoveOnlyCxxValueType.test("Test move only field access in holder") {
5660
expectEqual(c.x.x, 5)
5761
}
5862

63+
#if HAS_NONCOPYABLE_GENERICS
5964
MoveOnlyCxxValueType.test("Test move only field access in derived holder") {
6065
var c = NonCopyableHolderDerivedDerived(-11)
6166
var k = borrowNC(c.x)
@@ -69,5 +74,6 @@ MoveOnlyCxxValueType.test("Test move only field access in derived holder") {
6974
c.x.mutMethod(5)
7075
expectEqual(c.x.x, 5)
7176
}
77+
#endif
7278

7379
runAllTests()

test/Interop/Cxx/operators/move-only/move-only-synthesized-properties.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9)
22
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6)
33
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift)
4+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
45
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9 -O)
56
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O)
67
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O)
8+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
79
//
810
// REQUIRES: executable_test
911

@@ -90,6 +92,7 @@ MoveOnlyCxxOperators.test("testNonCopyableHolderValueMutDeref pointee value") {
9092
expectEqual(k.x, k2.x)
9193
}
9294

95+
#if HAS_NONCOPYABLE_GENERICS
9396
MoveOnlyCxxOperators.test("NonCopyableHolderConstDerefDerivedDerived pointee borrow") {
9497
let holder = NonCopyableHolderConstDerefDerivedDerived(11)
9598
var k = borrowNC(holder.pointee)
@@ -157,5 +160,6 @@ MoveOnlyCxxOperators.test("testNonCopyableHolderValueMutDerefDerivedDerived poin
157160
var k2 = holder.pointee
158161
expectEqual(k.x, k2.x)
159162
}
163+
#endif
160164

161165
runAllTests()

0 commit comments

Comments
 (0)