Skip to content

Commit 37e8d26

Browse files
committed
[cxx-interop] Do not try to use UnsafePointer<T> for non-copyable T when adding base member accessors
(cherry picked from commit 6140ba1)
1 parent e594d1e commit 37e8d26

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5709,6 +5709,8 @@ DeclAttributes cloneImportedAttributes(ValueDecl *decl, ASTContext &context) {
57095709

57105710
static ValueDecl *
57115711
cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
5712+
ASTContext &context = decl->getASTContext();
5713+
57125714
if (auto fn = dyn_cast<FuncDecl>(decl)) {
57135715
// TODO: function templates are specialized during type checking so to
57145716
// support these we need to tell Swift to type check the synthesized bodies.
@@ -5726,7 +5728,6 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
57265728
return nullptr;
57275729
}
57285730

5729-
ASTContext &context = decl->getASTContext();
57305731
auto out = FuncDecl::createImplicit(
57315732
context, fn->getStaticSpelling(), fn->getName(),
57325733
fn->getNameLoc(), fn->hasAsync(), fn->hasThrows(),
@@ -5762,6 +5763,20 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
57625763
}
57635764

57645765
if (auto var = dyn_cast<VarDecl>(decl)) {
5766+
auto oldContext = var->getDeclContext();
5767+
auto oldTypeDecl = oldContext->getSelfNominalTypeDecl();
5768+
// If the base type is non-copyable, and non-copyable generics are disabled,
5769+
// we cannot synthesize the accessor, because its implementation would use
5770+
// `UnsafePointer<BaseTy>`.
5771+
// We cannot use `ty->isNoncopyable()` here because that would create a
5772+
// cyclic dependency between ModuleQualifiedLookupRequest and
5773+
// LookupConformanceInModuleRequest, so we check for the presence of
5774+
// move-only attribute that is implicitly added to non-copyable C++ types by
5775+
// ClangImporter.
5776+
if (oldTypeDecl->getAttrs().hasAttribute<MoveOnlyAttr>() &&
5777+
!context.LangOpts.hasFeature(Feature::NoncopyableGenerics))
5778+
return nullptr;
5779+
57655780
auto rawMemory = allocateMemoryForDecl<VarDecl>(var->getASTContext(),
57665781
sizeof(VarDecl), false);
57675782
auto out =

test/Interop/Cxx/class/move-only/inherited-field-access-irgen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-emit-irgen -I %S/Inputs -cxx-interoperability-mode=swift-6 %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s
2-
// RUN: %target-swift-emit-irgen -I %S/Inputs -cxx-interoperability-mode=upcoming-swift %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s
1+
// RUN: %target-swift-emit-irgen -I %S/Inputs -cxx-interoperability-mode=swift-6 -enable-experimental-feature NoncopyableGenerics %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s
2+
// RUN: %target-swift-emit-irgen -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s
33

44
import MoveOnlyCxxValueType
55

test/Interop/Cxx/class/move-only/inherited-field-access-silgen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-emit-sil -I %S/Inputs -cxx-interoperability-mode=swift-6 %s -validate-tbd-against-ir=none | %FileCheck %s
2-
// RUN: %target-swift-emit-sil -I %S/Inputs -cxx-interoperability-mode=upcoming-swift %s -validate-tbd-against-ir=none | %FileCheck %s
1+
// RUN: %target-swift-emit-sil -I %S/Inputs -cxx-interoperability-mode=swift-6 -enable-experimental-feature NoncopyableGenerics %s -validate-tbd-against-ir=none | %FileCheck %s
2+
// RUN: %target-swift-emit-sil -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics %s -validate-tbd-against-ir=none | %FileCheck %s
33

44
import MoveOnlyCxxValueType
55

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +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-6 -O)
4+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
35

4-
//
56
// REQUIRES: executable_test
67

78
import MoveOnlyCxxValueType
@@ -27,10 +28,12 @@ MoveOnlyCxxValueType.test("Test derived move only type member access") {
2728
var k = c.method(-3)
2829
expectEqual(k, -6)
2930
expectEqual(c.method(1), 2)
31+
#if HAS_NONCOPYABLE_GENERICS
3032
k = c.x
3133
expectEqual(k, 2)
3234
c.x = 11
3335
expectEqual(c.x, 11)
36+
#endif
3437
k = c.mutMethod(-13)
3538
expectEqual(k, -13)
3639
}
@@ -56,6 +59,7 @@ MoveOnlyCxxValueType.test("Test move only field access in holder") {
5659
expectEqual(c.x.x, 5)
5760
}
5861

62+
#if HAS_NONCOPYABLE_GENERICS
5963
MoveOnlyCxxValueType.test("Test move only field access in derived holder") {
6064
var c = NonCopyableHolderDerivedDerived(-11)
6165
var k = borrowNC(c.x)
@@ -69,5 +73,6 @@ MoveOnlyCxxValueType.test("Test move only field access in derived holder") {
6973
c.x.mutMethod(5)
7074
expectEqual(c.x.x, 5)
7175
}
76+
#endif
7277

7378
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,7 +1,9 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6)
22
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift)
3+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
34
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O)
45
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O)
6+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
57
//
68
// REQUIRES: executable_test
79

@@ -88,6 +90,7 @@ MoveOnlyCxxOperators.test("testNonCopyableHolderValueMutDeref pointee value") {
8890
expectEqual(k.x, k2.x)
8991
}
9092

93+
#if HAS_NONCOPYABLE_GENERICS
9194
MoveOnlyCxxOperators.test("NonCopyableHolderConstDerefDerivedDerived pointee borrow") {
9295
let holder = NonCopyableHolderConstDerefDerivedDerived(11)
9396
var k = borrowNC(holder.pointee)
@@ -155,5 +158,6 @@ MoveOnlyCxxOperators.test("testNonCopyableHolderValueMutDerefDerivedDerived poin
155158
var k2 = holder.pointee
156159
expectEqual(k.x, k2.x)
157160
}
161+
#endif
158162

159163
runAllTests()

0 commit comments

Comments
 (0)