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