Skip to content

Commit

Permalink
[Clang] Don't consider default constructors ineligible if the more co…
Browse files Browse the repository at this point in the history
…nstrained constructor is a template

Partially solves #59206:

We now mark trivial constructors as eligible even if there's a more constrained templated default constructor. Although technically non-conformant, this solves problems with pretty reasonable uses cases like
```
template<int n>
struct Foo {
	constexpr Foo() = default;

	template<class... Ts>
	Foo(Ts... vals) requires(sizeof...(Ts) == n) {}
};
```
where we currently consider the default constructor to be ineligible and therefor inheriting/containing classes have non trivial constructors. This is aligned with GCC: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=c75ebe76ae12ac4020f20a24f34606a594a40d15

This doesn't change `__is_trivial`. Although we're technically standard conformant in this regard, GCC/MSVC exhibit different behaviors that seem to make more sense. An issue has been filed to CWG and we await their response.

Reviewed By: erichkeane, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D139038
  • Loading branch information
royjacobson committed Dec 5, 2022
1 parent 7fe4abb commit 7d58c95
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 5 additions & 1 deletion clang/lib/AST/DeclCXX.cpp
Expand Up @@ -1378,7 +1378,11 @@ void CXXRecordDecl::addedSelectedDestructor(CXXDestructorDecl *DD) {
}

void CXXRecordDecl::addedEligibleSpecialMemberFunction(const CXXMethodDecl *MD,
unsigned SMKind) {
unsigned SMKind) {
// FIXME: We shouldn't change DeclaredNonTrivialSpecialMembers if `MD` is
// a function template, but this needs CWG attention before we break ABI.
// See https://github.com/llvm/llvm-project/issues/59206

if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
if (DD->isUserProvided())
data().HasIrrelevantDestructor = false;
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Sema/SemaDecl.cpp
Expand Up @@ -18193,8 +18193,11 @@ static bool AreSpecialMemberFunctionsSameKind(ASTContext &Context,
CXXMethodDecl *M1,
CXXMethodDecl *M2,
Sema::CXXSpecialMember CSM) {
// We don't want to compare templates to non-templates: See
// https://github.com/llvm/llvm-project/issues/59206
if (CSM == Sema::CXXDefaultConstructor)
return true;
return bool(M1->getDescribedFunctionTemplate()) ==
bool(M2->getDescribedFunctionTemplate());
if (!Context.hasSameType(M1->getParamDecl(0)->getType(),
M2->getParamDecl(0)->getType()))
return false;
Expand Down
40 changes: 40 additions & 0 deletions clang/test/SemaCXX/constrained-special-member-functions.cpp
Expand Up @@ -225,3 +225,43 @@ void func() {
S<2, 2> s2;
}
}

namespace GH59206 {

struct A {
A() = default; //eligible, second constructor unsatisfied
template<class... Args>
A(Args&&... args) requires (sizeof...(Args) > 0) {}
};

struct B {
B() = default; //ineligible, second constructor more constrained
template<class... Args>
B(Args&&... args) requires (sizeof...(Args) == 0) {}
};

struct C {
C() = default; //eligible, but
template<class... Args> //also eligible and non-trivial
C(Args&&... args) {}
};

struct D : B {};

static_assert(__is_trivially_copyable(A), "");
static_assert(__is_trivially_copyable(B), "");
static_assert(__is_trivially_copyable(C), "");
static_assert(__is_trivially_copyable(D), "");

// FIXME: Update when https://github.com/llvm/llvm-project/issues/59206 is
// resolved.
static_assert(!__is_trivial(A), "");
static_assert(!__is_trivial(B), "");
static_assert(!__is_trivial(C), "");
static_assert(__is_trivial(D), "");
static_assert(__is_trivially_constructible(A), "");
static_assert(__is_trivially_constructible(B), "");
static_assert(__is_trivially_constructible(C), "");
static_assert(__is_trivially_constructible(D), "");

}

0 comments on commit 7d58c95

Please sign in to comment.