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