Skip to content

[cxx-interop] Prevent usage in Swift of C++ copy constructor with default args #79325

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
Mar 4, 2025
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: 9 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7851,6 +7851,14 @@ static bool isSufficientlyTrivial(const clang::CXXRecordDecl *decl) {
return true;
}

static bool hasNonFirstDefaultArg(const clang::CXXConstructorDecl *ctor) {
if (ctor->getNumParams() < 2)
return false;

auto lastParam = ctor->parameters().back();
return lastParam->hasDefaultArg();
}

/// Checks if a record provides the required value type lifetime operations
/// (copy and destroy).
static bool hasCopyTypeOperations(const clang::CXXRecordDecl *decl) {
Expand All @@ -7867,6 +7875,7 @@ static bool hasCopyTypeOperations(const clang::CXXRecordDecl *decl) {
// struct.
return llvm::any_of(decl->ctors(), [](clang::CXXConstructorDecl *ctor) {
return ctor->isCopyConstructor() && !ctor->isDeleted() &&
!hasNonFirstDefaultArg(ctor) &&
ctor->getAccess() == clang::AccessSpecifier::AS_public;
});
}
Expand Down
10 changes: 9 additions & 1 deletion lib/IRGen/GenStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,20 @@ namespace {
FixedTypeInfo, ClangFieldInfo> {
const clang::RecordDecl *ClangDecl;

bool hasNonFirstDefaultArg(const clang::CXXConstructorDecl *ctor) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: is there a way to deduplicate this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open a similar pr for move constructors soon. I'll see if I can fix this there.

if (ctor->getNumParams() < 2)
return false;

auto lastParam = ctor->parameters().back();
return lastParam->hasDefaultArg();
}

const clang::CXXConstructorDecl *findCopyConstructor() const {
const auto *cxxRecordDecl = dyn_cast<clang::CXXRecordDecl>(ClangDecl);
if (!cxxRecordDecl)
return nullptr;
for (auto ctor : cxxRecordDecl->ctors()) {
if (ctor->isCopyConstructor() &&
if (ctor->isCopyConstructor() && !hasNonFirstDefaultArg(ctor) &&
ctor->getAccess() == clang::AS_public && !ctor->isDeleted())
return ctor;
}
Expand Down
4 changes: 0 additions & 4 deletions lib/SILOptimizer/Transforms/TempRValueElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,6 @@ void TempRValueOptPass::tryOptimizeCopyIntoTemp(CopyAddrInst *copyInst) {
// This copy_addr [take] will perform the final deinitialization.
return false;
}
assert(!tempObj->getType().isMoveOnly() &&
"introducing copy of move-only value!?");
return true;
}
if (auto *li = dyn_cast<LoadInst>(lastLoadInst)) {
Expand All @@ -650,8 +648,6 @@ void TempRValueOptPass::tryOptimizeCopyIntoTemp(CopyAddrInst *copyInst) {
// This load [take] will perform the final deinitialization.
return false;
}
assert(!tempObj->getType().isMoveOnly() &&
"introducing copy of move-only value!?");
return true;
}
return true;
Expand Down
12 changes: 12 additions & 0 deletions test/Interop/Cxx/class/Inputs/type-classification.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,16 @@ struct __attribute__((swift_attr("~Copyable"))) StructCopyableMovableAnnotatedNo
~StructCopyableMovableAnnotatedNonCopyable() = default;
};

struct HasCopyConstructorWithDefaultArgs {
int value;
HasCopyConstructorWithDefaultArgs(int value) : value(value) {}

HasCopyConstructorWithDefaultArgs(
const HasCopyConstructorWithDefaultArgs &other, int value = 1)
: value(other.value + value) {}

HasCopyConstructorWithDefaultArgs(HasCopyConstructorWithDefaultArgs &&) =
default;
};

#endif // TEST_INTEROP_CXX_CLASS_INPUTS_TYPE_CLASSIFICATION_H
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func testAnnotated() {
_ = v
}

func testNonCopyable() {
let x = HasCopyConstructorWithDefaultArgs(5)
let v = copy x // expected-error {{'copy' cannot be applied to noncopyable types}}
_ = v
}

test()
testField()
testAnnotated()
testNonCopyable()
24 changes: 24 additions & 0 deletions test/Interop/Cxx/value-witness-table/Inputs/copy-constructors.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ struct HasNonTrivialDefaultCopyConstructor {
const HasNonTrivialDefaultCopyConstructor &) = default;
};

struct HasCopyConstructorWithDefaultArgs {
int value;
HasCopyConstructorWithDefaultArgs(int value) : value(value) {}

HasCopyConstructorWithDefaultArgs(
const HasCopyConstructorWithDefaultArgs &other, int value = 1)
: value(other.value + value) {}

HasCopyConstructorWithDefaultArgs(HasCopyConstructorWithDefaultArgs &&) =
default;
};

struct HasCopyConstructorWithOneParameterWithDefaultArg {
int numCopies;

HasCopyConstructorWithOneParameterWithDefaultArg(int numCopies)
: numCopies(numCopies) {}

HasCopyConstructorWithOneParameterWithDefaultArg(
const HasCopyConstructorWithOneParameterWithDefaultArg &other =
HasCopyConstructorWithOneParameterWithDefaultArg{1})
: numCopies(other.numCopies + 1) {}
};

// Make sure that we don't crash on struct templates with copy-constructors.
template <typename T> struct S {
S(S const &) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ CXXCopyConstructorTestSuite.test("Default copy constructor, member with user-def
expectTrue(result.0.box.numCopies + result.1.box.numCopies > 0)
}

CXXCopyConstructorTestSuite.test("Copy constructor with default arguments") {
// When in the presence of a C++ copy constructor with default args, we make the type non-copyable
let originalObj = HasCopyConstructorWithDefaultArgs(5)
expectEqual(originalObj.value, 5)

// move originalObj
let newObj = originalObj
expectEqual(newObj.value, 5)
}

CXXCopyConstructorTestSuite.test("Copy constructor with one parameter that has a default argument") {
// If the C++ copy constructor has exactly one param and it has a default argument, ignore the default argument and import the type as copyable to Swift

let original = HasCopyConstructorWithOneParameterWithDefaultArg(1)
let copy = original
expectTrue(original.numCopies + copy.numCopies > 1)
}

runAllTests()