xref: /aosp_15_r20/external/clang/test/SemaCXX/enum-unscoped-nonexistent.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li struct Base {
4*67e74705SXin Li   static const int a = 1;
5*67e74705SXin Li };
6*67e74705SXin Li template<typename T> struct S : Base {
7*67e74705SXin Li   enum E : int;
8*67e74705SXin Li   constexpr int f() const;
9*67e74705SXin Li   constexpr int g() const;
10*67e74705SXin Li   void h();
11*67e74705SXin Li };
12*67e74705SXin Li template<> enum S<char>::E : int {}; // expected-note {{enum 'S<char>::E' was explicitly specialized here}}
13*67e74705SXin Li template<> enum S<short>::E : int { b = 2 };
14*67e74705SXin Li template<> enum S<int>::E : int { a = 4 };
15*67e74705SXin Li template<typename T> enum S<T>::E : int { b = 8 };
16*67e74705SXin Li 
17*67e74705SXin Li // The unqualified-id here names a member of the non-dependent base class Base
18*67e74705SXin Li // and not the injected enumerator name 'a' from the specialization.
f() const19*67e74705SXin Li template<typename T> constexpr int S<T>::f() const { return a; }
20*67e74705SXin Li static_assert(S<char>().f() == 1, "");
21*67e74705SXin Li static_assert(S<int>().f() == 1, "");
22*67e74705SXin Li 
23*67e74705SXin Li // The unqualified-id here names a member of the current instantiation, which
24*67e74705SXin Li // bizarrely might not exist in some instantiations.
g() const25*67e74705SXin Li template<typename T> constexpr int S<T>::g() const { return b; } // expected-error {{enumerator 'b' does not exist in instantiation of 'S<char>'}}
26*67e74705SXin Li static_assert(S<char>().g() == 1, ""); // expected-note {{here}} expected-error {{not an integral constant expression}}
27*67e74705SXin Li static_assert(S<short>().g() == 2, "");
28*67e74705SXin Li static_assert(S<long>().g() == 8, "");
29*67e74705SXin Li 
30*67e74705SXin Li // 'b' is type-dependent, so these assertions should not fire before 'h' is
31*67e74705SXin Li // instantiated.
h()32*67e74705SXin Li template<typename T> void S<T>::h() {
33*67e74705SXin Li   char c[S<T>::b];
34*67e74705SXin Li   static_assert(b != 8, "");
35*67e74705SXin Li   static_assert(sizeof(c) != 8, "");
36*67e74705SXin Li }
f()37*67e74705SXin Li void f() {
38*67e74705SXin Li   S<short>().h(); // ok, b == 2
39*67e74705SXin Li }
40