Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fsyntax-only -verify %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 3 | // RUN: %clang_cc1 -fsyntax-only -verify -std=gnu++1z %s |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4 | |
| 5 | |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 6 | |
| 7 | // Errors |
| 8 | export class foo { }; // expected-error {{expected template}} |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9 | template x; // expected-error {{C++ requires a type specifier for all declarations}} \ |
| 10 | // expected-error {{does not refer}} |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 11 | export template x; // expected-error {{expected '<' after 'template'}} |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 12 | export template<class T> class x0; // expected-warning {{exported templates are unsupported}} |
David Blaikie | b031eab | 2012-04-06 23:33:59 +0000 | [diff] [blame] | 13 | template < ; // expected-error {{expected template parameter}} \ |
Douglas Gregor | 99ea734 | 2010-10-15 01:15:58 +0000 | [diff] [blame] | 14 | // expected-error{{expected ',' or '>' in template-parameter-list}} \ |
| 15 | // expected-warning {{declaration does not declare anything}} |
David Blaikie | eb52f86a | 2012-04-09 16:37:11 +0000 | [diff] [blame] | 16 | template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}} |
| 17 | |
| 18 | // verifies that we only walk to the ',' & still produce errors on the rest of the template parameters |
| 19 | template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \ |
| 20 | expected-error {{expected unqualified-id}} |
| 21 | template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \ |
| 22 | expected-error {{template template parameter requires 'class' after the parameter list}} |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 23 | template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \ |
| 24 | // expected-error{{extraneous}} |
David Blaikie | 673720d | 2012-04-06 06:28:32 +0000 | [diff] [blame] | 25 | template <template <typename> > struct Err2; // expected-error {{template template parameter requires 'class' after the parameter list}} |
| 26 | template <template <typename> Foo> struct Err3; // expected-error {{template template parameter requires 'class' after the parameter list}} |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 27 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 28 | template <template <typename> typename Foo> struct Cxx1z; |
| 29 | #if __cplusplus <= 201402L |
| 30 | // expected-warning@-2 {{extension}} |
| 31 | #endif |
| 32 | |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 33 | // Template function declarations |
| 34 | template <typename T> void foo(); |
| 35 | template <typename T, typename U> void foo(); |
| 36 | |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 37 | // Template function definitions. |
| 38 | template <typename T> void foo() { } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 39 | |
| 40 | // Template class (forward) declarations |
| 41 | template <typename T> struct A; |
| 42 | template <typename T, typename U> struct b; |
| 43 | template <typename> struct C; |
| 44 | template <typename, typename> struct D; |
| 45 | |
| 46 | // Forward declarations with default parameters? |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 47 | template <typename T = int> class X1; |
| 48 | template <typename = int> class X2; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 49 | |
| 50 | // Forward declarations w/template template parameters |
| 51 | template <template <typename> class T> class TTP1; |
| 52 | template <template <typename> class> class TTP2; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 53 | template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}} |
| 54 | template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}} |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 55 | template <template <typename X, typename Y> class T> class TTP5; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 56 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 57 | // Forward declarations with non-type params |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 58 | template <int> class NTP0; |
| 59 | template <int N> class NTP1; |
| 60 | template <int N = 5> class NTP2; |
| 61 | template <int = 10> class NTP3; |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 62 | template <unsigned int N = 12u> class NTP4; |
| 63 | template <unsigned int = 12u> class NTP5; |
| 64 | template <unsigned = 15u> class NTP6; |
| 65 | template <typename T, T Obj> class NTP7; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 66 | |
| 67 | // Template class declarations |
| 68 | template <typename T> struct A { }; |
| 69 | template <typename T, typename U> struct B { }; |
| 70 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 71 | // Template parameter shadowing |
| 72 | template<typename T, // expected-note{{template parameter is declared here}} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | typename T> // expected-error{{declaration of 'T' shadows template parameter}} |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 74 | void shadow1(); |
| 75 | |
| 76 | template<typename T> // expected-note{{template parameter is declared here}} |
| 77 | void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}} |
| 78 | |
| 79 | template<typename T> // expected-note{{template parameter is declared here}} |
| 80 | class T { // expected-error{{declaration of 'T' shadows template parameter}} |
| 81 | }; |
| 82 | |
| 83 | template<int Size> // expected-note{{template parameter is declared here}} |
| 84 | void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}} |
| 85 | |
Douglas Gregor | c19ee3e | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 86 | // <rdar://problem/6952203> |
| 87 | template<typename T> // expected-note{{here}} |
| 88 | struct shadow4 { |
| 89 | int T; // expected-error{{shadows}} |
| 90 | }; |
| 91 | |
| 92 | template<typename T> // expected-note{{here}} |
| 93 | struct shadow5 { |
| 94 | int T(int, float); // expected-error{{shadows}} |
| 95 | }; |
| 96 | |
Richard Smith | c7e863f | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 97 | template<typename T, // expected-note{{template parameter is declared here}} |
| 98 | T T> // expected-error{{declaration of 'T' shadows template parameter}} |
| 99 | void shadow6(); |
| 100 | |
| 101 | template<typename T, // expected-note{{template parameter is declared here}} |
| 102 | template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}} |
| 103 | void shadow7(); |
| 104 | |
| 105 | // PR8302 |
| 106 | template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}} |
| 107 | template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}} |
| 108 | }; |
| 109 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 110 | // Non-type template parameters in scope |
| 111 | template<int Size> |
| 112 | void f(int& i) { |
| 113 | i = Size; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 114 | #ifdef DELAYED_TEMPLATE_PARSING |
| 115 | Size = i; |
| 116 | #else |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 117 | Size = i; // expected-error{{expression is not assignable}} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 118 | #endif |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | template<typename T> |
| 122 | const T& min(const T&, const T&); |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 123 | |
| 124 | void f2() { |
| 125 | int x; |
| 126 | A< typeof(x>1) > a; |
| 127 | } |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | // PR3844 |
| 131 | template <> struct S<int> { }; // expected-error{{explicit specialization of non-template struct 'S'}} |
Rafael Espindola | c4fb7ef | 2013-11-08 23:06:10 +0000 | [diff] [blame] | 132 | template <> union U<int> { }; // expected-error{{explicit specialization of non-template union 'U'}} |
Douglas Gregor | 1df0ee9 | 2010-02-05 07:07:10 +0000 | [diff] [blame] | 133 | |
| 134 | namespace PR6184 { |
| 135 | namespace N { |
| 136 | template <typename T> |
| 137 | void bar(typename T::x); |
| 138 | } |
| 139 | |
| 140 | template <typename T> |
| 141 | void N::bar(typename T::x) { } |
| 142 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 143 | |
| 144 | // This PR occurred only in template parsing mode. |
| 145 | namespace PR17637 { |
| 146 | template <int> |
| 147 | struct L { |
| 148 | template <typename T> |
| 149 | struct O { |
| 150 | template <typename U> |
| 151 | static void Fun(U); |
| 152 | }; |
| 153 | }; |
| 154 | |
| 155 | template <int k> |
| 156 | template <typename T> |
| 157 | template <typename U> |
| 158 | void L<k>::O<T>::Fun(U) {} |
| 159 | |
| 160 | void Instantiate() { L<0>::O<int>::Fun(0); } |
| 161 | |
| 162 | } |
| 163 | |
| 164 | namespace explicit_partial_specializations { |
| 165 | typedef char (&oneT)[1]; |
| 166 | typedef char (&twoT)[2]; |
| 167 | typedef char (&threeT)[3]; |
| 168 | typedef char (&fourT)[4]; |
| 169 | typedef char (&fiveT)[5]; |
| 170 | typedef char (&sixT)[6]; |
| 171 | |
| 172 | char one[1]; |
| 173 | char two[2]; |
| 174 | char three[3]; |
| 175 | char four[4]; |
| 176 | char five[5]; |
| 177 | char six[6]; |
| 178 | |
| 179 | template<bool b> struct bool_ { typedef int type; }; |
| 180 | template<> struct bool_<false> { }; |
| 181 | |
| 182 | #define XCAT(x,y) x ## y |
| 183 | #define CAT(x,y) XCAT(x,y) |
| 184 | #define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__); |
| 185 | |
| 186 | |
| 187 | template <int> |
| 188 | struct L { |
| 189 | template <typename T> |
| 190 | struct O { |
| 191 | template <typename U> |
| 192 | static oneT Fun(U); |
| 193 | |
| 194 | }; |
| 195 | }; |
| 196 | template <int k> |
| 197 | template <typename T> |
| 198 | template <typename U> |
| 199 | oneT L<k>::O<T>::Fun(U) { return one; } |
| 200 | |
| 201 | template<> |
| 202 | template<> |
| 203 | template<typename U> |
| 204 | oneT L<0>::O<char>::Fun(U) { return one; } |
| 205 | |
| 206 | |
| 207 | void Instantiate() { |
| 208 | sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one)); |
| 209 | sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one)); |
| 210 | } |
| 211 | |
| 212 | } |