Debian Codegraph @

clang/test/SemaTemplate/instantiate-self.cpp

raw
1 // RUN: %clang_cc1 -std=c++1z -verify -pedantic-errors %s
2
3 // Check that we deal with cases where the instantiation of a class template
4 // recursively requires the instantiation of the same template.
5 namespace test1 {
6 template<typename T> struct A {
7 struct B { // expected-note {{not complete until the closing '}'}}
8 B b; // expected-error {{has incomplete type 'B'}}
9 };
10 B b; // expected-note {{in instantiation of}}
11 };
12 A<int> a; // expected-note {{in instantiation of}}
13 }
14
15 namespace test2 {
16 template<typename T> struct A {
17 struct B {
18 struct C {};
19 char c[1 + C()]; // expected-error {{invalid operands to binary expression}}
20 friend constexpr int operator+(int, C) { return 4; }
21 };
22 B b; // expected-note {{in instantiation of}}
23 };
24 A<int> a; // expected-note {{in instantiation of}}
25 }
26
27 namespace test3 {
28 // PR12317
29 template<typename T> struct A {
30 struct B {
31 enum { Val = 1 };
32 char c[1 + Val]; // ok
33 };
34 B b;
35 };
36 A<int> a;
37 }
38
39 namespace test4 {
40 template<typename T> struct M { typedef int type; };
41 template<typename T> struct A {
42 struct B { // expected-note {{not complete until the closing '}'}}
43 int k[typename A<typename M<T>::type>::B().k[0] + 1]; // expected-error {{incomplete type}}
44 };
45 B b; // expected-note {{in instantiation of}}
46 };
47 A<int> a; // expected-note {{in instantiation of}}
48 }
49
50 // PR12298: Recursive constexpr function template instantiation leads to
51 // stack overflow.
52 namespace test5 {
53 template<typename T> struct A {
54 constexpr T f(T k) { return g(k); }
55 constexpr T g(T k) {
56 return k ? f(k-1)+1 : 0;
57 }
58 };
59 constexpr int x = A<int>().f(5); // ok
60 }
61
62 namespace test6 {
63 template<typename T> constexpr T f(T);
64 template<typename T> constexpr T g(T t) {
65 // FIXME: It'd be nice to say that the function is currently being defined, rather than being undefined.
66 typedef int arr[f(T())]; // expected-error {{variable length array}} expected-note {{undefined function 'f<int>'}}
67 return t;
68 }
69 template<typename T> constexpr T f(T t) { // expected-note {{declared here}}
70 typedef int arr[g(T())]; // expected-error {{zero size array}} expected-note {{instantiation of}}
71 return t;
72 }
73 int n = f(0); // expected-note 2{{instantiation of}}
74 }
75
76 namespace test7 {
77 template<typename T> constexpr T g(T t) {
78 return t;
79 }
80 template<typename T> constexpr T f(T t) {
81 typedef int arr[g(T() + 1)];
82 return t;
83 }
84 int n = f(0);
85 }
86
87 namespace test8 {
88 template<typename T> struct A {
89 int n = A{}.n; // expected-error {{default member initializer for 'n' uses itself}}
90 };
91 A<int> ai = {}; // expected-note {{instantiation of default member init}}
92 }
93
94 namespace test9 {
95 template<typename T> struct A { enum class B; };
96 // FIXME: It'd be nice to give the "it has not yet been instantiated" diagnostic here.
97 template<typename T> enum class A<T>::B { k = A<T>::B::k2, k2 = k }; // expected-error {{no member named 'k2'}}
98 auto k = A<int>::B::k; // expected-note {{in instantiation of}}
99 }
100
101 namespace test10 {
102 template<typename T> struct A {
103 void f() noexcept(noexcept(f())); // expected-error {{exception specification of 'f' uses itself}}
104 };
105 bool b = noexcept(A<int>().f()); // expected-note {{instantiation of}}
106 }
107
108 namespace test11 {
109 template<typename T> const int var = var<T>;
110 int k = var<int>;
111
112 template<typename T> struct X {
113 static const int b = false;
114 static const int k = X<T>::b ? X<T>::k : 0;
115 };
116 template<typename T> const int X<T>::k;
117 int q = X<int>::k;
118
119 template<typename T> struct Y {
120 static const int k;
121 };
122 // OK (but not constant initialization).
123 template<typename T> const int Y<T>::k = Y<T>::k;
124 int r = Y<int>::k;
125 }
126
127 namespace test12 {
128 template<typename T> int f(T t, int = f(T())) {} // expected-error {{recursive evaluation of default argument}}
129 struct X {};
130 int q = f(X()); // expected-note {{instantiation of}}
131 }
132
133 namespace test13 {
134 struct A {
135 // Cycle via type of non-type template parameter.
136 template<typename T, typename T::template W<T>::type U = 0> struct W { using type = int; };
137 // Cycle via default template argument.
138 template<typename T, typename U = typename T::template X<T>> struct X {};
139 template<typename T, int U = T::template Y<T>::value> struct Y { static const int value = 0; };
140 template<typename T, template<typename> typename U = T::template Z<T>::template nested> struct Z { template<typename> struct nested; };
141 };
142 template<typename T> struct Wrap {
143 template<typename U> struct W : A::W<T> {};
144 template<typename U> struct X : A::X<T> {};
145 template<typename U> struct Y : A::Y<T> {};
146 template<typename U> struct Z : A::Z<T> {};
147 };
148 struct B {
149 template<typename U> struct W { using type = int; };
150 template<typename U> struct X {};
151 template<typename U> struct Y { static const int value = 0; };
152 template<typename U> struct Z { template<typename> struct nested; };
153 };
154
155 A::W<B> awb;
156 A::X<B> axb;
157 A::Y<B> ayb;
158 A::Z<B> azb;
159
160 A::W<Wrap<Wrap<B>>> awwwb;
161 A::X<Wrap<Wrap<B>>> axwwb;
162 A::Y<Wrap<Wrap<B>>> aywwb;
163 A::Z<Wrap<Wrap<B>>> azwwb;
164
165 // FIXME: These tests cause us to use too much stack and crash on a self-hosted debug build.
166 // FIXME: Check for recursion here and give a better diagnostic.
167 #if 0
168 A::W<A> awa;
169 A::X<A> axa;
170 A::Y<A> aya;
171 A::Z<A> aza;
172 #endif
173 }
174
175 namespace test14 {
176 template <class> void f();
177 template <class T, decltype(new (f<void>()) T)> T x;
178 }
179
180 namespace test15 {
181 template <class V> void __overload(V);
182
183 template <class, class> struct __invoke_result_impl;
184 template <class _Arg>
185 struct __invoke_result_impl<decltype(__overload(*(_Arg*)0)),
186 _Arg>;
187 struct variant {
188 template <class _Arg,
189 class = typename __invoke_result_impl<void, _Arg>::type>
190 variant(_Arg);
191 };
192 struct Matcher {
193 Matcher(variant);
194 } vec(vec); // expected-warning {{uninitialized}}
195 } // namespace test15