instantiate-function-1.cpp 5.3 KB

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