1a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith// RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++11 -Werror=c++1y-extensions %s
2a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith// RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++1y -DCXX1Y %s
39f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
49f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithnamespace N {
59f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  typedef char C;
69f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
79f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
89f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithnamespace M {
99f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  typedef double D;
109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
12a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithstruct NonLiteral { // expected-note 3{{no constexpr constructors}}
139f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  NonLiteral() {}
149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  NonLiteral(int) {}
159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith};
169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct Literal {
179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  constexpr Literal() {}
189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  operator int() const { return 0; }
199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith};
209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct S {
229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  virtual int ImplicitlyVirtual() const = 0; // expected-note {{overridden virtual function}}
239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith};
249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct SS : S {
259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  int ImplicitlyVirtual() const;
269f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith};
279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// The definition of a constexpr function shall satisfy the following
299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// constraints:
306180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstruct T : SS, NonLiteral { // expected-note {{base class 'NonLiteral' of non-literal type}}
3186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr T();
32840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr int f() const; // expected-error {{non-literal type 'T' cannot have constexpr members}}
339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - it shall not be virtual;
35840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  virtual constexpr int ExplicitlyVirtual() const { return 0; } // expected-error {{virtual function cannot be constexpr}}
369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
37840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr int ImplicitlyVirtual() const { return 0; } // expected-error {{virtual function cannot be constexpr}}
389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
399f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - its return type shall be a literal type;
40840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr NonLiteral NonLiteralReturn() const { return {}; } // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}}
41a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr void VoidReturn() const { return; }
42a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
43a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{constexpr function's return type 'void' is not a literal type}}
44a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
459f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  constexpr ~T(); // expected-error {{destructor cannot be marked constexpr}}
46840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  typedef NonLiteral F() const;
4786c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr F NonLiteralReturn2; // ok until definition
489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - each of its parameter types shall be a literal type;
50840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr int NonLiteralParam(NonLiteral) const { return 0; } // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}}
51840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  typedef int G(NonLiteral) const;
5286c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr G NonLiteralParam2; // ok until definition
539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - its function-body shall be = delete, = default,
55840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr int Deleted() const = delete;
56a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // It's not possible for the function-body to legally be "= default" here
57a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // (that is, for a non-constructor function) in C++11.
589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // Other than constructors, only the copy- and move-assignment operators and
599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // destructor can be defaulted. Destructors can't be constexpr since they
609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // don't have a literal return type. Defaulted assignment operators can't be
619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // constexpr since they can't be const.
62a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr T &operator=(const T&) = default;
63a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
64a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}}
65a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-warning@-3 {{C++1y}}
66a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith#else
67a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith  // expected-error@-5 {{defaulted definition of copy assignment operator is not constexpr}}
68a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith};
70a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith#ifdef CXX1Y
71a8942d7686dde6d221a176c502ce857bdc409dabRichard Smithstruct T2 {
72a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith  int n = 0;
73a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith  constexpr T2 &operator=(const T2&) = default; // ok
74a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith};
75a8942d7686dde6d221a176c502ce857bdc409dabRichard Smithstruct T3 {
76a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith  constexpr T3 &operator=(const T3&) const = default;
77a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith  // expected-error@-1 {{an explicitly-defaulted copy assignment operator may not have 'const' or 'volatile' qualifiers}}
78a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith};
79a8942d7686dde6d221a176c502ce857bdc409dabRichard Smith#endif
809f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct U {
81840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr U SelfReturn() const;
82840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr int SelfParam(U) const;
839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith};
849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
853534050c491454e4a5c7bcd0f8e6054cd89012cdRichard Smithstruct V : virtual U { // expected-note {{here}}
86840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr int F() const { return 0; } // expected-error {{constexpr member function not allowed in struct with virtual base class}}
873534050c491454e4a5c7bcd0f8e6054cd89012cdRichard Smith};
883534050c491454e4a5c7bcd0f8e6054cd89012cdRichard Smith
89a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith//  or a compound-statememt that contains only [CXX11]
90a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int AllowedStmtsCXX11() {
919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - null statements
929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  ;
939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - static_assert-declarations
959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  static_assert(true, "the impossible happened!");
969f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
979f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - typedef declarations and alias-declarations that do not define classes
989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //    or enumerations
999f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  typedef int I;
1009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  typedef struct S T;
1019f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  using J = int;
1029f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  using K = int[sizeof(I) + sizeof(J)];
1039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // Note, the standard requires we reject this.
1049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  struct U;
1059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
1069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - using-declarations
1079f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  using N::C;
1089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
1099f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - using-directives
1109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  using namespace N;
1119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
1129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  //  - and exactly one return statement
1139f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return sizeof(K) + sizeof(C) + sizeof(K);
1149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
115a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith
116a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith//  or a compound-statement that does not contain [CXX1Y]
117a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int DisallowedStmtsCXX1Y_1() {
118a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  //  - an asm-definition
119a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  asm("int3"); // expected-error {{statement not allowed in constexpr function}}
120a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
121a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
122a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int DisallowedStmtsCXX1Y_2() {
123a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  //  - a goto statement
124a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  goto x; // expected-error {{statement not allowed in constexpr function}}
125a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithx:
126a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
127a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
128a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int DisallowedStmtsCXX1Y_3() {
129a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  //  - a try-block,
130a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  try {} catch (...) {} // expected-error {{statement not allowed in constexpr function}}
131a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
132a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
133a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int DisallowedStmtsCXX1Y_4() {
134a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  //  - a definition of a variable of non-literal type
135a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  NonLiteral nl; // expected-error {{variable of non-literal type 'NonLiteral' cannot be defined in a constexpr function}}
136a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
137a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
138a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int DisallowedStmtsCXX1Y_5() {
139a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  //  - a definition of a variable of static storage duration
140a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  static constexpr int n = 123; // expected-error {{static variable not permitted in a constexpr function}}
141a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return n;
142a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
143a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int DisallowedStmtsCXX1Y_6() {
144a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  //  - a definition of a variable of thread storage duration
145a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  thread_local constexpr int n = 123; // expected-error {{thread_local variable not permitted in a constexpr function}}
146a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return n;
147a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
148a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int DisallowedStmtsCXX1Y_7() {
149a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  //  - a definition of a variable for which no initialization is performed
150a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  int n; // expected-error {{variables defined in a constexpr function must be initialized}}
151a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
152a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
153a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith
1549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int ForStmt() {
155a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  for (int n = 0; n < 10; ++n)
156a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
157a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{statement not allowed in constexpr function}}
158a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
1599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return 0;
1609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
1619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int VarDecl() {
162a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  int a = 0;
163a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
164a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
165a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
166a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
167a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
168a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int ConstexprVarDecl() {
169a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr int a = 0;
170a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
171a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
172a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
173a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
174a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
175a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int VarWithCtorDecl() {
176a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  Literal a;
177a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
178a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
179a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
1809f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return 0;
1819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
182a10b97898ee6339c3110e6ca33f178ff52f05238Richard SmithNonLiteral nl;
183a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr NonLiteral &ExternNonLiteralVarDecl() {
184a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  extern NonLiteral nl;
185a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
186a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
187a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
188a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return nl;
189a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
190a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithstatic_assert(&ExternNonLiteralVarDecl() == &nl, "");
1919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int FuncDecl() {
192a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr int ForwardDecl(int);
193a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
194a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{use of this statement in a constexpr function is a C++1y extension}}
195a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
1969f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return ForwardDecl(42);
1979f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
1989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int ClassDecl1() {
199a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  typedef struct { } S1;
200a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
201a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
202a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
2039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return 0;
2049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
2059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int ClassDecl2() {
206a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  using S2 = struct { };
207a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
208a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
209a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
2109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return 0;
2119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
2129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int ClassDecl3() {
213a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  struct S3 { };
214a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
215a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
216a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
2179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return 0;
2189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
2199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int NoReturn() {} // expected-error {{no return statement in constexpr function}}
2209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int MultiReturn() {
221a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
222a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  return 0;
223a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
224a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-error@-2 {{multiple return statements in constexpr function}}
225a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  // expected-note@-4 {{return statement}}
226a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
2279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
2289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
2299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith//  - every constructor call and implicit conversion used in initializing the
2309f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith//    return value shall be one of those allowed in a constant expression.
2319f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith//
2329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// We implement the proposed resolution of DR1364 and ignore this bullet.
233745f5147e065900267c85a5568785a1991d4838fRichard Smith// However, we implement the spirit of the check as part of the p5 checking that
234745f5147e065900267c85a5568785a1991d4838fRichard Smith// a constexpr function must be able to produce a constant expression.
235745f5147e065900267c85a5568785a1991d4838fRichard Smithnamespace DR1364 {
236745f5147e065900267c85a5568785a1991d4838fRichard Smith  constexpr int f(int k) {
237745f5147e065900267c85a5568785a1991d4838fRichard Smith    return k; // ok, even though lvalue-to-rvalue conversion of a function
238745f5147e065900267c85a5568785a1991d4838fRichard Smith              // parameter is not allowed in a constant expression.
239745f5147e065900267c85a5568785a1991d4838fRichard Smith  }
240745f5147e065900267c85a5568785a1991d4838fRichard Smith  int kGlobal; // expected-note {{here}}
241745f5147e065900267c85a5568785a1991d4838fRichard Smith  constexpr int f() { // expected-error {{constexpr function never produces a constant expression}}
242745f5147e065900267c85a5568785a1991d4838fRichard Smith    return kGlobal; // expected-note {{read of non-const}}
243745f5147e065900267c85a5568785a1991d4838fRichard Smith  }
244745f5147e065900267c85a5568785a1991d4838fRichard Smith}
245484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor
246484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregornamespace rdar13584715 {
247484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor  typedef __PTRDIFF_TYPE__ ptrdiff_t;
248484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor
249484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor  template<typename T> struct X {
250484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor    static T value() {};
251484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor  };
252484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor
253484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor  void foo(ptrdiff_t id) {
254484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor    switch (id) {
255484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor    case reinterpret_cast<ptrdiff_t>(&X<long>::value):  // expected-error{{case value is not a constant expression}} \
256484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor      // expected-note{{reinterpret_cast is not allowed in a constant expression}}
257484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor      break;
258484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor    }
259484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor  }
260484f6fa0bd6b16b64e5c4cb40260f69a3dd52b4aDouglas Gregor}
261a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith
262a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithnamespace std_example {
263a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr int square(int x) {
264a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    return x * x;
265a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  }
266a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr long long_max() {
267a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    return 2147483647;
268a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  }
269a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr int abs(int x) {
270a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    if (x < 0)
271a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
272a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      // expected-error@-2 {{C++1y}}
273a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
274a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      x = -x;
275a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    return x;
276a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  }
277a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr int first(int n) {
278a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    static int value = n; // expected-error {{static variable not permitted}}
279a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    return value;
280a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  }
281a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr int uninit() {
282a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    int a; // expected-error {{must be initialized}}
283a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    return a;
284a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  }
285b476a14e9b35ef2448b42b033e1f0cceaa3f2778Richard Smith  constexpr int prev(int x) {
286b476a14e9b35ef2448b42b033e1f0cceaa3f2778Richard Smith    return --x;
287a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  }
2885528ac9f40ec6cb54e7096908bf2beeed511bce4Richard Smith#ifndef CXX1Y
289b476a14e9b35ef2448b42b033e1f0cceaa3f2778Richard Smith  // expected-error@-4 {{never produces a constant expression}}
290b476a14e9b35ef2448b42b033e1f0cceaa3f2778Richard Smith  // expected-note@-4 {{subexpression}}
291b476a14e9b35ef2448b42b033e1f0cceaa3f2778Richard Smith#endif
292a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  constexpr int g(int x, int n) {
293a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    int r = 1;
294a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    while (--n > 0) r *= x;
295a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    return r;
296a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  }
297a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y
298a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    // expected-error@-5 {{C++1y}}
299a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith    // expected-error@-5 {{statement not allowed}}
300a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif
301a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith}
302