| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s |
| 3 |
|
| 4 |
|
| 5 |
static_assert(__is_literal(int), "fail"); |
| 6 |
static_assert(__is_literal_type(int), "fail"); // alternate spelling for GCC |
| 7 |
static_assert(__is_literal(void*), "fail"); |
| 8 |
enum E { E1 }; |
| 9 |
static_assert(__is_literal(E), "fail"); |
| 10 |
static_assert(__is_literal(decltype(E1)), "fail"); |
| 11 |
typedef int IAR[10]; |
| 12 |
static_assert(__is_literal(IAR), "fail"); |
| 13 |
typedef int Vector __attribute__((vector_size(16))); |
| 14 |
typedef int VectorExt __attribute__((ext_vector_type(4))); |
| 15 |
static_assert(__is_literal(Vector), "fail"); |
| 16 |
static_assert(__is_literal(VectorExt), "fail"); |
| 17 |
|
| 18 |
// C++0x [basic.types]p10: |
| 19 |
// A type is a literal type if it is: |
| 20 |
// [...] |
| 21 |
// -- a class type that has all of the following properties: |
| 22 |
// -- it has a trivial destructor |
| 23 |
// [P0784R7 changed the condition to "constexpr destructor" in C++20] |
| 24 |
// -- every constructor call and full-expression in the |
| 25 |
// brace-or-equal-initializers for non-static data members (if an) is |
| 26 |
// a constant expression, |
| 27 |
// -- it is an aggregate type or has at least one constexpr constructor |
| 28 |
// or constructor template that is not a copy or move constructor, and |
| 29 |
// [DR1452 adds class types with trivial default constructors to |
| 30 |
// this list] |
| 31 |
// -- it has all non-static data members and base classes of literal |
| 32 |
// types |
| 33 |
struct Empty {}; |
| 34 |
struct LiteralType { |
| 35 |
int x; |
| 36 |
E e; |
| 37 |
IAR arr; |
| 38 |
Empty empty; |
| 39 |
int method(); |
| 40 |
}; |
| 41 |
struct HasDtor { ~HasDtor(); }; |
| 42 |
|
| 43 |
class NonAggregate { int x; }; |
| 44 |
struct NonLiteral { NonLiteral(); }; |
| 45 |
struct HasNonLiteralBase : NonLiteral {}; |
| 46 |
struct HasNonLiteralMember { HasDtor x; }; |
| 47 |
|
| 48 |
static_assert(__is_literal(Empty), "fail"); |
| 49 |
static_assert(__is_literal(LiteralType), "fail"); |
| 50 |
static_assert(__is_literal(NonAggregate), "fail"); |
| 51 |
static_assert(!__is_literal(NonLiteral), "fail"); |
| 52 |
static_assert(!__is_literal(HasDtor), "fail"); |
| 53 |
static_assert(!__is_literal(HasNonLiteralBase), "fail"); |
| 54 |
static_assert(!__is_literal(HasNonLiteralMember), "fail"); |
| 55 |
|
| 56 |
// DR1361 removes the brace-or-equal-initializer bullet so that we can allow: |
| 57 |
extern int f(); // expected-note {{here}} |
| 58 |
struct HasNonConstExprMemInit { |
| 59 |
int x = f(); // expected-note {{non-constexpr function}} |
| 60 |
constexpr HasNonConstExprMemInit() {} // expected-error {{never produces a constant expression}} |
| 61 |
constexpr HasNonConstExprMemInit(int y) : x(y) {} // ok |
| 62 |
}; |
| 63 |
static_assert(__is_literal(HasNonConstExprMemInit), "fail"); |
| 64 |
|
| 65 |
class HasConstExprCtor { |
| 66 |
int x; |
| 67 |
public: |
| 68 |
constexpr HasConstExprCtor(int x) : x(x) {} |
| 69 |
}; |
| 70 |
template <typename T> class HasConstExprCtorTemplate { |
| 71 |
T x; |
| 72 |
public: |
| 73 |
template <typename U> constexpr HasConstExprCtorTemplate(U y) : x(y) {} |
| 74 |
}; |
| 75 |
template <typename T> class HasConstExprCtorT { |
| 76 |
constexpr HasConstExprCtorT(T) {} |
| 77 |
}; |
| 78 |
static_assert(__is_literal(HasConstExprCtor), "fail"); |
| 79 |
static_assert(__is_literal(HasConstExprCtorTemplate<int>), "fail"); |
| 80 |
static_assert(__is_literal(HasConstExprCtorT<NonLiteral>), "fail"); |
| 81 |
|
| 82 |
|
| 83 |
#if __cplusplus >= 202003L |
| 84 |
namespace GH77924 { |
| 85 |
|
| 86 |
struct A { A(); }; |
| 87 |
template <class T> |
| 88 |
struct opt { |
| 89 |
union Data { |
| 90 |
constexpr Data() : x{} {} |
| 91 |
constexpr ~Data() {} |
| 92 |
char x; |
| 93 |
T data; |
| 94 |
}; |
| 95 |
|
| 96 |
constexpr opt() : data{} {} |
| 97 |
constexpr ~opt() { if (engaged) data.data.~T(); } |
| 98 |
Data data; |
| 99 |
bool engaged = false; |
| 100 |
}; |
| 101 |
|
| 102 |
consteval void foo() { |
| 103 |
opt<A> a; |
| 104 |
} |
| 105 |
|
| 106 |
void test() { |
| 107 |
foo(); |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
#endif |
| 112 |
|
| 113 |
#if __cplusplus >= 201103L |
| 114 |
namespace GH85550 { |
| 115 |
struct HasDefaultCtorAndNonConstexprDtor { |
| 116 |
constexpr HasDefaultCtorAndNonConstexprDtor() = default; |
| 117 |
~HasDefaultCtorAndNonConstexprDtor() {} |
| 118 |
}; |
| 119 |
|
| 120 |
union UnionWithNonLiteralMember { |
| 121 |
HasDefaultCtorAndNonConstexprDtor x; |
| 122 |
int y; |
| 123 |
|
| 124 |
constexpr UnionWithNonLiteralMember() : x{} {} |
| 125 |
}; |
| 126 |
#if __cplusplus >= 202002L |
| 127 |
static_assert(__is_literal(UnionWithNonLiteralMember), "fail"); |
| 128 |
#else |
| 129 |
static_assert(!__is_literal(UnionWithNonLiteralMember), "fail"); |
| 130 |
#endif |
| 131 |
|
| 132 |
union UnionWithNonLiteralMemberExplicitDtor1 { |
| 133 |
HasDefaultCtorAndNonConstexprDtor x; |
| 134 |
int y; |
| 135 |
// expected-note@-2 {{destructor of 'UnionWithNonLiteralMemberExplicitDtor1' is implicitly deleted because variant field 'x' has a non-trivial destructor}} |
| 136 |
|
| 137 |
constexpr UnionWithNonLiteralMemberExplicitDtor1() : x{} {} |
| 138 |
~UnionWithNonLiteralMemberExplicitDtor1() = default; // expected-warning {{explicitly defaulted destructor is implicitly deleted}} |
| 139 |
// expected-note@-1 {{replace 'default' with 'delete'}} |
| 140 |
}; |
| 141 |
#if __cplusplus >= 202002L |
| 142 |
static_assert(__is_literal(UnionWithNonLiteralMemberExplicitDtor1), "fail"); |
| 143 |
#else |
| 144 |
static_assert(!__is_literal(UnionWithNonLiteralMemberExplicitDtor1), "fail"); |
| 145 |
#endif |
| 146 |
|
| 147 |
union UnionWithNonLiteralMemberExplicitDtor2 { |
| 148 |
HasDefaultCtorAndNonConstexprDtor x; |
| 149 |
int y; |
| 150 |
|
| 151 |
constexpr UnionWithNonLiteralMemberExplicitDtor2() : x{} {} |
| 152 |
~UnionWithNonLiteralMemberExplicitDtor2() = delete; |
| 153 |
}; |
| 154 |
static_assert(!__is_literal(UnionWithNonLiteralMemberExplicitDtor2), "fail"); |
| 155 |
|
| 156 |
#if __cplusplus >= 202002L |
| 157 |
union UnionWithNonLiteralMemberConstexprDtor1 { |
| 158 |
HasDefaultCtorAndNonConstexprDtor x; |
| 159 |
int y; |
| 160 |
// expected-note@-2 {{destructor of 'UnionWithNonLiteralMemberConstexprDtor1' is implicitly deleted because variant field 'x' has a non-trivial destructor}} |
| 161 |
|
| 162 |
constexpr UnionWithNonLiteralMemberConstexprDtor1() : x{} {} |
| 163 |
constexpr ~UnionWithNonLiteralMemberConstexprDtor1() = default; // expected-warning {{explicitly defaulted destructor is implicitly deleted}} |
| 164 |
// expected-note@-1 {{replace 'default' with 'delete'}} |
| 165 |
}; |
| 166 |
static_assert(__is_literal(UnionWithNonLiteralMemberConstexprDtor1), "fail"); |
| 167 |
|
| 168 |
union UnionWithNonLiteralMemberConstexprDtor2 { |
| 169 |
HasDefaultCtorAndNonConstexprDtor x; |
| 170 |
int y; |
| 171 |
|
| 172 |
constexpr UnionWithNonLiteralMemberConstexprDtor2() : x{} {} |
| 173 |
constexpr ~UnionWithNonLiteralMemberConstexprDtor2() = delete; |
| 174 |
}; |
| 175 |
static_assert(__is_literal(UnionWithNonLiteralMemberConstexprDtor2), "fail"); |
| 176 |
#endif |
| 177 |
} |
| 178 |
#endif |