comparison clang/test/SemaTemplate/instantiate-self.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
comparison
equal deleted inserted replaced
147:c2174574ed3a 150:1d019706d866
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 'test1::A<int>::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 typedef int arr[f(T())]; // expected-error {{variable length array}}
66 return t;
67 }
68 template<typename T> constexpr T f(T t) {
69 typedef int arr[g(T())]; // expected-error {{zero size array}} expected-note {{instantiation of}}
70 return t;
71 }
72 int n = f(0); // expected-note 2{{instantiation of}}
73 }
74
75 namespace test7 {
76 template<typename T> constexpr T g(T t) {
77 return t;
78 }
79 template<typename T> constexpr T f(T t) {
80 typedef int arr[g(T() + 1)];
81 return t;
82 }
83 int n = f(0);
84 }
85
86 namespace test8 {
87 template<typename T> struct A {
88 int n = A{}.n; // expected-error {{default member initializer for 'n' uses itself}} expected-note {{instantiation of default member init}}
89 };
90 A<int> ai = {}; // expected-note {{instantiation of default member init}}
91 }
92
93 namespace test9 {
94 template<typename T> struct A { enum class B; };
95 // FIXME: It'd be nice to give the "it has not yet been instantiated" diagnostic here.
96 template<typename T> enum class A<T>::B { k = A<T>::B::k2, k2 = k }; // expected-error {{no member named 'k2'}}
97 auto k = A<int>::B::k; // expected-note {{in instantiation of}}
98 }
99
100 namespace test10 {
101 template<typename T> struct A {
102 void f() noexcept(noexcept(f())); // expected-error {{exception specification of 'f' uses itself}} expected-note {{instantiation of}}
103 };
104 bool b = noexcept(A<int>().f()); // expected-note {{instantiation of}}
105 }
106
107 namespace test11 {
108 template<typename T> const int var = var<T>;
109 int k = var<int>;
110
111 template<typename T> struct X {
112 static const int b = false;
113 static const int k = X<T>::b ? X<T>::k : 0;
114 };
115 template<typename T> const int X<T>::k;
116 int q = X<int>::k;
117
118 template<typename T> struct Y {
119 static const int k;
120 };
121 // OK (but not constant initialization).
122 template<typename T> const int Y<T>::k = Y<T>::k;
123 int r = Y<int>::k;
124 }
125
126 namespace test12 {
127 template<typename T> int f(T t, int = f(T())) {} // expected-error {{recursive evaluation of default argument}} expected-note {{instantiation of}}
128 struct X {};
129 int q = f(X()); // expected-note {{instantiation of}}
130 }
131
132 namespace test13 {
133 struct A {
134 // Cycle via type of non-type template parameter.
135 template<typename T, typename T::template W<T>::type U = 0> struct W { using type = int; };
136 // Cycle via default template argument.
137 template<typename T, typename U = typename T::template X<T>> struct X {};
138 template<typename T, int U = T::template Y<T>::value> struct Y { static const int value = 0; };
139 template<typename T, template<typename> typename U = T::template Z<T>::template nested> struct Z { template<typename> struct nested; };
140 };
141 template<typename T> struct Wrap {
142 template<typename U> struct W : A::W<T> {};
143 template<typename U> struct X : A::X<T> {};
144 template<typename U> struct Y : A::Y<T> {};
145 template<typename U> struct Z : A::Z<T> {};
146 };
147 struct B {
148 template<typename U> struct W { using type = int; };
149 template<typename U> struct X {};
150 template<typename U> struct Y { static const int value = 0; };
151 template<typename U> struct Z { template<typename> struct nested; };
152 };
153
154 A::W<B> awb;
155 A::X<B> axb;
156 A::Y<B> ayb;
157 A::Z<B> azb;
158
159 A::W<Wrap<Wrap<B>>> awwwb;
160 A::X<Wrap<Wrap<B>>> axwwb;
161 A::Y<Wrap<Wrap<B>>> aywwb;
162 A::Z<Wrap<Wrap<B>>> azwwb;
163
164 // FIXME: These tests cause us to use too much stack and crash on a self-hosted debug build.
165 // FIXME: Check for recursion here and give a better diagnostic.
166 #if 0
167 A::W<A> awa;
168 A::X<A> axa;
169 A::Y<A> aya;
170 A::Z<A> aza;
171 #endif
172 }