[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / using-decl-1.cpp
blobb4477e2c6245b692124742b5eb94bc3f6d30e790
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4 extern "C" { void f(bool); }
6 namespace std {
7 using ::f;
8 inline void f() { return f(true); }
11 namespace M {
12 void f(float);
15 namespace N {
16 using M::f;
17 void f(int) { } // expected-note{{previous}}
19 void f(int) { } // expected-error{{redefinition}}
22 namespace N {
23 void f(double);
24 void f(long);
27 struct X0 {
28 void operator()(int);
29 void operator()(long);
32 struct X1 : X0 {
33 void operator()(float&);
34 using X0::operator();
36 void test() {
37 (*this)(1);
41 struct A { void f(); };
42 struct B : A { };
43 class C : B { using B::f; };
45 // PR5751: Resolve overloaded functions through using decls.
46 namespace O {
47 void f(int i);
48 void f(double d);
50 namespace P {
51 void f();
52 void g(void (*ptr)(int));
53 using O::f;
54 void test() {
55 f();
56 f(1);
57 void (*f_ptr1)(double) = f;
58 void (*f_ptr2)() = f;
59 g(f);
63 // Make sure that ADL can find names brought in by using decls.
64 namespace test0 {
65 namespace ns {
66 class Foo {};
68 namespace inner {
69 void foo(char *); // expected-note {{no known conversion}}
72 using inner::foo;
75 void test(ns::Foo *p) {
76 foo(*p); // expected-error {{no matching function for call to 'foo'}}
80 // Redeclarations!
81 namespace test1 {
82 namespace ns0 { struct Foo {}; }
83 namespace A { void foo(ns0::Foo *p, int y, int z); }
84 namespace ns2 { using A::foo; }
85 namespace ns1 { struct Bar : ns0::Foo {}; }
86 namespace A { void foo(ns0::Foo *p, int y, int z = 0); } // expected-note {{candidate}}
87 namespace ns1 { using A::foo; }
88 namespace ns2 { struct Baz : ns1::Bar {}; }
89 namespace A { void foo(ns0::Foo *p, int y = 0, int z); }
91 void test(ns2::Baz *p) {
92 foo(p, 0, 0); // okay!
93 foo(p, 0); // should be fine!
94 foo(p); // expected-error {{no matching function}}
98 namespace test2 {
99 namespace ns { int foo; }
100 template <class T> using ns::foo; // expected-error {{cannot template a using declaration}}
102 // PR8022
103 struct A {
104 template <typename T> void f(T);
106 class B : A {
107 template <typename T> using A::f<T>; // expected-error {{cannot template a using declaration}}
111 // PR8756
112 namespace foo
114 class Class1; // expected-note{{forward declaration}}
115 class Class2
117 using ::foo::Class1::Function; // expected-error{{incomplete type 'foo::Class1' named in nested name specifier}}
121 // Don't suggest non-typenames for positions requiring typenames.
122 namespace using_suggestion_tyname_val {
123 namespace N { void FFF() {} }
124 using typename N::FFG; // expected-error {{no member named 'FFG' in namespace 'using_suggestion_tyname_val::N'}}
127 namespace using_suggestion_member_tyname_val {
128 class CCC { public: void AAA() { } };
129 class DDD : public CCC { public: using typename CCC::AAB; }; // expected-error {{no member named 'AAB' in 'using_suggestion_member_tyname_val::CCC'}}
132 namespace using_suggestion_tyname_val_dropped_specifier {
133 void FFF() {}
134 namespace N { }
135 using typename N::FFG; // expected-error {{no member named 'FFG' in namespace 'using_suggestion_tyname_val_dropped_specifier::N'}}
138 // Currently hints aren't provided to drop out the incorrect M::.
139 namespace using_suggestion_ty_dropped_nested_specifier {
140 namespace N {
141 class AAA {}; // expected-note {{'N::AAA' declared here}}
142 namespace M { }
144 using N::M::AAA; // expected-error {{no member named 'AAA' in namespace 'using_suggestion_ty_dropped_nested_specifier::N::M'; did you mean 'N::AAA'?}}
147 namespace using_suggestion_tyname_ty_dropped_nested_specifier {
148 namespace N {
149 class AAA {}; // expected-note {{'N::AAA' declared here}}
150 namespace M { }
152 using typename N::M::AAA; // expected-error {{no member named 'AAA' in namespace 'using_suggestion_tyname_ty_dropped_nested_specifier::N::M'; did you mean 'N::AAA'?}}
155 namespace using_suggestion_val_dropped_nested_specifier {
156 namespace N {
157 void FFF() {} // expected-note {{'N::FFF' declared here}}
158 namespace M { }
160 using N::M::FFF; // expected-error {{no member named 'FFF' in namespace 'using_suggestion_val_dropped_nested_specifier::N::M'; did you mean 'N::FFF'?}}
163 namespace UsingDeclVsHiddenName {
164 namespace A {
165 enum HiddenTag1 {}; // expected-note {{previous use is here}}
166 enum HiddenTag2 {}; // expected-note {{target}}
167 int HiddenFn1; // expected-note {{target}}
168 int HiddenFn2; // expected-note {{target}}
169 int HiddenLocalExtern1;
170 int HiddenLocalExtern2;
173 namespace B {
174 using A::HiddenTag1;
175 using A::HiddenFn1; // expected-note {{using declaration}}
176 using A::HiddenLocalExtern1;
178 struct S {
179 friend struct HiddenTag1; // expected-error {{tag type that does not match previous}}
180 friend struct HiddenTag2; // expected-note {{conflicting declaration}}
181 friend void HiddenFn1(); // expected-error {{cannot befriend target of using declaration}}
182 friend void HiddenFn2(); // expected-note {{conflicting declaration}}
183 void f() {
184 // OK, these are not in the scope of namespace B, even though they're
185 // members of the namespace.
186 void HiddenLocalExtern1();
187 void HiddenLocalExtern2();
191 using A::HiddenTag2; // expected-error {{conflicts with declaration already in scope}}
192 using A::HiddenFn2; // expected-error {{conflicts with declaration already in scope}}
193 using A::HiddenLocalExtern2;
197 namespace PR19171 {
198 struct Z {
199 Z();
202 typedef struct {
203 Z i;
204 } S;
206 struct Y : S {
207 using S::S;
208 #if __cplusplus < 201103L
209 // expected-error@-2 {{no member named 'S' in 'PR19171::S'}}
210 #endif
213 // [namespace.udecl]p3: In a using-declaration used as a member-declaration,
214 // the nested-name-specifier shall name a base class of the class being defined.
215 // If such a using-declaration names a constructor, the nested-name-specifier
216 // shall name a direct base class of the class being defined;
218 struct B_blah { };
219 struct C_blah : B_blah { C_blah(int); }; // expected-note 0-1{{declared here}}
220 struct D1 : C_blah {
221 // FIXME: We should be able to correct this in C++11 mode.
222 using B_blah::C_blah; // expected-error-re {{no member named 'C_blah' in 'PR19171::B_blah'{{$}}}}
224 struct D2 : C_blah {
225 // Somewhat bizarrely, this names the injected-class-name of B_blah within
226 // C_blah, and is valid.
227 using C_blah::B_blah;
229 struct D3 : C_blah {
230 using C_blah::D_blah;
231 #if __cplusplus < 201103L
232 // expected-error-re@-2 {{no member named 'D_blah' in 'PR19171::C_blah'{{$}}}}
233 #else
234 // expected-error@-4 {{no member named 'D_blah' in 'PR19171::C_blah'; did you mean 'C_blah'?}}
235 #endif
237 #if __cplusplus >= 201103L
238 D3 d3(0); // ok
239 #endif
241 struct E { };
242 struct EE { int EE; };
243 struct F : E {
244 using E::EE; // expected-error-re {{no member named 'EE' in 'PR19171::E'{{$}}}}
247 struct TypoDuplicate { // expected-note 0-4{{here}}
248 TypoDuplicate(int);
249 void foobar(); // expected-note 2{{here}}
251 struct TypoDuplicateDerived1 : TypoDuplicate {
252 #if __cplusplus >= 201103L
253 using TypoDuplicate::TypoFuplicate; // expected-error {{did you mean 'TypoDuplicate'}} expected-note {{previous}}
254 using TypoDuplicate::TypoDuplicate; // expected-error {{redeclaration}}
255 #endif
256 using TypoDuplicate::goobar; // expected-error {{did you mean 'foobar'}} expected-note {{previous}}
257 using TypoDuplicate::foobar; // expected-error {{redeclaration}}
259 struct TypoDuplicateDerived2 : TypoDuplicate {
260 #if __cplusplus >= 201103L
261 using TypoFuplicate::TypoDuplicate; // expected-error {{did you mean 'TypoDuplicate'}} expected-note {{previous}}
262 using TypoDuplicate::TypoDuplicate; // expected-error {{redeclaration}}
263 #endif
265 struct TypoDuplicateDerived3 : TypoDuplicate {
266 #if __cplusplus >= 201103L
267 // FIXME: Don't suggest a correction that would lead to a redeclaration
268 // error here... or at least diagnose the error.
269 using TypoDuplicate::TypoDuplicate;
270 using TypoDuplicate::TypoFuplicate; // expected-error {{did you mean 'TypoDuplicate'}}
271 #endif
272 using TypoDuplicate::foobar;
273 using TypoDuplicate::goobar; // expected-error {{did you mean 'foobar'}}
275 struct TypoDuplicateDerived4 : TypoDuplicate {
276 #if __cplusplus >= 201103L
277 using TypoDuplicate::TypoDuplicate; // expected-note {{previous}}
278 using TypoFuplicate::TypoDuplicate; // expected-error {{did you mean 'TypoDuplicate'}} expected-error {{redeclaration}}
279 #endif
283 namespace TypoCorrectTemplateMember {
284 struct A {
285 template<typename T> void foobar(T); // expected-note {{'foobar' declared here}}
287 struct B : A {
288 using A::goobar; // expected-error {{no member named 'goobar' in 'TypoCorrectTemplateMember::A'; did you mean 'foobar'?}}
292 namespace use_instance_in_static {
293 struct A { int n; };
294 struct B : A {
295 using A::n;
296 static int f() { return n; } // expected-error {{invalid use of member 'n' in static member function}}
300 namespace PR24030 {
301 namespace X {
302 class A; // expected-note {{target}}
303 int i; // expected-note {{target}}
305 namespace Y {
306 using X::A; // expected-note {{using}}
307 using X::i; // expected-note {{using}}
308 class A {}; // expected-error {{conflicts}}
309 int i; // expected-error {{conflicts}}
313 namespace PR24033 {
314 extern int a; // expected-note 2{{target of using declaration}}
315 void f(); // expected-note 2{{target of using declaration}}
316 struct s; // expected-note 2{{target of using declaration}}
317 enum e {}; // expected-note 2{{target of using declaration}}
319 template<typename> extern int vt; // expected-note 2{{target of using declaration}} expected-warning 0-1{{extension}}
320 template<typename> void ft(); // expected-note 2{{target of using declaration}}
321 template<typename> struct st; // expected-note 2{{target of using declaration}}
323 namespace X {
324 using PR24033::a; // expected-note {{using declaration}}
325 using PR24033::f; // expected-note {{using declaration}}
326 using PR24033::s; // expected-note {{using declaration}}
327 using PR24033::e; // expected-note {{using declaration}}
329 using PR24033::vt; // expected-note {{using declaration}}
330 using PR24033::ft; // expected-note {{using declaration}}
331 using PR24033::st; // expected-note {{using declaration}}
333 extern int a; // expected-error {{declaration conflicts with target of using declaration already in scope}}
334 void f(); // expected-error {{declaration conflicts with target of using declaration already in scope}}
335 struct s; // expected-error {{declaration conflicts with target of using declaration already in scope}}
336 enum e {}; // expected-error {{declaration conflicts with target of using declaration already in scope}}
338 template<typename> extern int vt; // expected-error {{declaration conflicts with target of using declaration already in scope}} expected-warning 0-1{{extension}}
339 template<typename> void ft(); // expected-error {{declaration conflicts with target of using declaration already in scope}}
340 template<typename> struct st; // expected-error {{declaration conflicts with target of using declaration already in scope}}
343 namespace Y {
344 extern int a; // expected-note {{conflicting declaration}}
345 void f(); // expected-note {{conflicting declaration}}
346 struct s; // expected-note {{conflicting declaration}}
347 enum e {}; // expected-note {{conflicting declaration}}
349 template<typename> extern int vt; // expected-note {{conflicting declaration}} expected-warning 0-1{{extension}}
350 template<typename> void ft(); // expected-note {{conflicting declaration}}
351 template<typename> struct st; // expected-note {{conflicting declaration}}
353 using PR24033::a; // expected-error {{target of using declaration conflicts with declaration already in scope}}
354 using PR24033::f; // expected-error {{target of using declaration conflicts with declaration already in scope}}
355 using PR24033::s; // expected-error {{target of using declaration conflicts with declaration already in scope}}
356 using PR24033::e; // expected-error {{target of using declaration conflicts with declaration already in scope}}
358 using PR24033::vt; // expected-error {{target of using declaration conflicts with declaration already in scope}}
359 using PR24033::ft; // expected-error {{target of using declaration conflicts with declaration already in scope}}
360 using PR24033::st; // expected-error {{target of using declaration conflicts with declaration already in scope}}
364 namespace field_use {
365 struct A { int field; };
366 struct B : A {
367 // Previously Clang rejected this valid C++11 code because it didn't look
368 // through the UsingShadowDecl.
369 using A::field;
370 #if __cplusplus < 201103L
371 // expected-error@+2 {{invalid use of non-static data member 'field'}}
372 #endif
373 enum { X = sizeof(field) };
377 namespace tag_vs_var {
378 namespace N {
379 struct X {};
381 struct Y {};
382 int Y;
384 int Z;
386 using N::X;
387 using N::Y;
388 using N::Z;
390 namespace N {
391 int X;
393 struct Z {};
395 using N::X;
396 using N::Y;
397 using N::Z;
400 // expected-error@+5 {{requires a qualified name}}
401 // expected-error@+4 {{expected ';'}}
402 // expected-error@+3 {{expected '}'}}
403 // expected-note@+2 {{to match this '{'}}
404 // expected-error@+1 {{expected ';'}}
405 template<class> struct S { using S