| 1 |
// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++11 -verify -triple x86_64-apple-darwin %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++17 -verify -triple x86_64-apple-darwin %s |
| 3 |
|
| 4 |
enum class E1 { |
| 5 |
Val1 = 1L |
| 6 |
}; |
| 7 |
|
| 8 |
enum struct E2 { |
| 9 |
Val1 = '\0' |
| 10 |
}; |
| 11 |
|
| 12 |
E1 v1 = Val1; // expected-error{{undeclared identifier}} |
| 13 |
E1 v2 = E1::Val1; |
| 14 |
|
| 15 |
static_assert(sizeof(E1) == sizeof(int), "bad size"); |
| 16 |
static_assert(sizeof(E1::Val1) == sizeof(int), "bad size"); |
| 17 |
static_assert(sizeof(E2) == sizeof(int), "bad size"); |
| 18 |
static_assert(sizeof(E2::Val1) == sizeof(int), "bad size"); |
| 19 |
|
| 20 |
E1 v3 = E2::Val1; // expected-error{{cannot initialize a variable}} |
| 21 |
int x1 = E1::Val1; // expected-error{{cannot initialize a variable}} |
| 22 |
|
| 23 |
enum E3 : char { |
| 24 |
Val2 = 1 |
| 25 |
}; |
| 26 |
|
| 27 |
E3 v4 = Val2; |
| 28 |
E1 v5 = Val2; // expected-error{{cannot initialize a variable}} |
| 29 |
|
| 30 |
static_assert(sizeof(E3) == 1, "bad size"); |
| 31 |
|
| 32 |
int x2 = Val2; |
| 33 |
|
| 34 |
int a1[Val2]; |
| 35 |
int a2[E1::Val1]; |
| 36 |
|
| 37 |
#if __cplusplus >= 201703L |
| 38 |
// expected-error@-3 {{type 'E1' is not implicitly convertible to 'unsigned long'}} |
| 39 |
#else |
| 40 |
// expected-error@-5 {{size of array has non-integer type}} |
| 41 |
#endif |
| 42 |
|
| 43 |
int* p1 = new int[Val2]; |
| 44 |
int* p2 = new int[E1::Val1]; |
| 45 |
|
| 46 |
#if __cplusplus >= 201703L |
| 47 |
// expected-error@-3 {{converting 'E1' to incompatible type 'unsigned long'}} |
| 48 |
#else |
| 49 |
// expected-error@-5 {{array size expression must have integral or unscoped enumeration type, not 'E1'}} |
| 50 |
#endif |
| 51 |
|
| 52 |
enum class E4 { |
| 53 |
e1 = -2147483648, // ok |
| 54 |
e2 = 2147483647, // ok |
| 55 |
e3 = 2147483648 // expected-error{{enumerator value evaluates to 2147483648, which cannot be narrowed to type 'int'}} |
| 56 |
// expected-warning@-1{{changes value}} |
| 57 |
}; |
| 58 |
|
| 59 |
enum class E5 { |
| 60 |
e1 = 2147483647, // ok |
| 61 |
e2 // expected-error{{2147483648 is not representable in the underlying}} |
| 62 |
}; |
| 63 |
|
| 64 |
enum class E6 : bool { |
| 65 |
e1 = false, e2 = true, |
| 66 |
e3 // expected-error{{2 is not representable in the underlying}} |
| 67 |
}; |
| 68 |
|
| 69 |
enum E7 : bool { |
| 70 |
e1 = false, e2 = true, |
| 71 |
e3 // expected-error{{2 is not representable in the underlying}} |
| 72 |
}; |
| 73 |
|
| 74 |
template <class T> |
| 75 |
struct X { |
| 76 |
enum E : T { |
| 77 |
e1, e2, |
| 78 |
e3 // expected-error{{2 is not representable in the underlying}} |
| 79 |
}; |
| 80 |
}; |
| 81 |
|
| 82 |
X<bool> X2; // expected-note{{in instantiation of template}} |
| 83 |
|
| 84 |
enum Incomplete1; // expected-error{{C++ forbids forward references}} |
| 85 |
|
| 86 |
enum Complete1 : int; |
| 87 |
Complete1 complete1; |
| 88 |
|
| 89 |
enum class Complete2; |
| 90 |
Complete2 complete2; |
| 91 |
|
| 92 |
// All the redeclarations below are done twice on purpose. Tests that the type |
| 93 |
// of the declaration isn't changed. |
| 94 |
|
| 95 |
enum class Redeclare2; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}} |
| 96 |
enum Redeclare2; // expected-error{{previously declared as scoped}} |
| 97 |
enum Redeclare2; // expected-error{{previously declared as scoped}} |
| 98 |
|
| 99 |
enum Redeclare3 : int; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}} |
| 100 |
enum Redeclare3; // expected-error{{previously declared with fixed underlying type}} |
| 101 |
enum Redeclare3; // expected-error{{previously declared with fixed underlying type}} |
| 102 |
|
| 103 |
enum class Redeclare5; |
| 104 |
enum class Redeclare5 : int; // ok |
| 105 |
|
| 106 |
enum Redeclare6 : int; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}} |
| 107 |
enum Redeclare6 : short; // expected-error{{redeclared with different underlying type}} |
| 108 |
enum Redeclare6 : short; // expected-error{{redeclared with different underlying type}} |
| 109 |
|
| 110 |
enum class Redeclare7; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}} |
| 111 |
enum class Redeclare7 : short; // expected-error{{redeclared with different underlying type}} |
| 112 |
enum class Redeclare7 : short; // expected-error{{redeclared with different underlying type}} |
| 113 |
|
| 114 |
enum : long { |
| 115 |
long_enum_val = 10000 |
| 116 |
}; |
| 117 |
|
| 118 |
enum : long x; // expected-error{{unnamed enumeration must be a definition}} |
| 119 |
|
| 120 |
void PR9333() { |
| 121 |
enum class scoped_enum { yes, no, maybe }; |
| 122 |
scoped_enum e = scoped_enum::yes; |
| 123 |
if (e == scoped_enum::no) { } |
| 124 |
} |
| 125 |
|
| 126 |
namespace rdar9366066 { |
| 127 |
enum class X : unsigned { value }; |
| 128 |
|
| 129 |
void f(X x) { |
| 130 |
x % X::value; // expected-error{{invalid operands to binary expression ('X' and 'rdar9366066::X')}} |
| 131 |
x % 8; // expected-error{{invalid operands to binary expression ('X' and 'int')}} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// Part 1 of PR10264 |
| 136 |
namespace test5 { |
| 137 |
namespace ns { |
| 138 |
typedef unsigned Atype; |
| 139 |
enum A : Atype; |
| 140 |
} |
| 141 |
enum ns::A : ns::Atype { |
| 142 |
x, y, z |
| 143 |
}; |
| 144 |
} |
| 145 |
|
| 146 |
// Part 2 of PR10264 |
| 147 |
namespace test6 { |
| 148 |
enum A : unsigned; |
| 149 |
struct A::a; // expected-error {{incomplete type 'test6::A' named in nested name specifier}} |
| 150 |
// expected-error@-1{{forward declaration of struct cannot have a nested name specifier}} |
| 151 |
enum A::b; // expected-error {{incomplete type 'test6::A' named in nested name specifier}} |
| 152 |
// expected-error@-1{{forward declaration of enum cannot have a nested name specifier}} |
| 153 |
int A::c; // expected-error {{incomplete type 'test6::A' named in nested name specifier}} |
| 154 |
void A::d(); // expected-error {{incomplete type 'test6::A' named in nested name specifier}} |
| 155 |
void test() { |
| 156 |
(void) A::e; // expected-error {{incomplete type 'test6::A' named in nested name specifier}} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
namespace PR11484 { |
| 161 |
const int val = 104; |
| 162 |
enum class test1 { owner_dead = val, }; |
| 163 |
} |
| 164 |
|
| 165 |
namespace N2764 { |
| 166 |
enum class E *x0a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}} |
| 167 |
enum E2 *x0b; // OK |
| 168 |
enum class E { a, b }; |
| 169 |
enum E x1 = E::a; // ok |
| 170 |
enum class E x2 = E::a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}} |
| 171 |
|
| 172 |
enum F { a, b }; |
| 173 |
enum F y1 = a; // ok |
| 174 |
enum class F y2 = a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}} |
| 175 |
|
| 176 |
struct S { |
| 177 |
friend enum class E; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}} |
| 178 |
// expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}} |
| 179 |
// expected-note@-2 {{remove 'enum class' to befriend an enum}} |
| 180 |
friend enum class F; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}} |
| 181 |
// expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}} |
| 182 |
// expected-note@-2 {{remove 'enum class' to befriend an enum}} |
| 183 |
|
| 184 |
friend enum G {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}} |
| 185 |
// expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}} |
| 186 |
// expected-note@-2 {{remove 'enum' to befriend an enum}} |
| 187 |
friend enum class H {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}} |
| 188 |
// expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}} |
| 189 |
// expected-note@-2 {{remove 'enum' to befriend an enum}} |
| 190 |
friend enum I : int {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}} |
| 191 |
// expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}} |
| 192 |
// expected-note@-2 {{remove 'enum' to befriend an enum}} |
| 193 |
|
| 194 |
enum A : int; |
| 195 |
A a; |
| 196 |
} s; |
| 197 |
|
| 198 |
enum S::A : int {}; |
| 199 |
|
| 200 |
enum class B; |
| 201 |
} |
| 202 |
|
| 203 |
enum class N2764::B {}; |
| 204 |
|
| 205 |
namespace PR12106 { |
| 206 |
template<typename E> struct Enum { |
| 207 |
Enum() : m_e(E::Last) {} |
| 208 |
E m_e; |
| 209 |
}; |
| 210 |
|
| 211 |
enum eCOLORS { Last }; |
| 212 |
Enum<eCOLORS> e; |
| 213 |
} |
| 214 |
|
| 215 |
namespace test7 { |
| 216 |
enum class E { e = (struct S*)0 == (struct S*)0 }; |
| 217 |
S *p; |
| 218 |
} |
| 219 |
|
| 220 |
namespace test8 { |
| 221 |
template<typename T> struct S { |
| 222 |
enum A : int; // expected-note {{here}} |
| 223 |
enum class B; // expected-note {{here}} |
| 224 |
enum class C : int; // expected-note {{here}} |
| 225 |
enum class D : int; // expected-note {{here}} |
| 226 |
}; |
| 227 |
template<typename T> enum S<T>::A { a }; // expected-error {{previously declared with fixed underlying type}} |
| 228 |
template<typename T> enum class S<T>::B : char { b }; // expected-error {{redeclared with different underlying}} |
| 229 |
template<typename T> enum S<T>::C : int { c }; // expected-error {{previously declared as scoped}} |
| 230 |
template<typename T> enum class S<T>::D : char { d }; // expected-error {{redeclared with different underlying}} |
| 231 |
} |
| 232 |
|
| 233 |
namespace test9 { |
| 234 |
template<typename T> struct S { |
| 235 |
enum class ET : T; // expected-note 2{{here}} |
| 236 |
enum class Eint : int; // expected-note 2{{here}} |
| 237 |
}; |
| 238 |
template<> enum class S<int>::ET : int {}; |
| 239 |
template<> enum class S<char>::ET : short {}; // expected-error {{different underlying type}} |
| 240 |
template<> enum class S<int>::Eint : short {}; // expected-error {{different underlying type}} |
| 241 |
template<> enum class S<char>::Eint : int {}; |
| 242 |
|
| 243 |
template<typename T> enum class S<T>::ET : int {}; // expected-error {{different underlying type 'int' (was 'short')}} |
| 244 |
template<typename T> enum class S<T>::Eint : T {}; // expected-error {{different underlying type 'short' (was 'int')}} |
| 245 |
|
| 246 |
// The implicit instantiation of S<short> causes the implicit instantiation of |
| 247 |
// all declarations of member enumerations, so is ill-formed, even though we |
| 248 |
// never instantiate the definitions of S<short>::ET nor S<short>::Eint. |
| 249 |
S<short> s; // expected-note {{in instantiation of}} |
| 250 |
} |
| 251 |
|
| 252 |
namespace test10 { |
| 253 |
template<typename T> int f() { |
| 254 |
enum E : int; |
| 255 |
enum E : T; // expected-note {{here}} |
| 256 |
E x; |
| 257 |
enum E : int { e }; // expected-error {{different underlying}} |
| 258 |
x = e; |
| 259 |
return x; |
| 260 |
} |
| 261 |
int k = f<int>(); |
| 262 |
int l = f<short>(); // expected-note {{here}} |
| 263 |
|
| 264 |
template<typename T> int g() { |
| 265 |
enum class E : int; |
| 266 |
enum class E : T; // expected-note {{here}} |
| 267 |
E x; |
| 268 |
enum class E : int { e }; // expected-error {{different underlying}} |
| 269 |
x = E::e; |
| 270 |
return (int)x; |
| 271 |
} |
| 272 |
int m = g<int>(); |
| 273 |
int n = g<short>(); // expected-note {{here}} |
| 274 |
} |
| 275 |
|
| 276 |
namespace pr13128 { |
| 277 |
// This should compile cleanly |
| 278 |
class C { |
| 279 |
enum class E { C }; |
| 280 |
}; |
| 281 |
} |
| 282 |
|
| 283 |
namespace PR15633 { |
| 284 |
template<typename T> struct A { |
| 285 |
struct B { |
| 286 |
enum class E : T; |
| 287 |
enum class E2 : T; |
| 288 |
}; |
| 289 |
}; |
| 290 |
template<typename T> enum class A<T>::B::E { e }; |
| 291 |
template class A<int>; |
| 292 |
|
| 293 |
struct B { enum class E; }; |
| 294 |
template<typename T> enum class B::E { e }; // expected-error {{enumeration cannot be a template}} |
| 295 |
} |
| 296 |
|
| 297 |
namespace PR16900 { |
| 298 |
enum class A; |
| 299 |
A f(A a) { return -a; } // expected-error {{invalid argument type 'A' to unary expression}} |
| 300 |
} |
| 301 |
|
| 302 |
namespace PR18551 { |
| 303 |
enum class A { A }; |
| 304 |
bool f() { return !A::A; } // expected-error {{invalid argument type 'PR18551::A' to unary expression}} |
| 305 |
} |
| 306 |
|
| 307 |
namespace rdar15124329 { |
| 308 |
enum class B : bool { F, T }; |
| 309 |
|
| 310 |
const rdar15124329::B T1 = B::T; |
| 311 |
typedef B C; const C T2 = B::T; |
| 312 |
|
| 313 |
static_assert(T1 != B::F, ""); |
| 314 |
static_assert(T2 == B::T, ""); |
| 315 |
} |
| 316 |
|
| 317 |
namespace PR18044 { |
| 318 |
enum class E { a }; |
| 319 |
|
| 320 |
int E::e = 0; // expected-error {{does not refer into a class}} |
| 321 |
void E::f() {} // expected-error {{does not refer into a class}} |
| 322 |
struct E::S {}; // expected-error {{no struct named 'S'}} |
| 323 |
struct T : E::S {}; // expected-error {{expected class name}} |
| 324 |
enum E::E {}; // expected-error {{no enum named 'E'}} |
| 325 |
int E::*p; // expected-error {{does not point into a class}} |
| 326 |
using E::f; // expected-error {{no member named 'f'}} |
| 327 |
|
| 328 |
using E::a; // expected-warning {{using declaration naming a scoped enumerator is a C++20 extension}} |
| 329 |
E b = a; |
| 330 |
} |
| 331 |
|
| 332 |
namespace test11 { |
| 333 |
enum class E { a }; |
| 334 |
typedef E E2; |
| 335 |
E2 f1() { return E::a; } |
| 336 |
|
| 337 |
bool f() { return !f1(); } // expected-error {{invalid argument type 'E2' (aka 'test11::E') to unary expression}} |
| 338 |
} |
| 339 |
|
| 340 |
namespace PR35586 { |
| 341 |
enum C { R=-1, G, B }; |
| 342 |
enum B { F = (enum C) -1, T}; // this should compile cleanly, it used to assert. |
| 343 |
}; |
| 344 |
|
| 345 |
namespace test12 { |
| 346 |
// Check that clang rejects this code without crashing in c++17. |
| 347 |
enum class A; |
| 348 |
enum class B; |
| 349 |
A a; |
| 350 |
B b{a}; // expected-error {{cannot initialize}} |
| 351 |
} |