Skip to content

[cxx-interop] Treat un-instantiated templated types as unsafe #64897

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
Apr 4, 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
9 changes: 6 additions & 3 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6395,7 +6395,9 @@ static bool hasIteratorAPIAttr(const clang::Decl *decl) {
static bool hasPointerInSubobjects(const clang::CXXRecordDecl *decl) {
// Probably a class template that has not yet been specialized:
if (!decl->getDefinition())
return false;
// If the definition is unknown, there is no way to determine if the type
// stores pointers. Stay on the safe side and assume that it does.
return true;

auto checkType = [](clang::QualType t) {
if (t->isPointerType())
Expand Down Expand Up @@ -6690,9 +6692,10 @@ bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator,
return false;
}

// Mark this as safe to help our diganostics down the road.
if (!cxxRecordReturnType->getDefinition()) {
return true;
// This is a templated type that has not been instantiated yet. We do
// not know if it is safe. Assume that it isn't.
return false;
}

if (!cxxRecordReturnType->hasUserDeclaredCopyConstructor() &&
Expand Down
28 changes: 28 additions & 0 deletions test/Interop/Cxx/class/Inputs/type-classification.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,32 @@ struct HasMethodThatReturnsIteratorBox {
IteratorBox getIteratorBox() const;
};

template <typename T>
struct TemplatedPointerBox {
T *ptr;
};

struct HasMethodThatReturnsTemplatedPointerBox {
TemplatedPointerBox<int> getTemplatedPointerBox() const;
};

template <typename T>
struct TemplatedBox {
T value;
};

struct HasMethodThatReturnsTemplatedBox {
TemplatedBox<int> getIntBox() const;
TemplatedBox<int *> getIntPtrBox() const;
};

template <typename T>
struct __attribute__((swift_attr("import_iterator"))) TemplatedIterator {
T idx;
};

struct HasMethodThatReturnsTemplatedIterator {
TemplatedIterator<int *> getIterator() const;
};

#endif // TEST_INTEROP_CXX_CLASS_INPUTS_TYPE_CLASSIFICATION_H
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@
// CHECK: func __getIteratorBoxUnsafe() -> IteratorBox
// CHECK-SKIP-UNSAFE-NOT: func __getIteratorBoxUnsafe() -> IteratorBox
// CHECK: }

// CHECK: struct HasMethodThatReturnsTemplatedPointerBox {
// CHECK: func __getTemplatedPointerBoxUnsafe() -> TemplatedPointerBox<Int32>
// CHECK-SKIP-UNSAFE-NOT: func __getTemplatedPointerBoxUnsafe() -> TemplatedPointerBox<Int32>
// CHECK: }

// CHECK: struct HasMethodThatReturnsTemplatedBox {
// FIXME: This is unfortunate, we should be able to recognize that TemplatedBox<Int32> does not store any pointers as fields.
// CHECK: func __getIntBoxUnsafe() -> TemplatedBox<Int32>
// CHECK: func __getIntPtrBoxUnsafe()
// CHECK: }

// CHECK: struct HasMethodThatReturnsTemplatedIterator {
// CHECK: func __getIteratorUnsafe()
// CHECK: }
22 changes: 11 additions & 11 deletions test/Interop/Cxx/templates/Inputs/large-class-templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ struct ValExpr {
using type = typename E::type;
E expr;

ValExpr<SliceExpr<E, 1>> test1() { return {SliceExpr<E, 1>{expr}}; }
ValExpr<SliceExpr<E, 2>> test2() { return {SliceExpr<E, 2>{expr}}; }
ValExpr<SliceExpr<E, 3>> test3() { return {SliceExpr<E, 3>{expr}}; }
ValExpr<SliceExpr<E, 4>> test4() { return {SliceExpr<E, 4>{expr}}; }
ValExpr<SliceExpr<E, 5>> test5() { return {SliceExpr<E, 5>{expr}}; }
ValExpr<SliceExpr<E, 6>> test6() { return {SliceExpr<E, 6>{expr}}; }
ValExpr<SliceExpr<E, 7>> test7() { return {SliceExpr<E, 7>{expr}}; }
ValExpr<SliceExpr<E, 8>> test8() { return {SliceExpr<E, 8>{expr}}; }
ValExpr<SliceExpr<E, 9>> test9() { return {SliceExpr<E, 8>{expr}}; }
ValExpr<SliceExpr<E, 11>> test11() { return {SliceExpr<E, 11>{expr}}; }
ValExpr<SliceExpr<E, 12>> test12() { return {SliceExpr<E, 12>{expr}}; }
ValExpr<SliceExpr<E, 1>> test1() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 1>{expr}}; }
ValExpr<SliceExpr<E, 2>> test2() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 2>{expr}}; }
ValExpr<SliceExpr<E, 3>> test3() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 3>{expr}}; }
ValExpr<SliceExpr<E, 4>> test4() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 4>{expr}}; }
ValExpr<SliceExpr<E, 5>> test5() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 5>{expr}}; }
ValExpr<SliceExpr<E, 6>> test6() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 6>{expr}}; }
ValExpr<SliceExpr<E, 7>> test7() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 7>{expr}}; }
ValExpr<SliceExpr<E, 8>> test8() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 8>{expr}}; }
ValExpr<SliceExpr<E, 9>> test9() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 8>{expr}}; }
ValExpr<SliceExpr<E, 11>> test11() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 11>{expr}}; }
ValExpr<SliceExpr<E, 12>> test12() __attribute__((swift_attr("import_unsafe"))) { return {SliceExpr<E, 12>{expr}}; }
};

// This class template is exponentially slow to *fully* instantiate (and the
Expand Down