-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Only mark projections of self-contained types as unsafe. #67296
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
zoecarver
merged 10 commits into
swiftlang:main
from
zoecarver:only-methods-on-self-contained-types-are-unsafe
Jul 19, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
474a7cd
[cxx-interop] Only mark projections of self-contained types as unsafe.
zoecarver 7db669c
[cxx-interop] Fix a bug with explicit, nested, self-contained types (…
zoecarver 2115f7a
[cxx-interop] Add `NewCxxMethodSafetyHeuristics` feature and guard sw…
zoecarver 8677fab
[cxx-interop] Add legacy implementation of is-safe-use-of-cxx-method.
zoecarver 782641a
[cxx-interop] ExpirementalFeature -> LanguageFeature.
zoecarver a6a0f63
[nfc][cxx-interop] Fix a few tests.
zoecarver fd7b39e
[cxx-interop] Mark *all* iterators as unsafe; update tests accordingl…
zoecarver ffc57bf
[cxx-interop][nfc] Fix last straggling test.
zoecarver 806956d
[cxx-interop] always mark begin and end methods as unsafe (to help au…
zoecarver e670a0e
[cxx-interop][nfc] post-rebase fallout for a few tests.
zoecarver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6789,6 +6789,37 @@ CxxRecordAsSwiftType::evaluate(Evaluator &evaluator, | |
return nullptr; | ||
} | ||
|
||
bool anySubobjectsSelfContained(const clang::CXXRecordDecl *decl) { | ||
if (!decl->getDefinition()) | ||
return false; | ||
|
||
if (hasCustomCopyOrMoveConstructor(decl) || hasOwnedValueAttr(decl)) | ||
return true; | ||
|
||
auto checkType = [](clang::QualType t) { | ||
if (auto recordType = dyn_cast<clang::RecordType>(t.getCanonicalType())) { | ||
if (auto cxxRecord = | ||
dyn_cast<clang::CXXRecordDecl>(recordType->getDecl())) { | ||
return anySubobjectsSelfContained(cxxRecord); | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
for (auto field : decl->fields()) { | ||
if (checkType(field->getType())) | ||
return true; | ||
} | ||
|
||
for (auto base : decl->bases()) { | ||
if (checkType(base.getType())) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator, | ||
SafeUseOfCxxDeclDescriptor desc) const { | ||
const clang::Decl *decl = desc.decl; | ||
|
@@ -6806,10 +6837,24 @@ bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator, | |
if (isForeignReferenceType(method->getReturnType())) | ||
return true; | ||
|
||
// If it returns a pointer or reference, that's a projection. | ||
// begin and end methods likely return an interator, so they're unsafe. This | ||
// is required so that automatic the conformance to RAC works properly. | ||
if (method->getNameAsString() == "begin" || | ||
method->getNameAsString() == "end") | ||
return false; | ||
|
||
auto parentQualType = method | ||
->getParent()->getTypeForDecl()->getCanonicalTypeUnqualified(); | ||
|
||
bool parentIsSelfContained = | ||
!isForeignReferenceType(parentQualType) && | ||
anySubobjectsSelfContained(method->getParent()); | ||
|
||
// If it returns a pointer or reference from an owned parent, that's a | ||
// projection (unsafe). | ||
if (method->getReturnType()->isPointerType() || | ||
method->getReturnType()->isReferenceType()) | ||
return false; | ||
return !parentIsSelfContained; | ||
|
||
// Check if it's one of the known unsafe methods we currently | ||
// mark as safe by default. | ||
|
@@ -6824,20 +6869,22 @@ bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator, | |
dyn_cast<clang::CXXRecordDecl>(returnType->getDecl())) { | ||
if (isSwiftClassType(cxxRecordReturnType)) | ||
return true; | ||
|
||
if (hasIteratorAPIAttr(cxxRecordReturnType) || | ||
isIterator(cxxRecordReturnType)) { | ||
isIterator(cxxRecordReturnType)) | ||
return false; | ||
} | ||
|
||
// Mark this as safe to help our diganostics down the road. | ||
if (!cxxRecordReturnType->getDefinition()) { | ||
return true; | ||
} | ||
|
||
if (!hasCustomCopyOrMoveConstructor(cxxRecordReturnType) && | ||
// A projection of a view type (such as a string_view) from a self | ||
// contained parent is a proejction (unsafe). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: projection |
||
if (!anySubobjectsSelfContained(cxxRecordReturnType) && | ||
!hasOwnedValueAttr(cxxRecordReturnType) && | ||
hasPointerInSubobjects(cxxRecordReturnType)) { | ||
return false; | ||
return !parentIsSelfContained; | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ module Test { | |
struct Ptr { int *p; }; | ||
|
||
struct X { | ||
X(const X&); | ||
|
||
int *test() { } | ||
Ptr other() { } | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#ifndef TEST_INTEROP_CXX_CLASS_METHOD_UNSAFE_PROJECTIONS_H | ||
#define TEST_INTEROP_CXX_CLASS_METHOD_UNSAFE_PROJECTIONS_H | ||
|
||
#include <string> | ||
|
||
struct NestedSelfContained; | ||
struct Empty; | ||
struct SelfContained; | ||
struct ExplicitSelfContained; | ||
struct NestedExplicitSelfContained; | ||
|
||
struct View { | ||
void *ptr; | ||
|
||
void *data() const; | ||
void *empty() const; | ||
std::string name() const; | ||
NestedSelfContained nested() const; | ||
ExplicitSelfContained explicitSelfContained() const; | ||
NestedExplicitSelfContained explicitNested() const; | ||
}; | ||
|
||
struct SelfContained { | ||
void *ptr; | ||
SelfContained(const SelfContained&); | ||
|
||
std::string name() const; | ||
SelfContained selfContained() const; | ||
NestedSelfContained nested() const; | ||
Empty empty() const; | ||
int value() const; | ||
View view() const; | ||
int *pointer() const; | ||
ExplicitSelfContained explicitSelfContained() const; | ||
NestedExplicitSelfContained explicitNested() const; | ||
}; | ||
|
||
struct NestedSelfContained { | ||
SelfContained member; | ||
|
||
std::string name() const; | ||
SelfContained selfContained() const; | ||
NestedSelfContained nested() const; | ||
Empty empty() const; | ||
int value() const; | ||
View view() const; | ||
int *pointer() const; | ||
ExplicitSelfContained explicitSelfContained() const; | ||
NestedExplicitSelfContained explicitNested() const; | ||
}; | ||
|
||
struct InheritSelfContained: SelfContained { | ||
std::string name() const; | ||
SelfContained selfContained() const; | ||
NestedSelfContained nested() const; | ||
Empty empty() const; | ||
int value() const; | ||
View view() const; | ||
int *pointer() const; | ||
}; | ||
|
||
struct __attribute__((swift_attr("import_owned"))) ExplicitSelfContained { | ||
void *ptr; | ||
|
||
void *pointer() const; | ||
View view() const; | ||
NestedSelfContained nested() const; | ||
}; | ||
|
||
struct NestedExplicitSelfContained { | ||
ExplicitSelfContained m; | ||
|
||
SelfContained selfContained() const; | ||
NestedSelfContained nested() const; | ||
int value() const; | ||
View view() const; | ||
int *pointer() const; | ||
}; | ||
|
||
struct Empty { | ||
Empty empty() const; | ||
void *pointer() const; | ||
SelfContained selfContained() const; | ||
}; | ||
|
||
struct IntPair { | ||
int a; int b; | ||
|
||
int first() const; | ||
void *pointer() const; | ||
SelfContained selfContained() const; | ||
}; | ||
|
||
#endif // TEST_INTEROP_CXX_CLASS_METHOD_UNSAFE_PROJECTIONS_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: iterator