cxx2a-constexpr-dynalloc.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // RUN: %clang_cc1 -std=c++2a -verify %s -DNEW=__builtin_operator_new -DDELETE=__builtin_operator_delete
  2. // RUN: %clang_cc1 -std=c++2a -verify %s "-DNEW=operator new" "-DDELETE=operator delete"
  3. // RUN: %clang_cc1 -std=c++2a -verify %s "-DNEW=::operator new" "-DDELETE=::operator delete"
  4. constexpr bool alloc_from_user_code() {
  5. void *p = NEW(sizeof(int)); // expected-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate'}}
  6. DELETE(p);
  7. return true;
  8. }
  9. static_assert(alloc_from_user_code()); // expected-error {{constant expression}} expected-note {{in call}}
  10. namespace std {
  11. using size_t = decltype(sizeof(0));
  12. // FIXME: It would be preferable to point these notes at the location of the call to allocator<...>::[de]allocate instead
  13. template<typename T> struct allocator {
  14. constexpr T *allocate(size_t N) {
  15. return (T*)NEW(sizeof(T) * N); // expected-note 3{{heap allocation}} expected-note {{not deallocated}}
  16. }
  17. constexpr void deallocate(void *p) {
  18. DELETE(p); // expected-note 2{{'std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
  19. }
  20. };
  21. }
  22. constexpr bool alloc_via_std_allocator() {
  23. std::allocator<int> alloc;
  24. int *p = alloc.allocate(1);
  25. alloc.deallocate(p);
  26. return true;
  27. }
  28. static_assert(alloc_via_std_allocator());
  29. template<> struct std::allocator<void()> {
  30. constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of function type 'void ()'}}
  31. };
  32. constexpr void *fn = std::allocator<void()>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
  33. struct Incomplete;
  34. template<> struct std::allocator<Incomplete> {
  35. constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of incomplete type 'Incomplete'}}
  36. };
  37. constexpr void *incomplete = std::allocator<Incomplete>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
  38. struct WrongSize { char x[5]; };
  39. static_assert(sizeof(WrongSize) == 5);
  40. template<> struct std::allocator<WrongSize> {
  41. constexpr void *allocate() { return NEW(7); } // expected-note {{allocated size 7 is not a multiple of size 5 of element type 'WrongSize'}}
  42. };
  43. constexpr void *wrong_size = std::allocator<WrongSize>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
  44. constexpr bool mismatched(int alloc_kind, int dealloc_kind) {
  45. int *p;
  46. switch (alloc_kind) {
  47. case 0:
  48. p = new int; // expected-note {{heap allocation}}
  49. break;
  50. case 1:
  51. p = new int[1]; // expected-note {{heap allocation}}
  52. break;
  53. case 2:
  54. p = std::allocator<int>().allocate(1);
  55. break;
  56. }
  57. switch (dealloc_kind) {
  58. case 0:
  59. delete p; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
  60. break;
  61. case 1:
  62. delete[] p; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
  63. break;
  64. case 2:
  65. std::allocator<int>().deallocate(p); // expected-note 2{{in call}}
  66. break;
  67. }
  68. return true;
  69. }
  70. static_assert(mismatched(0, 2)); // expected-error {{constant expression}} expected-note {{in call}}
  71. static_assert(mismatched(1, 2)); // expected-error {{constant expression}} expected-note {{in call}}
  72. static_assert(mismatched(2, 0)); // expected-error {{constant expression}} expected-note {{in call}}
  73. static_assert(mismatched(2, 1)); // expected-error {{constant expression}} expected-note {{in call}}
  74. static_assert(mismatched(2, 2));
  75. constexpr int *escape = std::allocator<int>().allocate(3); // expected-error {{constant expression}} expected-note {{pointer to subobject of heap-allocated}}
  76. constexpr int leak = (std::allocator<int>().allocate(3), 0); // expected-error {{constant expression}}
  77. constexpr int no_lifetime_start = (*std::allocator<int>().allocate(1) = 1); // expected-error {{constant expression}} expected-note {{assignment to object outside its lifetime}}
  78. void *operator new(std::size_t, void *p) { return p; }
  79. constexpr bool no_placement_new_in_user_code() { // expected-error {{never produces a constant expression}}
  80. int a;
  81. new (&a) int(42); // expected-note {{call to placement 'operator new'}}
  82. return a == 42;
  83. }
  84. namespace std {
  85. constexpr bool placement_new_in_stdlib() {
  86. int a;
  87. new (&a) int(42);
  88. return a == 42;
  89. }
  90. }
  91. static_assert(std::placement_new_in_stdlib());
  92. namespace std {
  93. template<typename T, typename ...Args>
  94. constexpr void construct_at(void *p, Args &&...args) {
  95. new (p) T((Args&&)args...); // #new
  96. }
  97. }
  98. constexpr bool call_std_construct_at() {
  99. int *p = std::allocator<int>().allocate(3);
  100. std::construct_at<int>(p, 1);
  101. std::construct_at<int>(p + 1, 2);
  102. std::construct_at<int>(p + 2, 3);
  103. bool good = p[0] + p[1] + p[2] == 6;
  104. std::allocator<int>().deallocate(p);
  105. return good;
  106. }
  107. static_assert(call_std_construct_at());
  108. constexpr bool bad_construct_at_type() {
  109. int a;
  110. // expected-note@#new {{placement new would change type of storage from 'int' to 'float'}}
  111. std::construct_at<float>(&a, 1.0f); // expected-note {{in call}}
  112. return true;
  113. }
  114. static_assert(bad_construct_at_type()); // expected-error{{}} expected-note {{in call}}
  115. constexpr bool bad_construct_at_subobject() {
  116. struct X { int a, b; };
  117. union A {
  118. int a;
  119. X x;
  120. };
  121. A a = {1};
  122. // expected-note@#new {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
  123. std::construct_at<int>(&a.x.a, 1); // expected-note {{in call}}
  124. return true;
  125. }
  126. static_assert(bad_construct_at_subobject()); // expected-error{{}} expected-note {{in call}}
  127. constexpr bool change_union_member() {
  128. union U {
  129. int a;
  130. int b;
  131. };
  132. U u = {.a = 1};
  133. std::construct_at<int>(&u.b, 2);
  134. return u.b == 2;
  135. }
  136. static_assert(change_union_member());
  137. int external;
  138. // expected-note@#new {{visible outside}}
  139. static_assert((std::construct_at<int>(&external, 1), true)); // expected-error{{}} expected-note {{in call}}
  140. constexpr int &&temporary = 0; // expected-note {{created here}}
  141. // expected-note@#new {{construction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
  142. static_assert((std::construct_at<int>(&temporary, 1), true)); // expected-error{{}} expected-note {{in call}}
  143. constexpr bool construct_after_lifetime() {
  144. int *p = new int;
  145. delete p;
  146. // expected-note@#new {{construction of heap allocated object that has been deleted}}
  147. std::construct_at<int>(p); // expected-note {{in call}}
  148. return true;
  149. }
  150. static_assert(construct_after_lifetime()); // expected-error {{}} expected-note {{in call}}
  151. constexpr bool construct_after_lifetime_2() {
  152. struct A { struct B {} b; };
  153. A a;
  154. a.~A();
  155. std::construct_at<A::B>(&a.b); // expected-note {{in call}}
  156. // expected-note@#new {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
  157. return true;
  158. }
  159. static_assert(construct_after_lifetime_2()); // expected-error {{}} expected-note {{in call}}