Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fsyntax-only -faccess-control -verify %s -std=c++0x |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 2 | namespace T1 { |
| 3 | |
| 4 | class A { |
| 5 | virtual int f(); // expected-note{{overridden virtual function is here}} |
| 6 | }; |
| 7 | |
| 8 | class B : A { |
| 9 | virtual void f(); // expected-error{{virtual function 'f' has a different return type ('void') than the function it overrides (which has return type 'int')}} |
| 10 | }; |
| 11 | |
| 12 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 13 | |
| 14 | namespace T2 { |
| 15 | |
| 16 | struct a { }; |
| 17 | struct b { }; |
| 18 | |
| 19 | class A { |
| 20 | virtual a* f(); // expected-note{{overridden virtual function is here}} |
| 21 | }; |
| 22 | |
| 23 | class B : A { |
Anders Carlsson | b5133c2 | 2009-05-14 21:20:16 +0000 | [diff] [blame] | 24 | virtual b* f(); // expected-error{{return type of virtual function 'f' is not covariant with the return type of the function it overrides ('struct T2::b *' is not derived from 'struct T2::a *')}} |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | } |
| 28 | |
| 29 | namespace T3 { |
| 30 | |
| 31 | struct a { }; |
| 32 | struct b : private a { }; // expected-note{{'private' inheritance specifier here}} |
| 33 | |
| 34 | class A { |
| 35 | virtual a* f(); // expected-note{{overridden virtual function is here}} |
| 36 | }; |
| 37 | |
| 38 | class B : A { |
| 39 | virtual b* f(); // expected-error{{return type of virtual function 'f' is not covariant with the return type of the function it overrides (conversion from 'struct T3::b' to inaccessible base class 'struct T3::a')}} |
| 40 | }; |
| 41 | |
| 42 | } |
| 43 | |
| 44 | namespace T4 { |
| 45 | |
| 46 | struct a { }; |
| 47 | struct a1 : a { }; |
| 48 | struct b : a, a1 { }; |
| 49 | |
| 50 | class A { |
| 51 | virtual a* f(); // expected-note{{overridden virtual function is here}} |
| 52 | }; |
| 53 | |
| 54 | class B : A { |
| 55 | virtual b* f(); // expected-error{{return type of virtual function 'f' is not covariant with the return type of the function it overrides (ambiguous conversion from derived class 'struct T4::b' to base class 'struct T4::a':\n\ |
| 56 | struct T4::b -> struct T4::a\n\ |
| 57 | struct T4::b -> struct T4::a1 -> struct T4::a)}} |
| 58 | }; |
| 59 | |
| 60 | } |
| 61 | |
| 62 | namespace T5 { |
| 63 | |
| 64 | struct a { }; |
| 65 | |
| 66 | class A { |
| 67 | virtual a* const f(); |
| 68 | virtual a* const g(); // expected-note{{overridden virtual function is here}} |
| 69 | }; |
| 70 | |
| 71 | class B : A { |
| 72 | virtual a* const f(); |
| 73 | virtual a* g(); // expected-error{{return type of virtual function 'g' is not covariant with the return type of the function it overrides ('struct T5::a *' has different qualifiers than 'struct T5::a *const')}} |
| 74 | }; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | namespace T6 { |
| 79 | |
| 80 | struct a { }; |
| 81 | |
| 82 | class A { |
| 83 | virtual const a* f(); |
| 84 | virtual a* g(); // expected-note{{overridden virtual function is here}} |
| 85 | }; |
| 86 | |
| 87 | class B : A { |
| 88 | virtual a* f(); |
| 89 | virtual const a* g(); // expected-error{{return type of virtual function 'g' is not covariant with the return type of the function it overrides (class type 'struct T6::a const *' is more qualified than class type 'struct T6::a *'}} |
| 90 | }; |
| 91 | |
| 92 | } |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 93 | |
| 94 | namespace T7 { |
| 95 | struct a { }; |
| 96 | struct b { }; |
| 97 | |
| 98 | class A { |
| 99 | a* f(); |
| 100 | }; |
| 101 | |
| 102 | class B : A { |
| 103 | virtual b* f(); |
| 104 | }; |
| 105 | } |
Douglas Gregor | d3a5058 | 2009-12-01 16:18:00 +0000 | [diff] [blame] | 106 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 107 | namespace T8 { |
| 108 | struct a { }; |
| 109 | struct b; // expected-note {{forward declaration of 'struct T8::b'}} |
| 110 | |
| 111 | class A { |
| 112 | virtual a *f(); |
| 113 | }; |
| 114 | |
| 115 | class B : A { |
| 116 | b* f(); // expected-error {{return type of virtual function 'f' is not covariant with the return type of the function it overrides ('struct T8::b' is incomplete)}} |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | namespace T9 { |
| 121 | struct a { }; |
| 122 | |
| 123 | template<typename T> struct b : a { |
| 124 | int a[sizeof(T) ? -1 : -1]; // expected-error {{array size is negative}} |
| 125 | }; |
| 126 | |
| 127 | class A { |
| 128 | virtual a *f(); |
| 129 | }; |
| 130 | |
| 131 | class B : A { |
| 132 | virtual b<int> *f(); // expected-note {{in instantiation of template class 'struct T9::b<int>' requested here}} |
| 133 | }; |
| 134 | } |
| 135 | |
Douglas Gregor | d3a5058 | 2009-12-01 16:18:00 +0000 | [diff] [blame] | 136 | // PR5656 |
| 137 | class X0 { |
| 138 | virtual void f0(); |
| 139 | }; |
| 140 | class X1 : public X0 { |
| 141 | void f0() = 0; |
| 142 | }; |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 143 | |
| 144 | template <typename Base> |
| 145 | struct Foo : Base { |
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 146 | void f(int) = 0; // expected-error{{not virtual and cannot be declared pure}} |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 149 | struct Base1 { virtual void f(int); }; |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 150 | struct Base2 { }; |
| 151 | |
| 152 | void test() { |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 153 | (void)sizeof(Foo<Base1>); |
| 154 | (void)sizeof(Foo<Base2>); // expected-note{{instantiation}} |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 155 | } |
Douglas Gregor | e6342c0 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 156 | |
| 157 | template<typename Base> |
| 158 | struct Foo2 : Base { |
| 159 | template<typename T> int f(T); |
| 160 | }; |
| 161 | |
| 162 | void test2() { |
| 163 | Foo2<Base1> f1; |
| 164 | Foo2<Base2> f2; |
| 165 | f1.f(17); |
| 166 | f2.f(17); |
| 167 | }; |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 168 | |
| 169 | struct Foo3 { |
| 170 | virtual void f(int) = 0; // expected-note{{pure virtual function}} |
| 171 | }; |
| 172 | |
| 173 | template<typename T> |
| 174 | struct Bar3 : Foo3 { |
| 175 | void f(T); |
| 176 | }; |
| 177 | |
| 178 | void test3() { |
| 179 | Bar3<int> b3i; // okay |
| 180 | Bar3<float> b3f; // expected-error{{is an abstract class}} |
| 181 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 182 | |
| 183 | // 5920 |
| 184 | namespace PR5920 { |
| 185 | class Base {}; |
| 186 | |
| 187 | template <typename T> |
| 188 | class Derived : public Base {}; |
| 189 | |
| 190 | class Foo { |
| 191 | public: |
| 192 | virtual Base* Method(); |
| 193 | }; |
| 194 | |
| 195 | class Bar : public Foo { |
| 196 | public: |
| 197 | virtual Derived<int>* Method(); |
| 198 | }; |
| 199 | } |
Chandler Carruth | ceb7e00 | 2010-01-22 13:07:41 +0000 | [diff] [blame] | 200 | |
| 201 | // Look through template types and typedefs to see whether return types are |
| 202 | // pointers or references. |
| 203 | namespace PR6110 { |
| 204 | class Base {}; |
| 205 | class Derived : public Base {}; |
| 206 | |
| 207 | typedef Base* BaseP; |
| 208 | typedef Derived* DerivedP; |
| 209 | |
| 210 | class X { virtual BaseP f(); }; |
| 211 | class X1 : public X { virtual DerivedP f(); }; |
| 212 | |
| 213 | template <typename T> class Y { virtual T f(); }; |
| 214 | template <typename T1, typename T> class Y1 : public Y<T> { virtual T1 f(); }; |
| 215 | Y1<Derived*, Base*> y; |
| 216 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame^] | 217 | |
| 218 | namespace T10 { |
| 219 | struct A { }; |
| 220 | struct B : A { }; |
| 221 | |
| 222 | struct C { |
| 223 | virtual A&& f(); |
| 224 | }; |
| 225 | |
| 226 | struct D : C { |
| 227 | virtual B&& f(); |
| 228 | }; |
| 229 | }; |
| 230 | |
| 231 | namespace T11 { |
| 232 | struct A { }; |
| 233 | struct B : A { }; |
| 234 | |
| 235 | struct C { |
| 236 | virtual A& f(); // expected-note {{overridden virtual function is here}} |
| 237 | }; |
| 238 | |
| 239 | struct D : C { |
| 240 | virtual B&& f(); // expected-error {{virtual function 'f' has a different return type ('struct T11::B &&') than the function it overrides (which has return type 'struct T11::A &')}} |
| 241 | }; |
| 242 | }; |
| 243 | |
| 244 | namespace T12 { |
| 245 | struct A { }; |
| 246 | struct B : A { }; |
| 247 | |
| 248 | struct C { |
| 249 | virtual A&& f(); // expected-note {{overridden virtual function is here}} |
| 250 | }; |
| 251 | |
| 252 | struct D : C { |
| 253 | virtual B& f(); // expected-error {{virtual function 'f' has a different return type ('struct T12::B &') than the function it overrides (which has return type 'struct T12::A &&')}} |
| 254 | }; |
| 255 | }; |