Skip to content

Commit 3bb5ebc

Browse files
royjacobsontstellar
authored andcommitted
[Clang] Adjust triviality computation in QualType::isTrivialType to C++20 cases.
Up to C++20, hasDefaultConstructor and !hasNonTrivialDefaultConstructor together implied hasTrivialDefaultConstructor. In C++20, a constructor might be ineligible and can set hasDefaultConstructor without setting hasNonTrivialDefaultConstructor. Fix this by querying hasTrivialDefaultConstructor instead of hasDefaultConstructor. I'd like to backport this to Clang 16. I only change isTrivialType and in a way that should only affect code that uses constrained constructors, so I think this is relatively safe to backport. Fixes #60697 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D143891
1 parent 4b4dfa3 commit 3bb5ebc

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

clang/lib/AST/Type.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,11 +2487,13 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
24872487
return true;
24882488
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
24892489
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2490-
// C++11 [class]p6:
2491-
// A trivial class is a class that has a default constructor,
2492-
// has no non-trivial default constructors, and is trivially
2493-
// copyable.
2494-
return ClassDecl->hasDefaultConstructor() &&
2490+
// C++20 [class]p6:
2491+
// A trivial class is a class that is trivially copyable, and
2492+
// has one or more eligible default constructors such that each is
2493+
// trivial.
2494+
// FIXME: We should merge this definition of triviality into
2495+
// CXXRecordDecl::isTrivial. Currently it computes the wrong thing.
2496+
return ClassDecl->hasTrivialDefaultConstructor() &&
24952497
!ClassDecl->hasNonTrivialDefaultConstructor() &&
24962498
ClassDecl->isTriviallyCopyable();
24972499
}

clang/test/SemaCXX/constrained-special-member-functions.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,37 @@ static_assert(__is_trivially_constructible(C), "");
265265
static_assert(__is_trivially_constructible(D), "");
266266

267267
}
268+
269+
namespace GH60697 {
270+
271+
template <class T>
272+
struct X {
273+
X() requires false = default;
274+
};
275+
static_assert(!__is_trivial(X<int>));
276+
277+
template <class T>
278+
struct S {
279+
S() requires(__is_trivially_constructible(T)) = default;
280+
281+
S() requires(!__is_trivially_constructible(T) &&
282+
__is_constructible(T)) {}
283+
284+
T t;
285+
};
286+
287+
struct D {
288+
D(int i) : i(i) {}
289+
int i;
290+
};
291+
static_assert(!__is_trivially_constructible(D));
292+
static_assert(!__is_constructible(D));
293+
static_assert(!__is_trivial(D));
294+
295+
static_assert(!__is_trivially_constructible(S<D>));
296+
static_assert(!__is_constructible(S<D>));
297+
298+
static_assert(__is_trivial(S<int>));
299+
static_assert(!__is_trivial(S<D>));
300+
301+
}

0 commit comments

Comments
 (0)