Skip to content

Commit 7fcdee6

Browse files
committed
[cxx-interop] Tweak C++ type semantics detection
`CxxRecordSemanticsKind::ExplicitlyUnsafe` and `CxxRecordSemanticsKind::UnsafePointerMember` were never directly used, and those do not indicate semantics: they indicate safety of the type when used from Swift, which should be handled by another request `IsSafeUseOfCxxDecl` instead of `CxxRecordSemantics`. Having `ExplicitlyUnsafe` and `UnsafePointerMember` as semantics indicators was problematic, for instance, for types that are move-only and store a pointer at the same time. Swift allowed the usage of these types (under the rules for `UnsafePointerMember` types) when move-only types are disabled, and did not apply the move-only attribute on such types when move-only types are enabled. rdar://110644300 (cherry picked from commit 6e7fb32)
1 parent 6f1cf76 commit 7fcdee6

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

include/swift/ClangImporter/ClangImporterRequests.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,8 @@ enum class CxxRecordSemanticsKind {
306306
MoveOnly,
307307
Reference,
308308
Iterator,
309-
// An API that has be annotated as explicitly unsafe, but still importable.
310-
// TODO: we should rename these APIs.
311-
ExplicitlyUnsafe,
312309
// A record that is either not copyable or not destructible.
313310
MissingLifetimeOperation,
314-
// A record that contains a pointer (aka non-trivial type).
315-
UnsafePointerMember,
316311
// A C++ record that represents a Swift class type exposed to C++ from Swift.
317312
SwiftClassType
318313
};

lib/ClangImporter/ClangImporter.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6665,10 +6665,6 @@ CxxRecordSemantics::evaluate(Evaluator &evaluator,
66656665
return CxxRecordSemanticsKind::MissingLifetimeOperation;
66666666
}
66676667

6668-
if (hasUnsafeAPIAttr(cxxDecl)) {
6669-
return CxxRecordSemanticsKind::ExplicitlyUnsafe;
6670-
}
6671-
66726668
if (hasOwnedValueAttr(cxxDecl)) {
66736669
return CxxRecordSemanticsKind::Owned;
66746670
}
@@ -6677,11 +6673,6 @@ CxxRecordSemantics::evaluate(Evaluator &evaluator,
66776673
return CxxRecordSemanticsKind::Iterator;
66786674
}
66796675

6680-
if (!hasCustomCopyOrMoveConstructor(cxxDecl) &&
6681-
hasPointerInSubobjects(cxxDecl)) {
6682-
return CxxRecordSemanticsKind::UnsafePointerMember;
6683-
}
6684-
66856676
if (hasCopyTypeOperations(cxxDecl)) {
66866677
return CxxRecordSemanticsKind::Owned;
66876678
}

lib/Sema/CSDiagnostics.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,9 +4012,6 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
40124012
name.getBaseIdentifier().str());
40134013
ctx.Diags.diagnose(loc, diag::iterator_potentially_unsafe);
40144014
} else {
4015-
assert(methodSemantics ==
4016-
CxxRecordSemanticsKind::UnsafePointerMember);
4017-
40184015
auto baseSwiftLoc = ctx.getClangModuleLoader()->importSourceLocation(
40194016
cxxRecord->getLocation());
40204017

test/Interop/Cxx/class/Inputs/type-classification.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ struct StructWithSubobjectPrivateDefaultedDestructor {
112112
StructWithPrivateDefaultedDestructor subobject;
113113
};
114114

115+
struct StructWithDeletedCopyConstructor {
116+
StructWithDeletedCopyConstructor(
117+
const StructWithDeletedCopyConstructor &other) = delete;
118+
};
119+
120+
struct StructWithMoveConstructorAndDeletedCopyConstructor {
121+
StructWithMoveConstructorAndDeletedCopyConstructor() {}
122+
StructWithMoveConstructorAndDeletedCopyConstructor(
123+
const StructWithMoveConstructorAndDeletedCopyConstructor &other) = delete;
124+
StructWithMoveConstructorAndDeletedCopyConstructor(
125+
StructWithMoveConstructorAndDeletedCopyConstructor &&other) {}
126+
~StructWithMoveConstructorAndDeletedCopyConstructor(){};
127+
};
128+
115129
struct StructWithDeletedDestructor {
116130
~StructWithDeletedDestructor() = delete;
117131
};
@@ -148,6 +162,22 @@ struct StructNonCopyableTriviallyMovable {
148162
~StructNonCopyableTriviallyMovable() = default;
149163
};
150164

165+
/// Similar to std::unique_ptr
166+
struct StructWithPointerNonCopyableTriviallyMovable {
167+
int *ptr = nullptr;
168+
169+
StructWithPointerNonCopyableTriviallyMovable() = default;
170+
StructWithPointerNonCopyableTriviallyMovable(
171+
const StructWithPointerNonCopyableTriviallyMovable &other) = delete;
172+
StructWithPointerNonCopyableTriviallyMovable(
173+
StructWithPointerNonCopyableTriviallyMovable &&other) = default;
174+
~StructWithPointerNonCopyableTriviallyMovable() = default;
175+
};
176+
177+
struct StructWithPointerNonCopyableTriviallyMovableField {
178+
StructWithPointerNonCopyableTriviallyMovable p = {};
179+
};
180+
151181
struct StructNonCopyableNonMovable {
152182
StructNonCopyableNonMovable(const StructNonCopyableNonMovable &) = delete;
153183
StructNonCopyableNonMovable(StructNonCopyableNonMovable &&) = default;

test/Interop/Cxx/class/type-classification-module-interface.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// CHECK-NOT: StructWithInheritedPrivateDefaultedCopyConstructor
77
// CHECK-NOT: StructWithSubobjectPrivateDefaultedCopyConstructor
88
// CHECK-NOT: StructNonCopyableTriviallyMovable
9+
// CHECK-NOT: StructWithPointerNonCopyableTriviallyMovable
10+
// CHECK-NOT: StructWithPointerNonCopyableTriviallyMovableField
911
// CHECK-NOT: StructNonCopyableNonMovable
1012
// CHECK-NOT: StructWithMoveConstructor
1113
// CHECK-NOT: StructWithInheritedMoveConstructor
@@ -16,6 +18,8 @@
1618
// CHECK-NOT: StructWithPrivateDefaultedDestructor
1719
// CHECK-NOT: StructWithInheritedPrivateDefaultedDestructor
1820
// CHECK-NOT: StructWithSubobjectPrivateDefaultedDestructor
21+
// CHECK-NOT: StructWithDeletedCopyConstructor
22+
// CHECK-NOT: StructWithMoveConstructorAndDeletedCopyConstructor
1923
// CHECK-NOT: StructWithDeletedDestructor
2024
// CHECK-NOT: StructWithInheritedDeletedDestructor
2125
// CHECK-NOT: StructWithSubobjectDeletedDestructor

0 commit comments

Comments
 (0)