1 // RUN: clang-cc -fsyntax-only -verify %s 2 template<typename T, typename U> 3 struct X0 { 4 void f(T x, U y) { 5 (void)(x + y); // expected-error{{invalid operands}} 6 } 7 }; 8 9 struct X1 { }; 10 11 template struct X0<int, float>; 12 template struct X0<int*, int>; 13 template struct X0<int X1::*, int>; // expected-note{{instantiation of}} 14 15 template<typename T> 16 struct X2 { 17 void f(T); 18 19 T g(T x, T y) { 20 /* DeclStmt */; 21 T *xp = &x, &yr = y; // expected-error{{pointer to a reference}} 22 /* NullStmt */; 23 } 24 }; 25 26 template struct X2<int>; 27 template struct X2<int&>; // expected-note{{instantiation of}} 28 29 template<typename T> 30 struct X3 { 31 void f(T) { 32 Label: 33 T x; 34 goto Label; 35 } 36 }; 37 38 template struct X3<int>; 39 40 template <typename T> struct X4 { 41 T f() const { 42 return; // expected-warning{{non-void function 'f' should return a value}} 43 } 44 45 T g() const { 46 return 1; // expected-warning{{void function 'g' should not return a value}} 47 } 48 }; 49 50 template struct X4<void>; // expected-note{{in instantiation of}} 51 template struct X4<int>; // expected-note{{in instantiation of}} 52 53 struct Incomplete; // expected-note 2{{forward declaration}} 54 55 template<typename T> struct X5 { 56 T f() { } // expected-error{{incomplete result type}} 57 }; 58 void test_X5(X5<Incomplete> x5); // okay! 59 60 template struct X5<Incomplete>; // expected-note{{instantiation}} 61 62 template<typename T, typename U, typename V> struct X6 { 63 U f(T t, U u, V v) { 64 // IfStmt 65 if (t > 0) 66 return u; 67 else { 68 if (t < 0) 69 return v; // expected-error{{incompatible type}} 70 } 71 72 if (T x = t) { 73 t = x; 74 } 75 return v; 76 } 77 }; 78 79 struct ConvertibleToInt { 80 operator int() const; 81 }; 82 83 template struct X6<ConvertibleToInt, float, char>; 84 template struct X6<bool, int, int*>; // expected-note{{instantiation}} 85 86 template <typename T> struct X7 { 87 void f() { 88 void *v = this; 89 } 90 }; 91 92 template struct X7<int>; 93 94 template<typename T> struct While0 { 95 void f(T t) { 96 while (t) { 97 } 98 99 while (T t2 = T()) ; 100 } 101 }; 102 103 template struct While0<float>; 104 105 template<typename T> struct Do0 { 106 void f(T t) { 107 do { 108 } while (t); // expected-error{{not contextually}} 109 110 do { 111 } while (T t2 = T()); 112 } 113 }; 114 115 struct NotConvertibleToBool { }; 116 template struct Do0<ConvertibleToInt>; 117 template struct Do0<NotConvertibleToBool>; // expected-note{{instantiation}} 118 119 template<typename T> struct For0 { 120 void f(T f, T l) { 121 for (; f != l; ++f) { 122 if (*f) 123 continue; 124 else if (*f == 17) 125 break; 126 } 127 } 128 }; 129 130 template struct For0<int*>; 131 132 template<typename T> struct Member0 { 133 void f(T t) { 134 t; 135 t.f; 136 t->f; 137 138 T* tp; 139 tp.f; // expected-error{{member reference base type 'T *' is not a structure or union}} 140 tp->f; 141 142 this->f; 143 this.f; // expected-error{{member reference base type 'Member0<T> *const' is not a structure or union}} 144 } 145 }; 146 147 template<typename T, typename U> struct Switch0 { 148 U f(T value, U v0, U v1, U v2) { 149 switch (value) { 150 case 0: return v0; 151 152 case 1: return v1; 153 154 case 2: // fall through 155 156 default: 157 return v2; 158 } 159 } 160 }; 161 162 template struct Switch0<int, float>; 163 164 template<typename T, int I1, int I2> struct Switch1 { 165 T f(T x, T y, T z) { 166 switch (x) { 167 case I1: return y; // expected-note{{previous}} 168 case I2: return z; // expected-error{{duplicate}} 169 default: return x; 170 } 171 } 172 }; 173 174 template struct Switch1<int, 1, 2>; 175 template struct Switch1<int, 2, 2>; // expected-note{{instantiation}} 176 177 template<typename T> struct IndirectGoto0 { 178 void f(T x) { 179 // FIXME: crummy error message below 180 goto *x; // expected-error{{incompatible}} 181 182 prior: 183 T prior_label = &&prior; 184 185 T later_label = &&later; 186 187 later: 188 (void)(1+1); 189 } 190 }; 191 192 template struct IndirectGoto0<void*>; 193 template struct IndirectGoto0<int>; // expected-note{{instantiation}} 194 195 template<typename T> struct TryCatch0 { 196 void f() { 197 try { 198 } catch (T t) { // expected-error{{incomplete type}} \ 199 // expected-error{{abstract class}} 200 } catch (...) { 201 } 202 } 203 }; 204 205 struct Abstract { 206 virtual void foo() = 0; // expected-note{{pure virtual}} 207 }; 208 209 template struct TryCatch0<int>; // okay 210 template struct TryCatch0<Incomplete*>; // expected-note{{instantiation}} 211 template struct TryCatch0<Abstract>; // expected-note{{instantiation}} 212 213 // PR4383 214 template<typename T> struct X; 215 template<typename T> struct Y : public X<T> { 216 Y& x() { return *this; } 217 }; 218