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
12 E1 v1
= Val1
; // expected-error{{undeclared identifier}}
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}}
28 E1 v5
= Val2
; // expected-error{{cannot initialize a variable}}
30 static_assert(sizeof(E3
) == 1, "bad size");
37 #if __cplusplus >= 201703L
38 // expected-error@-3 {{type 'E1' is not implicitly convertible to 'unsigned long'}}
40 // expected-error@-5 {{size of array has non-integer type}}
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'}}
49 // expected-error@-5 {{array size expression must have integral or unscoped enumeration type, not 'E1'}}
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}}
60 e1
= 2147483647, // ok
61 e2
// expected-error{{2147483648 is not representable in the underlying}}
64 enum class E6
: bool {
65 e1
= false, e2
= true,
66 e3
// expected-error{{2 is not representable in the underlying}}
70 e1
= false, e2
= true,
71 e3
// expected-error{{2 is not representable in the underlying}}
78 e3
// expected-error{{2 is not representable in the underlying}}
82 X
<bool> X2
; // expected-note{{in instantiation of template}}
84 enum Incomplete1
; // expected-error{{C++ forbids forward references}}
92 // All the redeclarations below are done twice on purpose. Tests that the type
93 // of the declaration isn't changed.
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}}
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}}
103 enum class Redeclare5
;
104 enum class Redeclare5
: int; // ok
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}}
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}}
115 long_enum_val
= 10000
118 enum : long x
; // expected-error{{unnamed enumeration must be a definition}}
121 enum class scoped_enum
{ yes
, no
, maybe
};
122 scoped_enum e
= scoped_enum::yes
;
123 if (e
== scoped_enum::no
) { }
126 namespace rdar9366066
{
127 enum class X
: unsigned { value
};
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')}}
138 typedef unsigned Atype
;
141 enum ns::A
: ns::Atype
{
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}}
156 (void) A::e
; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
162 enum class test1
{ owner_dead
= val
, };
166 enum class E
*x0a
; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
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'}}
174 enum class F y2
= a
; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
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}}
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}}
203 enum class N2764::B
{};
206 template<typename E
> struct Enum
{
207 Enum() : m_e(E::Last
) {}
211 enum eCOLORS
{ Last
};
216 enum class E
{ e
= (struct S
*)0 == (struct S
*)0 };
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}}
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}}
234 template<typename T
> struct S
{
235 enum class ET
: T
; // expected-note 2{{here}}
236 enum class Eint
: int; // expected-note 2{{here}}
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 {};
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')}}
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}}
253 template<typename T
> int f() {
255 enum E
: T
; // expected-note {{here}}
257 enum E
: int { e
}; // expected-error {{different underlying}}
262 int l
= f
<short>(); // expected-note {{here}}
264 template<typename T
> int g() {
266 enum class E
: T
; // expected-note {{here}}
268 enum class E
: int { e
}; // expected-error {{different underlying}}
273 int n
= g
<short>(); // expected-note {{here}}
277 // This should compile cleanly
284 template<typename T
> struct A
{
290 template<typename T
> enum class A
<T
>::B::E
{ e
};
291 template class A
<int>;
293 struct B
{ enum class E
; };
294 template<typename T
> enum class B::E
{ e
}; // expected-error {{enumeration cannot be a template}}
299 A
f(A a
) { return -a
; } // expected-error {{invalid argument type 'A' to unary expression}}
304 bool f() { return !A::A
; } // expected-error {{invalid argument type 'PR18551::A' to unary expression}}
307 namespace rdar15124329
{
308 enum class B
: bool { F
, T
};
310 const rdar15124329::B T1
= B::T
;
311 typedef B C
; const C T2
= B::T
;
313 static_assert(T1
!= B::F
, "");
314 static_assert(T2
== B::T
, "");
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'}}
328 using E::a
; // expected-warning {{using declaration naming a scoped enumerator is a C++20 extension}}
335 E2
f1() { return E::a
; }
337 bool f() { return !f1(); } // expected-error {{invalid argument type 'E2' (aka 'test11::E') to unary expression}}
341 enum C
{ R
=-1, G
, B
};
342 enum B
{ F
= (enum C
) -1, T
}; // this should compile cleanly, it used to assert.
346 // Check that clang rejects this code without crashing in c++17.
350 B b
{a
}; // expected-error {{cannot initialize}}