Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / enum-scoped.cpp
blob2bfe7203367503b85d5d4334171fa06e22071c0d
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
4 enum class E1 {
5 Val1 = 1L
6 };
8 enum struct E2 {
9 Val1 = '\0'
12 E1 v1 = Val1; // expected-error{{undeclared identifier}}
13 E1 v2 = E1::Val1;
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");
20 E1 v3 = E2::Val1; // expected-error{{cannot initialize a variable}}
21 int x1 = E1::Val1; // expected-error{{cannot initialize a variable}}
23 enum E3 : char {
24 Val2 = 1
27 E3 v4 = Val2;
28 E1 v5 = Val2; // expected-error{{cannot initialize a variable}}
30 static_assert(sizeof(E3) == 1, "bad size");
32 int x2 = Val2;
34 int a1[Val2];
35 int a2[E1::Val1];
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
43 int* p1 = new int[Val2];
44 int* p2 = new int[E1::Val1];
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
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'}}
58 enum class E5 {
59 e1 = 2147483647, // ok
60 e2 // expected-error{{2147483648 is not representable in the underlying}}
63 enum class E6 : bool {
64 e1 = false, e2 = true,
65 e3 // expected-error{{2 is not representable in the underlying}}
68 enum E7 : bool {
69 e1 = false, e2 = true,
70 e3 // expected-error{{2 is not representable in the underlying}}
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}}
81 X<bool> X2; // expected-note{{in instantiation of template}}
83 enum Incomplete1; // expected-error{{C++ forbids forward references}}
85 enum Complete1 : int;
86 Complete1 complete1;
88 enum class Complete2;
89 Complete2 complete2;
91 // All the redeclarations below are done twice on purpose. Tests that the type
92 // of the declaration isn't changed.
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}}
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}}
102 enum class Redeclare5;
103 enum class Redeclare5 : int; // ok
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}}
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}}
113 enum : long {
114 long_enum_val = 10000
117 enum : long x; // expected-error{{unnamed enumeration must be a definition}}
119 void PR9333() {
120 enum class scoped_enum { yes, no, maybe };
121 scoped_enum e = scoped_enum::yes;
122 if (e == scoped_enum::no) { }
125 namespace rdar9366066 {
126 enum class X : unsigned { value };
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')}}
134 // Part 1 of PR10264
135 namespace test5 {
136 namespace ns {
137 typedef unsigned Atype;
138 enum A : Atype;
140 enum ns::A : ns::Atype {
141 x, y, z
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 enum A::b; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
150 int A::c; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
151 void A::d(); // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
152 void test() {
153 (void) A::e; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
157 namespace PR11484 {
158 const int val = 104;
159 enum class test1 { owner_dead = val, };
162 namespace N2764 {
163 enum class E *x0a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
164 enum E2 *x0b; // OK
165 enum class E { a, b };
166 enum E x1 = E::a; // ok
167 enum class E x2 = E::a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
169 enum F { a, b };
170 enum F y1 = a; // ok
171 enum class F y2 = a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
173 struct S {
174 friend enum class E; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
175 friend enum class F; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
177 friend enum G {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
178 friend enum class H {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
179 friend enum I : int {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
181 enum A : int;
182 A a;
183 } s;
185 enum S::A : int {};
187 enum class B;
190 enum class N2764::B {};
192 namespace PR12106 {
193 template<typename E> struct Enum {
194 Enum() : m_e(E::Last) {}
195 E m_e;
198 enum eCOLORS { Last };
199 Enum<eCOLORS> e;
202 namespace test7 {
203 enum class E { e = (struct S*)0 == (struct S*)0 };
204 S *p;
207 namespace test8 {
208 template<typename T> struct S {
209 enum A : int; // expected-note {{here}}
210 enum class B; // expected-note {{here}}
211 enum class C : int; // expected-note {{here}}
212 enum class D : int; // expected-note {{here}}
214 template<typename T> enum S<T>::A { a }; // expected-error {{previously declared with fixed underlying type}}
215 template<typename T> enum class S<T>::B : char { b }; // expected-error {{redeclared with different underlying}}
216 template<typename T> enum S<T>::C : int { c }; // expected-error {{previously declared as scoped}}
217 template<typename T> enum class S<T>::D : char { d }; // expected-error {{redeclared with different underlying}}
220 namespace test9 {
221 template<typename T> struct S {
222 enum class ET : T; // expected-note 2{{here}}
223 enum class Eint : int; // expected-note 2{{here}}
225 template<> enum class S<int>::ET : int {};
226 template<> enum class S<char>::ET : short {}; // expected-error {{different underlying type}}
227 template<> enum class S<int>::Eint : short {}; // expected-error {{different underlying type}}
228 template<> enum class S<char>::Eint : int {};
230 template<typename T> enum class S<T>::ET : int {}; // expected-error {{different underlying type 'int' (was 'short')}}
231 template<typename T> enum class S<T>::Eint : T {}; // expected-error {{different underlying type 'short' (was 'int')}}
233 // The implicit instantiation of S<short> causes the implicit instantiation of
234 // all declarations of member enumerations, so is ill-formed, even though we
235 // never instantiate the definitions of S<short>::ET nor S<short>::Eint.
236 S<short> s; // expected-note {{in instantiation of}}
239 namespace test10 {
240 template<typename T> int f() {
241 enum E : int;
242 enum E : T; // expected-note {{here}}
243 E x;
244 enum E : int { e }; // expected-error {{different underlying}}
245 x = e;
246 return x;
248 int k = f<int>();
249 int l = f<short>(); // expected-note {{here}}
251 template<typename T> int g() {
252 enum class E : int;
253 enum class E : T; // expected-note {{here}}
254 E x;
255 enum class E : int { e }; // expected-error {{different underlying}}
256 x = E::e;
257 return (int)x;
259 int m = g<int>();
260 int n = g<short>(); // expected-note {{here}}
263 namespace pr13128 {
264 // This should compile cleanly
265 class C {
266 enum class E { C };
270 namespace PR15633 {
271 template<typename T> struct A {
272 struct B {
273 enum class E : T;
274 enum class E2 : T;
277 template<typename T> enum class A<T>::B::E { e };
278 template class A<int>;
280 struct B { enum class E; };
281 template<typename T> enum class B::E { e }; // expected-error {{enumeration cannot be a template}}
284 namespace PR16900 {
285 enum class A;
286 A f(A a) { return -a; } // expected-error {{invalid argument type 'A' to unary expression}}
289 namespace PR18551 {
290 enum class A { A };
291 bool f() { return !A::A; } // expected-error {{invalid argument type 'PR18551::A' to unary expression}}
294 namespace rdar15124329 {
295 enum class B : bool { F, T };
297 const rdar15124329::B T1 = B::T;
298 typedef B C; const C T2 = B::T;
300 static_assert(T1 != B::F, "");
301 static_assert(T2 == B::T, "");
304 namespace PR18044 {
305 enum class E { a };
307 int E::e = 0; // expected-error {{does not refer into a class}}
308 void E::f() {} // expected-error {{does not refer into a class}}
309 struct E::S {}; // expected-error {{no struct named 'S'}}
310 struct T : E::S {}; // expected-error {{expected class name}}
311 enum E::E {}; // expected-error {{no enum named 'E'}}
312 int E::*p; // expected-error {{does not point into a class}}
313 using E::f; // expected-error {{no member named 'f'}}
315 using E::a; // expected-warning {{using declaration naming a scoped enumerator is a C++20 extension}}
316 E b = a;
319 namespace test11 {
320 enum class E { a };
321 typedef E E2;
322 E2 f1() { return E::a; }
324 bool f() { return !f1(); } // expected-error {{invalid argument type 'E2' (aka 'test11::E') to unary expression}}
327 namespace PR35586 {
328 enum C { R=-1, G, B };
329 enum B { F = (enum C) -1, T}; // this should compile cleanly, it used to assert.
332 namespace test12 {
333 // Check that clang rejects this code without crashing in c++17.
334 enum class A;
335 enum class B;
336 A a;
337 B b{a}; // expected-error {{cannot initialize}}