Skip to content

[cxx-interop] Do not treat std::pair<UnsafeType, T> as safe #66169

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
Jun 1, 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
17 changes: 13 additions & 4 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6574,6 +6574,17 @@ static bool hasDestroyTypeOperations(const clang::CXXRecordDecl *decl) {
return false;
}

static bool hasCustomCopyOrMoveConstructor(const clang::CXXRecordDecl *decl) {
// std::pair and std::tuple might have copy and move constructors, but that
// doesn't mean they are safe to use from Swift, e.g. std::pair<UnsafeType, T>
if (decl->isInStdNamespace() &&
(decl->getName() == "pair" || decl->getName() == "tuple")) {
return false;
}
return decl->hasUserDeclaredCopyConstructor() ||
decl->hasUserDeclaredMoveConstructor();
}

static bool isSwiftClassType(const clang::CXXRecordDecl *decl) {
// Swift type must be annotated with external_source_symbol attribute.
auto essAttr = decl->getAttr<clang::ExternalSourceSymbolAttr>();
Expand Down Expand Up @@ -6641,8 +6652,7 @@ CxxRecordSemantics::evaluate(Evaluator &evaluator,
return CxxRecordSemanticsKind::Iterator;
}

if (!cxxDecl->hasUserDeclaredCopyConstructor() &&
!cxxDecl->hasUserDeclaredMoveConstructor() &&
if (!hasCustomCopyOrMoveConstructor(cxxDecl) &&
hasPointerInSubobjects(cxxDecl)) {
return CxxRecordSemanticsKind::UnsafePointerMember;
}
Expand Down Expand Up @@ -6730,8 +6740,7 @@ bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator,
return true;
}

if (!cxxRecordReturnType->hasUserDeclaredCopyConstructor() &&
!cxxRecordReturnType->hasUserDeclaredMoveConstructor() &&
if (!hasCustomCopyOrMoveConstructor(cxxRecordReturnType) &&
!hasOwnedValueAttr(cxxRecordReturnType) &&
hasPointerInSubobjects(cxxRecordReturnType)) {
return false;
Expand Down
14 changes: 14 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ using PairStructInt = std::pair<StructInPair, int>;
inline PairStructInt getPairStructInt(int x) {
return { { x * 2, -x}, x };
}

struct UnsafeStruct {
int *ptr;
};

struct __attribute__((swift_attr("import_iterator"))) Iterator {};

using PairUnsafeStructInt = std::pair<UnsafeStruct, int>;
using PairIteratorInt = std::pair<Iterator, int>;

struct HasMethodThatReturnsUnsafePair {
PairUnsafeStructInt getUnsafePair() const { return {}; }
PairIteratorInt getIteratorPair() const { return {}; }
};
8 changes: 8 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-pair-typechecker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop
// REQUIRES: OS=macosx || OS=linux-gnu

import StdPair

let u = HasMethodThatReturnsUnsafePair()
u.getUnsafePair() // expected-error {{value of type 'HasMethodThatReturnsUnsafePair' has no member 'getUnsafePair'}}
u.getIteratorPair() // expected-error {{value of type 'HasMethodThatReturnsUnsafePair' has no member 'getIteratorPair'}}