1 // RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++11 -Werror=c++14-extensions -Werror=c++20-extensions %s
2 // RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++14 -DCXX14 -Werror=c++20-extensions %s
3 // RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++20 -DCXX14 -DCXX20 %s
4 
5 namespace N {
6   typedef char C;
7 }
8 
9 namespace M {
10   typedef double D;
11 }
12 
13 struct NonLiteral { // expected-note 3{{no constexpr constructors}}
14   NonLiteral() {}
15   NonLiteral(int) {}
16 };
17 struct Literal {
18   constexpr Literal() {}
19   operator int() const { return 0; }
20 };
21 
22 struct S {
23   virtual int ImplicitlyVirtual() const = 0;
24 #if __cplusplus <= 201703L
25   // expected-note@-2 {{overridden virtual function}}
26 #endif
27 };
28 struct SS : S {
29   int ImplicitlyVirtual() const;
30 };
31 
32 // The definition of a constexpr function shall satisfy the following
33 // constraints:
34 struct T : SS, NonLiteral {
35   constexpr T();
36   constexpr int f() const;
37 
38   //  - it shall not be virtual; [until C++20]
39   virtual constexpr int ExplicitlyVirtual() const { return 0; }
40 #if __cplusplus <= 201703L
41   // expected-error@-2 {{virtual function cannot be constexpr}}
42 #endif
43 
44   constexpr int ImplicitlyVirtual() const { return 0; }
45 #if __cplusplus <= 201703L
46   // expected-error@-2 {{virtual function cannot be constexpr}}
47 #endif
48 
49   virtual constexpr int OutOfLineVirtual() const;
50 #if __cplusplus <= 201703L
51   // expected-error@-2 {{virtual function cannot be constexpr}}
52 #endif
53 
54   //  - its return type shall be a literal type;
55   constexpr NonLiteral NonLiteralReturn() const { return {}; } // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}}
56   constexpr void VoidReturn() const { return; }
57 #ifndef CXX14
58   // expected-error@-2 {{constexpr function's return type 'void' is not a literal type}}
59 #endif
60   constexpr ~T();
61 #ifndef CXX20
62   // expected-error@-2 {{destructor cannot be declared constexpr}}
63 #endif
64   typedef NonLiteral F() const;
65   constexpr F NonLiteralReturn2; // ok until definition
66 
67   //  - each of its parameter types shall be a literal type;
68   constexpr int NonLiteralParam(NonLiteral) const { return 0; } // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}}
69   typedef int G(NonLiteral) const;
70   constexpr G NonLiteralParam2; // ok until definition
71 
72   //  - its function-body shall be = delete, = default,
73   constexpr int Deleted() const = delete;
74   // It's not possible for the function-body to legally be "= default" here
75   // (that is, for a non-constructor function) in C++11.
76   // Other than constructors, only the copy- and move-assignment operators and
77   // destructor can be defaulted. Destructors can't be constexpr since they
78   // don't have a literal return type. Defaulted assignment operators can't be
79   // constexpr since they can't be const.
80   constexpr T &operator=(const T&) = default;
81 #ifndef CXX14
82   // expected-error@-2 {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}}
83   // expected-warning@-3 {{C++14}}
84 #else
85   // expected-error@-5 {{defaulted definition of copy assignment operator is not constexpr}}
86 #endif
87 };
88 
89 constexpr int T::OutOfLineVirtual() const { return 0; }
90 #ifdef CXX14
91 struct T2 {
92   int n = 0;
93   constexpr T2 &operator=(const T2&) = default; // ok
94 };
95 struct T3 {
96   constexpr T3 &operator=(const T3&) const = default;
97 #ifndef CXX20
98   // expected-error@-2 {{an explicitly-defaulted copy assignment operator may not have 'const' or 'volatile' qualifiers}}
99 #else
100   // expected-warning@-4 {{explicitly defaulted copy assignment operator is implicitly deleted}}
101   // expected-note@-5 {{function is implicitly deleted because its declared type does not match the type of an implicit copy assignment operator}}
102 #endif
103 };
104 #endif
105 struct U {
106   constexpr U SelfReturn() const;
107   constexpr int SelfParam(U) const;
108 };
109 
110 struct V : virtual U { // expected-note {{here}}
111   constexpr int F() const { return 0; } // expected-error {{constexpr member function not allowed in struct with virtual base class}}
112 };
113 
114 //  or a compound-statememt that contains only [CXX11]
115 constexpr int AllowedStmtsCXX11() {
116   //  - null statements
117   ;
118 
119   //  - static_assert-declarations
120   static_assert(true, "the impossible happened!");
121 
122   //  - typedef declarations and alias-declarations that do not define classes
123   //    or enumerations
124   typedef int I;
125   typedef struct S T;
126   using J = int;
127   using K = int[sizeof(I) + sizeof(J)];
128   // Note, the standard requires we reject this.
129   struct U;
130 
131   //  - using-declarations
132   using N::C;
133 
134   //  - using-directives
135   using namespace N;
136 
137   //  - and exactly one return statement
138   return sizeof(K) + sizeof(C) + sizeof(K);
139 }
140 
141 //  or a compound-statement that does not contain [C++14]
142 constexpr int DisallowedStmtsCXX14_1(bool b) {
143   //  - an asm-definition
144   if (b)
145     asm("int3");
146 #if !defined(CXX20)
147   // expected-error@-2 {{use of this statement in a constexpr function is a C++20 extension}}
148 #endif
149   return 0;
150 }
151 constexpr int DisallowedStmtsCXX14_2() {
152   //  - a goto statement
153   goto x; // expected-error {{statement not allowed in constexpr function}}
154 x:
155   return 0;
156 }
157 constexpr int DisallowedStmtsCXX14_2_1() {
158   try {
159     return 0;
160   } catch (...) {
161   merp: goto merp; // expected-error {{statement not allowed in constexpr function}}
162   }
163 }
164 constexpr int DisallowedStmtsCXX14_3() {
165   //  - a try-block,
166   try {} catch (...) {}
167 #if !defined(CXX20)
168   // expected-error@-2 {{use of this statement in a constexpr function is a C++20 extension}}
169 #endif
170   return 0;
171 }
172 constexpr int DisallowedStmtsCXX14_4() {
173   //  - a definition of a variable of non-literal type
174   NonLiteral nl; // expected-error {{variable of non-literal type 'NonLiteral' cannot be defined in a constexpr function}}
175   return 0;
176 }
177 constexpr int DisallowedStmtsCXX14_5() {
178   //  - a definition of a variable of static storage duration
179   static constexpr int n = 123; // expected-error {{static variable not permitted in a constexpr function}}
180   return n;
181 }
182 constexpr int DisallowedStmtsCXX14_6() {
183   //  - a definition of a variable of thread storage duration
184   thread_local constexpr int n = 123; // expected-error {{thread_local variable not permitted in a constexpr function}}
185   return n;
186 }
187 constexpr int DisallowedStmtsCXX14_7() {
188   //  - a definition of a variable for which no initialization is performed
189   int n;
190 #ifndef CXX20
191   // expected-error@-2 {{uninitialized variable in a constexpr function}}
192 #endif
193   return 0;
194 }
195 
196 constexpr int ForStmt() {
197   for (int n = 0; n < 10; ++n)
198 #ifndef CXX14
199   // expected-error@-2 {{statement not allowed in constexpr function}}
200 #endif
201     return 0;
202 }
203 constexpr int VarDecl() {
204   int a = 0;
205 #ifndef CXX14
206   // expected-error@-2 {{variable declaration in a constexpr function is a C++14 extension}}
207 #endif
208   return 0;
209 }
210 constexpr int ConstexprVarDecl() {
211   constexpr int a = 0;
212 #ifndef CXX14
213   // expected-error@-2 {{variable declaration in a constexpr function is a C++14 extension}}
214 #endif
215   return 0;
216 }
217 constexpr int VarWithCtorDecl() {
218   Literal a;
219 #ifndef CXX14
220   // expected-error@-2 {{variable declaration in a constexpr function is a C++14 extension}}
221 #endif
222   return 0;
223 }
224 NonLiteral nl;
225 constexpr NonLiteral &ExternNonLiteralVarDecl() {
226   extern NonLiteral nl;
227 #ifndef CXX14
228   // expected-error@-2 {{variable declaration in a constexpr function is a C++14 extension}}
229 #endif
230   return nl;
231 }
232 static_assert(&ExternNonLiteralVarDecl() == &nl, "");
233 constexpr int FuncDecl() {
234   constexpr int ForwardDecl(int);
235 #ifndef CXX14
236   // expected-error@-2 {{use of this statement in a constexpr function is a C++14 extension}}
237 #endif
238   return ForwardDecl(42);
239 }
240 constexpr int ClassDecl1() {
241   typedef struct { } S1;
242 #ifndef CXX14
243   // expected-error@-2 {{type definition in a constexpr function is a C++14 extension}}
244 #endif
245   return 0;
246 }
247 constexpr int ClassDecl2() {
248   using S2 = struct { };
249 #ifndef CXX14
250   // expected-error@-2 {{type definition in a constexpr function is a C++14 extension}}
251 #endif
252   return 0;
253 }
254 constexpr int ClassDecl3() {
255   struct S3 { };
256 #ifndef CXX14
257   // expected-error@-2 {{type definition in a constexpr function is a C++14 extension}}
258 #endif
259   return 0;
260 }
261 constexpr int NoReturn() {} // expected-error {{no return statement in constexpr function}}
262 constexpr int MultiReturn() {
263   return 0;
264   return 0;
265 #ifndef CXX14
266   // expected-error@-2 {{multiple return statements in constexpr function}}
267   // expected-note@-4 {{return statement}}
268 #endif
269 }
270 
271 //  - every constructor call and implicit conversion used in initializing the
272 //    return value shall be one of those allowed in a constant expression.
273 //
274 // We implement the proposed resolution of DR1364 and ignore this bullet.
275 // However, we implement the spirit of the check as part of the p5 checking that
276 // a constexpr function must be able to produce a constant expression.
277 namespace DR1364 {
278   constexpr int f(int k) {
279     return k; // ok, even though lvalue-to-rvalue conversion of a function
280               // parameter is not allowed in a constant expression.
281   }
282   int kGlobal; // expected-note {{here}}
283   constexpr int f() { // expected-error {{constexpr function never produces a constant expression}}
284     return kGlobal; // expected-note {{read of non-const}}
285   }
286 }
287 
288 namespace rdar13584715 {
289   typedef __PTRDIFF_TYPE__ ptrdiff_t;
290 
291   template<typename T> struct X {
292     static T value() {};
293   };
294 
295   void foo(ptrdiff_t id) {
296     switch (id) {
297     case reinterpret_cast<ptrdiff_t>(&X<long>::value):  // expected-error{{case value is not a constant expression}} \
298       // expected-note{{reinterpret_cast is not allowed in a constant expression}}
299       break;
300     }
301   }
302 }
303 
304 namespace std_example {
305   constexpr int square(int x) {
306     return x * x;
307   }
308   constexpr long long_max() {
309     return 2147483647;
310   }
311   constexpr int abs(int x) {
312     if (x < 0)
313 #ifndef CXX14
314       // expected-error@-2 {{C++14}}
315 #endif
316       x = -x;
317     return x;
318   }
319   constexpr int first(int n) {
320     static int value = n; // expected-error {{static variable not permitted}}
321     return value;
322   }
323   constexpr int uninit() {
324     int a;
325 #ifndef CXX20
326     // expected-error@-2 {{uninitialized}}
327 #endif
328     return a;
329   }
330   constexpr int prev(int x) {
331     return --x;
332   }
333 #ifndef CXX14
334   // expected-error@-4 {{never produces a constant expression}}
335   // expected-note@-4 {{subexpression}}
336 #endif
337   constexpr int g(int x, int n) {
338     int r = 1;
339     while (--n > 0) r *= x;
340     return r;
341   }
342 #ifndef CXX14
343     // expected-error@-5 {{C++14}}
344     // expected-error@-5 {{statement not allowed}}
345 #endif
346 }
347