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'}}
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}}
69 e1
= false, e2
= true,
70 e3
// expected-error{{2 is not representable in the underlying}}
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}}
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}}
114 long_enum_val
= 10000
117 enum : long x
; // expected-error{{unnamed enumeration must be a definition}}
120 enum class scoped_enum
{ yes
, no
, maybe
};
121 scoped_enum e
= scoped_enum::yes
;
122 if (e
== scoped_enum::no
) { }
125 // <rdar://problem/9366066>
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 enum A::b
; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
151 int A::c
; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
152 void A::d(); // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
154 (void) A::e
; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
160 enum class test1
{ owner_dead
= val
, };
164 enum class E
*x0a
; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
166 enum class E
{ a
, b
};
167 enum E x1
= E::a
; // ok
168 enum class E x2
= E::a
; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
172 enum class F y2
= a
; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
175 friend enum class E
; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
176 friend enum class F
; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
178 friend enum G
{}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
179 friend enum class H
{}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
180 friend enum I
: int {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
191 enum class N2764::B
{};
194 template<typename E
> struct Enum
{
195 Enum() : m_e(E::Last
) {}
199 enum eCOLORS
{ Last
};
204 enum class E
{ e
= (struct S
*)0 == (struct S
*)0 };
209 template<typename T
> struct S
{
210 enum A
: int; // expected-note {{here}}
211 enum class B
; // expected-note {{here}}
212 enum class C
: int; // expected-note {{here}}
213 enum class D
: int; // expected-note {{here}}
215 template<typename T
> enum S
<T
>::A
{ a
}; // expected-error {{previously declared with fixed underlying type}}
216 template<typename T
> enum class S
<T
>::B
: char { b
}; // expected-error {{redeclared with different underlying}}
217 template<typename T
> enum S
<T
>::C
: int { c
}; // expected-error {{previously declared as scoped}}
218 template<typename T
> enum class S
<T
>::D
: char { d
}; // expected-error {{redeclared with different underlying}}
222 template<typename T
> struct S
{
223 enum class ET
: T
; // expected-note 2{{here}}
224 enum class Eint
: int; // expected-note 2{{here}}
226 template<> enum class S
<int>::ET
: int {};
227 template<> enum class S
<char>::ET
: short {}; // expected-error {{different underlying type}}
228 template<> enum class S
<int>::Eint
: short {}; // expected-error {{different underlying type}}
229 template<> enum class S
<char>::Eint
: int {};
231 template<typename T
> enum class S
<T
>::ET
: int {}; // expected-error {{different underlying type 'int' (was 'short')}}
232 template<typename T
> enum class S
<T
>::Eint
: T
{}; // expected-error {{different underlying type 'short' (was 'int')}}
234 // The implicit instantiation of S<short> causes the implicit instantiation of
235 // all declarations of member enumerations, so is ill-formed, even though we
236 // never instantiate the definitions of S<short>::ET nor S<short>::Eint.
237 S
<short> s
; // expected-note {{in instantiation of}}
241 template<typename T
> int f() {
243 enum E
: T
; // expected-note {{here}}
245 enum E
: int { e
}; // expected-error {{different underlying}}
250 int l
= f
<short>(); // expected-note {{here}}
252 template<typename T
> int g() {
254 enum class E
: T
; // expected-note {{here}}
256 enum class E
: int { e
}; // expected-error {{different underlying}}
261 int n
= g
<short>(); // expected-note {{here}}
265 // This should compile cleanly
272 template<typename T
> struct A
{
278 template<typename T
> enum class A
<T
>::B::E
{ e
};
279 template class A
<int>;
281 struct B
{ enum class E
; };
282 template<typename T
> enum class B::E
{ e
}; // expected-error {{enumeration cannot be a template}}
287 A
f(A a
) { return -a
; } // expected-error {{invalid argument type 'A' to unary expression}}
292 bool f() { return !A::A
; } // expected-error {{invalid argument type 'PR18551::A' to unary expression}}
295 namespace rdar15124329
{
296 enum class B
: bool { F
, T
};
298 const rdar15124329::B T1
= B::T
;
299 typedef B C
; const C T2
= B::T
;
301 static_assert(T1
!= B::F
, "");
302 static_assert(T2
== B::T
, "");
308 int E::e
= 0; // expected-error {{does not refer into a class}}
309 void E::f() {} // expected-error {{does not refer into a class}}
310 struct E::S
{}; // expected-error {{no struct named 'S'}}
311 struct T
: E::S
{}; // expected-error {{expected class name}}
312 enum E::E
{}; // expected-error {{no enum named 'E'}}
313 int E::*p
; // expected-error {{does not point into a class}}
314 using E::f
; // expected-error {{no member named 'f'}}
316 using E::a
; // expected-warning {{using declaration naming a scoped enumerator is a C++20 extension}}
323 E2
f1() { return E::a
; }
325 bool f() { return !f1(); } // expected-error {{invalid argument type 'E2' (aka 'test11::E') to unary expression}}
329 enum C
{ R
=-1, G
, B
};
330 enum B
{ F
= (enum C
) -1, T
}; // this should compile cleanly, it used to assert.
334 // Check that clang rejects this code without crashing in c++17.
338 B b
{a
}; // expected-error {{cannot initialize}}