Skip to content

[cxx-interop] Tweak C++ type semantics detection #67115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions include/swift/ClangImporter/ClangImporterRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,8 @@ enum class CxxRecordSemanticsKind {
MoveOnly,
Reference,
Iterator,
// An API that has be annotated as explicitly unsafe, but still importable.
// TODO: we should rename these APIs.
ExplicitlyUnsafe,
// A record that is either not copyable or not destructible.
MissingLifetimeOperation,
// A record that contains a pointer (aka non-trivial type).
UnsafePointerMember,
// A C++ record that represents a Swift class type exposed to C++ from Swift.
SwiftClassType
};
Expand Down
9 changes: 0 additions & 9 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6712,10 +6712,6 @@ CxxRecordSemantics::evaluate(Evaluator &evaluator,
return CxxRecordSemanticsKind::MissingLifetimeOperation;
}

if (hasUnsafeAPIAttr(cxxDecl)) {
return CxxRecordSemanticsKind::ExplicitlyUnsafe;
}

if (hasOwnedValueAttr(cxxDecl)) {
return CxxRecordSemanticsKind::Owned;
}
Expand All @@ -6724,11 +6720,6 @@ CxxRecordSemantics::evaluate(Evaluator &evaluator,
return CxxRecordSemanticsKind::Iterator;
}

if (!hasCustomCopyOrMoveConstructor(cxxDecl) &&
hasPointerInSubobjects(cxxDecl)) {
return CxxRecordSemanticsKind::UnsafePointerMember;
}

if (hasCopyTypeOperations(cxxDecl)) {
return CxxRecordSemanticsKind::Owned;
}
Expand Down
3 changes: 0 additions & 3 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4073,9 +4073,6 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
name.getBaseIdentifier().str());
ctx.Diags.diagnose(loc, diag::iterator_potentially_unsafe);
} else {
assert(methodSemantics ==
CxxRecordSemanticsKind::UnsafePointerMember);

auto baseSwiftLoc = ctx.getClangModuleLoader()->importSourceLocation(
cxxRecord->getLocation());

Expand Down
30 changes: 30 additions & 0 deletions test/Interop/Cxx/class/Inputs/type-classification.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ struct StructWithSubobjectPrivateDefaultedDestructor {
StructWithPrivateDefaultedDestructor subobject;
};

struct StructWithDeletedCopyConstructor {
StructWithDeletedCopyConstructor(
const StructWithDeletedCopyConstructor &other) = delete;
};

struct StructWithMoveConstructorAndDeletedCopyConstructor {
StructWithMoveConstructorAndDeletedCopyConstructor() {}
StructWithMoveConstructorAndDeletedCopyConstructor(
const StructWithMoveConstructorAndDeletedCopyConstructor &other) = delete;
StructWithMoveConstructorAndDeletedCopyConstructor(
StructWithMoveConstructorAndDeletedCopyConstructor &&other) {}
~StructWithMoveConstructorAndDeletedCopyConstructor(){};
};

struct StructWithDeletedDestructor {
~StructWithDeletedDestructor() = delete;
};
Expand Down Expand Up @@ -148,6 +162,22 @@ struct StructNonCopyableTriviallyMovable {
~StructNonCopyableTriviallyMovable() = default;
};

/// Similar to std::unique_ptr
struct StructWithPointerNonCopyableTriviallyMovable {
int *ptr = nullptr;

StructWithPointerNonCopyableTriviallyMovable() = default;
StructWithPointerNonCopyableTriviallyMovable(
const StructWithPointerNonCopyableTriviallyMovable &other) = delete;
StructWithPointerNonCopyableTriviallyMovable(
StructWithPointerNonCopyableTriviallyMovable &&other) = default;
~StructWithPointerNonCopyableTriviallyMovable() = default;
};

struct StructWithPointerNonCopyableTriviallyMovableField {
StructWithPointerNonCopyableTriviallyMovable p = {};
};

struct StructNonCopyableNonMovable {
StructNonCopyableNonMovable(const StructNonCopyableNonMovable &) = delete;
StructNonCopyableNonMovable(StructNonCopyableNonMovable &&) = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// CHECK-NOT: StructWithInheritedPrivateDefaultedCopyConstructor
// CHECK-NOT: StructWithSubobjectPrivateDefaultedCopyConstructor
// CHECK-NOT: StructNonCopyableTriviallyMovable
// CHECK-NOT: StructWithPointerNonCopyableTriviallyMovable
// CHECK-NOT: StructWithPointerNonCopyableTriviallyMovableField
// CHECK-NOT: StructNonCopyableNonMovable
// CHECK-NOT: StructWithMoveConstructor
// CHECK-NOT: StructWithInheritedMoveConstructor
Expand All @@ -16,6 +18,8 @@
// CHECK-NOT: StructWithPrivateDefaultedDestructor
// CHECK-NOT: StructWithInheritedPrivateDefaultedDestructor
// CHECK-NOT: StructWithSubobjectPrivateDefaultedDestructor
// CHECK-NOT: StructWithDeletedCopyConstructor
// CHECK-NOT: StructWithMoveConstructorAndDeletedCopyConstructor
// CHECK-NOT: StructWithDeletedDestructor
// CHECK-NOT: StructWithInheritedDeletedDestructor
// CHECK-NOT: StructWithSubobjectDeletedDestructor
Expand Down