brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.8 KiB · 44b6ba6 Raw
179 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s3 4 5static_assert(__is_literal(int), "fail");6static_assert(__is_literal_type(int), "fail"); // alternate spelling for GCC7static_assert(__is_literal(void*), "fail");8enum E { E1 };9static_assert(__is_literal(E), "fail");10static_assert(__is_literal(decltype(E1)), "fail");11typedef int IAR[10];12static_assert(__is_literal(IAR), "fail");13typedef int Vector __attribute__((vector_size(16)));14typedef int VectorExt __attribute__((ext_vector_type(4)));15static_assert(__is_literal(Vector), "fail");16static_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 destructor23//           [P0784R7 changed the condition to "constexpr destructor" in C++20]24//        -- every constructor call and full-expression in the25//           brace-or-equal-initializers for non-static data members (if an) is26//           a constant expression,27//        -- it is an aggregate type or has at least one constexpr constructor28//           or constructor template that is not a copy or move constructor, and29//           [DR1452 adds class types with trivial default constructors to30//            this list]31//        -- it has all non-static data members and base classes of literal32//           types33struct Empty {};34struct LiteralType {35  int x;36  E e;37  IAR arr;38  Empty empty;39  int method();40};41struct HasDtor { ~HasDtor(); };42 43class NonAggregate { int x; };44struct NonLiteral { NonLiteral(); };45struct HasNonLiteralBase : NonLiteral {};46struct HasNonLiteralMember { HasDtor x; };47 48static_assert(__is_literal(Empty), "fail");49static_assert(__is_literal(LiteralType), "fail");50static_assert(__is_literal(NonAggregate), "fail");51static_assert(!__is_literal(NonLiteral), "fail");52static_assert(!__is_literal(HasDtor), "fail");53static_assert(!__is_literal(HasNonLiteralBase), "fail");54static_assert(!__is_literal(HasNonLiteralMember), "fail");55 56// DR1361 removes the brace-or-equal-initializer bullet so that we can allow:57extern int f(); // expected-note {{here}}58struct 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) {} // ok62};63static_assert(__is_literal(HasNonConstExprMemInit), "fail");64 65class HasConstExprCtor {66  int x;67public:68  constexpr HasConstExprCtor(int x) : x(x) {}69};70template <typename T> class HasConstExprCtorTemplate {71  T x;72public:73  template <typename U> constexpr HasConstExprCtorTemplate(U y) : x(y) {}74};75template <typename T> class HasConstExprCtorT {76  constexpr HasConstExprCtorT(T) {}77};78static_assert(__is_literal(HasConstExprCtor), "fail");79static_assert(__is_literal(HasConstExprCtorTemplate<int>), "fail");80static_assert(__is_literal(HasConstExprCtorT<NonLiteral>), "fail");81 82 83#if __cplusplus >= 202003L84namespace GH77924 {85 86struct A { A(); };87template <class T>88struct 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 102consteval void foo() {103  opt<A> a;104}105 106void test() {107  foo();108}109 110}111#endif112 113#if __cplusplus >= 201103L114namespace GH85550 {115struct HasDefaultCtorAndNonConstexprDtor {116  constexpr HasDefaultCtorAndNonConstexprDtor() = default;117  ~HasDefaultCtorAndNonConstexprDtor() {}118};119 120union UnionWithNonLiteralMember {121  HasDefaultCtorAndNonConstexprDtor x;122  int y;123 124  constexpr UnionWithNonLiteralMember() : x{} {}125};126#if __cplusplus >= 202002L127static_assert(__is_literal(UnionWithNonLiteralMember), "fail");128#else129static_assert(!__is_literal(UnionWithNonLiteralMember), "fail");130#endif131 132union 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 >= 202002L142static_assert(__is_literal(UnionWithNonLiteralMemberExplicitDtor1), "fail");143#else144static_assert(!__is_literal(UnionWithNonLiteralMemberExplicitDtor1), "fail");145#endif146 147union UnionWithNonLiteralMemberExplicitDtor2 {148  HasDefaultCtorAndNonConstexprDtor x;149  int y;150 151  constexpr UnionWithNonLiteralMemberExplicitDtor2() : x{} {}152  ~UnionWithNonLiteralMemberExplicitDtor2() = delete;153};154static_assert(!__is_literal(UnionWithNonLiteralMemberExplicitDtor2), "fail");155 156#if __cplusplus >= 202002L157union 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};166static_assert(__is_literal(UnionWithNonLiteralMemberConstexprDtor1), "fail");167 168union UnionWithNonLiteralMemberConstexprDtor2 {169  HasDefaultCtorAndNonConstexprDtor x;170  int y;171 172  constexpr UnionWithNonLiteralMemberConstexprDtor2() : x{} {}173  constexpr ~UnionWithNonLiteralMemberConstexprDtor2() = delete;174};175static_assert(__is_literal(UnionWithNonLiteralMemberConstexprDtor2), "fail");176#endif177}178#endif179