xref: /aosp_15_r20/external/clang/test/SemaCXX/using-decl-1.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
2*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
3*67e74705SXin Li 
4*67e74705SXin Li extern "C" { void f(bool); }
5*67e74705SXin Li 
6*67e74705SXin Li namespace std {
7*67e74705SXin Li   using ::f;
f()8*67e74705SXin Li   inline void f() { return f(true); }
9*67e74705SXin Li }
10*67e74705SXin Li 
11*67e74705SXin Li namespace M {
12*67e74705SXin Li   void f(float);
13*67e74705SXin Li }
14*67e74705SXin Li 
15*67e74705SXin Li namespace N {
16*67e74705SXin Li   using M::f;
f(int)17*67e74705SXin Li   void f(int) { } // expected-note{{previous}}
18*67e74705SXin Li 
f(int)19*67e74705SXin Li   void f(int) { } // expected-error{{redefinition}}
20*67e74705SXin Li }
21*67e74705SXin Li 
22*67e74705SXin Li namespace N {
23*67e74705SXin Li   void f(double);
24*67e74705SXin Li   void f(long);
25*67e74705SXin Li }
26*67e74705SXin Li 
27*67e74705SXin Li struct X0 {
28*67e74705SXin Li   void operator()(int);
29*67e74705SXin Li   void operator()(long);
30*67e74705SXin Li };
31*67e74705SXin Li 
32*67e74705SXin Li struct X1 : X0 {
33*67e74705SXin Li   void operator()(float&);
34*67e74705SXin Li   using X0::operator();
35*67e74705SXin Li 
testX136*67e74705SXin Li   void test() {
37*67e74705SXin Li     (*this)(1);
38*67e74705SXin Li   }
39*67e74705SXin Li };
40*67e74705SXin Li 
41*67e74705SXin Li struct A { void f(); };
42*67e74705SXin Li struct B : A { };
43*67e74705SXin Li class C : B { using B::f; };
44*67e74705SXin Li 
45*67e74705SXin Li // PR5751: Resolve overloaded functions through using decls.
46*67e74705SXin Li namespace O {
47*67e74705SXin Li   void f(int i);
48*67e74705SXin Li   void f(double d);
49*67e74705SXin Li }
50*67e74705SXin Li namespace P {
51*67e74705SXin Li   void f();
52*67e74705SXin Li   void g(void (*ptr)(int));
53*67e74705SXin Li   using O::f;
test()54*67e74705SXin Li   void test() {
55*67e74705SXin Li     f();
56*67e74705SXin Li     f(1);
57*67e74705SXin Li     void (*f_ptr1)(double) = f;
58*67e74705SXin Li     void (*f_ptr2)() = f;
59*67e74705SXin Li     g(f);
60*67e74705SXin Li   }
61*67e74705SXin Li }
62*67e74705SXin Li 
63*67e74705SXin Li // Make sure that ADL can find names brought in by using decls.
64*67e74705SXin Li namespace test0 {
65*67e74705SXin Li   namespace ns {
66*67e74705SXin Li     class Foo {};
67*67e74705SXin Li 
68*67e74705SXin Li     namespace inner {
69*67e74705SXin Li       void foo(char *); // expected-note {{no known conversion}}
70*67e74705SXin Li     }
71*67e74705SXin Li 
72*67e74705SXin Li     using inner::foo;
73*67e74705SXin Li   }
74*67e74705SXin Li 
test(ns::Foo * p)75*67e74705SXin Li   void test(ns::Foo *p) {
76*67e74705SXin Li     foo(*p); // expected-error {{no matching function for call to 'foo'}}
77*67e74705SXin Li   }
78*67e74705SXin Li }
79*67e74705SXin Li 
80*67e74705SXin Li // Redeclarations!
81*67e74705SXin Li namespace test1 {
82*67e74705SXin Li   namespace ns0 { struct Foo {}; }
83*67e74705SXin Li   namespace A { void foo(ns0::Foo *p, int y, int z); }
84*67e74705SXin Li   namespace ns2 { using A::foo; }
85*67e74705SXin Li   namespace ns1 { struct Bar : ns0::Foo {}; }
86*67e74705SXin Li   namespace A { void foo(ns0::Foo *p, int y, int z = 0); } // expected-note {{candidate}}
87*67e74705SXin Li   namespace ns1 { using A::foo; }
88*67e74705SXin Li   namespace ns2 { struct Baz : ns1::Bar {}; }
89*67e74705SXin Li   namespace A { void foo(ns0::Foo *p, int y = 0, int z); }
90*67e74705SXin Li 
test(ns2::Baz * p)91*67e74705SXin Li   void test(ns2::Baz *p) {
92*67e74705SXin Li     foo(p, 0, 0); // okay!
93*67e74705SXin Li     foo(p, 0); // should be fine!
94*67e74705SXin Li     foo(p); // expected-error {{no matching function}}
95*67e74705SXin Li   }
96*67e74705SXin Li }
97*67e74705SXin Li 
98*67e74705SXin Li namespace test2 {
99*67e74705SXin Li   namespace ns { int foo; }
100*67e74705SXin Li   template <class T> using ns::foo; // expected-error {{cannot template a using declaration}}
101*67e74705SXin Li 
102*67e74705SXin Li   // PR8022
103*67e74705SXin Li   struct A {
104*67e74705SXin Li     template <typename T> void f(T);
105*67e74705SXin Li   };
106*67e74705SXin Li   class B : A {
107*67e74705SXin Li     template <typename T> using A::f<T>; // expected-error {{cannot template a using declaration}}
108*67e74705SXin Li   };
109*67e74705SXin Li }
110*67e74705SXin Li 
111*67e74705SXin Li // PR8756
112*67e74705SXin Li namespace foo
113*67e74705SXin Li {
114*67e74705SXin Li   class Class1; // expected-note{{forward declaration}}
115*67e74705SXin Li   class Class2
116*67e74705SXin Li   {
117*67e74705SXin Li     using ::foo::Class1::Function; // expected-error{{incomplete type 'foo::Class1' named in nested name specifier}}
118*67e74705SXin Li   };
119*67e74705SXin Li }
120*67e74705SXin Li 
121*67e74705SXin Li // Don't suggest non-typenames for positions requiring typenames.
122*67e74705SXin Li namespace using_suggestion_tyname_val {
FFF()123*67e74705SXin Li namespace N { void FFF() {} }
124*67e74705SXin Li using typename N::FFG; // expected-error {{no member named 'FFG' in namespace 'using_suggestion_tyname_val::N'}}
125*67e74705SXin Li }
126*67e74705SXin Li 
127*67e74705SXin Li namespace using_suggestion_member_tyname_val {
AAA()128*67e74705SXin Li class CCC { public: void AAA() { } };
129*67e74705SXin Li class DDD : public CCC { public: using typename CCC::AAB; }; // expected-error {{no member named 'AAB' in 'using_suggestion_member_tyname_val::CCC'}}
130*67e74705SXin Li }
131*67e74705SXin Li 
132*67e74705SXin Li namespace using_suggestion_tyname_val_dropped_specifier {
FFF()133*67e74705SXin Li void FFF() {}
134*67e74705SXin Li namespace N { }
135*67e74705SXin Li using typename N::FFG; // expected-error {{no member named 'FFG' in namespace 'using_suggestion_tyname_val_dropped_specifier::N'}}
136*67e74705SXin Li }
137*67e74705SXin Li 
138*67e74705SXin Li // Currently hints aren't provided to drop out the incorrect M::.
139*67e74705SXin Li namespace using_suggestion_ty_dropped_nested_specifier {
140*67e74705SXin Li namespace N {
141*67e74705SXin Li class AAA {}; // expected-note {{'N::AAA' declared here}}
142*67e74705SXin Li namespace M { }
143*67e74705SXin Li }
144*67e74705SXin Li 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'?}}
145*67e74705SXin Li }
146*67e74705SXin Li 
147*67e74705SXin Li namespace using_suggestion_tyname_ty_dropped_nested_specifier {
148*67e74705SXin Li namespace N {
149*67e74705SXin Li class AAA {}; // expected-note {{'N::AAA' declared here}}
150*67e74705SXin Li namespace M { }
151*67e74705SXin Li }
152*67e74705SXin Li 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'?}}
153*67e74705SXin Li }
154*67e74705SXin Li 
155*67e74705SXin Li namespace using_suggestion_val_dropped_nested_specifier {
156*67e74705SXin Li namespace N {
FFF()157*67e74705SXin Li void FFF() {} // expected-note {{'N::FFF' declared here}}
158*67e74705SXin Li namespace M { }
159*67e74705SXin Li }
160*67e74705SXin Li 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'?}}
161*67e74705SXin Li }
162*67e74705SXin Li 
163*67e74705SXin Li namespace UsingDeclVsHiddenName {
164*67e74705SXin Li   namespace A {
165*67e74705SXin Li     enum HiddenTag1 {}; // expected-note {{previous use is here}}
166*67e74705SXin Li     enum HiddenTag2 {}; // expected-note {{target}}
167*67e74705SXin Li     int HiddenFn1; // expected-note {{target}}
168*67e74705SXin Li     int HiddenFn2; // expected-note {{target}}
169*67e74705SXin Li     int HiddenLocalExtern1;
170*67e74705SXin Li     int HiddenLocalExtern2;
171*67e74705SXin Li   }
172*67e74705SXin Li 
173*67e74705SXin Li   namespace B {
174*67e74705SXin Li     using A::HiddenTag1;
175*67e74705SXin Li     using A::HiddenFn1; // expected-note {{using declaration}}
176*67e74705SXin Li     using A::HiddenLocalExtern1;
177*67e74705SXin Li 
178*67e74705SXin Li     struct S {
179*67e74705SXin Li       friend struct HiddenTag1; // expected-error {{tag type that does not match previous}}
180*67e74705SXin Li       friend struct HiddenTag2; // expected-note {{conflicting declaration}}
181*67e74705SXin Li       friend void HiddenFn1(); // expected-error {{cannot befriend target of using declaration}}
182*67e74705SXin Li       friend void HiddenFn2(); // expected-note {{conflicting declaration}}
fUsingDeclVsHiddenName::B::S183*67e74705SXin Li       void f() {
184*67e74705SXin Li         // OK, these are not in the scope of namespace B, even though they're
185*67e74705SXin Li         // members of the namespace.
186*67e74705SXin Li         void HiddenLocalExtern1();
187*67e74705SXin Li         void HiddenLocalExtern2();
188*67e74705SXin Li       }
189*67e74705SXin Li     };
190*67e74705SXin Li 
191*67e74705SXin Li     using A::HiddenTag2; // expected-error {{conflicts with declaration already in scope}}
192*67e74705SXin Li     using A::HiddenFn2; // expected-error {{conflicts with declaration already in scope}}
193*67e74705SXin Li     using A::HiddenLocalExtern2;
194*67e74705SXin Li   }
195*67e74705SXin Li }
196*67e74705SXin Li 
197*67e74705SXin Li namespace PR19171 {
198*67e74705SXin Li   struct Z {
199*67e74705SXin Li     Z();
200*67e74705SXin Li   };
201*67e74705SXin Li 
202*67e74705SXin Li   typedef struct {
203*67e74705SXin Li     Z i;
204*67e74705SXin Li   } S;
205*67e74705SXin Li 
206*67e74705SXin Li   struct Y : S {
207*67e74705SXin Li     using S::S;
208*67e74705SXin Li #if __cplusplus < 201103L
209*67e74705SXin Li     // expected-error@-2 {{no member named 'S' in 'PR19171::S'}}
210*67e74705SXin Li #endif
211*67e74705SXin Li   };
212*67e74705SXin Li 
213*67e74705SXin Li   // [namespace.udecl]p3: In a using-declaration used as a member-declaration,
214*67e74705SXin Li   // the nested-name-specifier shall name a base class of the class being defined.
215*67e74705SXin Li   // If such a using-declaration names a constructor, the nested-name-specifier
216*67e74705SXin Li   // shall name a direct base class of the class being defined;
217*67e74705SXin Li 
218*67e74705SXin Li   struct B_blah { };
219*67e74705SXin Li   struct C_blah : B_blah { C_blah(int); }; // expected-note 0-1{{declared here}}
220*67e74705SXin Li   struct D1 : C_blah {
221*67e74705SXin Li     // FIXME: We should be able to correct this in C++11 mode.
222*67e74705SXin Li     using B_blah::C_blah; // expected-error-re {{no member named 'C_blah' in 'PR19171::B_blah'{{$}}}}
223*67e74705SXin Li   };
224*67e74705SXin Li   struct D2 : C_blah {
225*67e74705SXin Li     // Somewhat bizarrely, this names the injected-class-name of B_blah within
226*67e74705SXin Li     // C_blah, and is valid.
227*67e74705SXin Li     using C_blah::B_blah;
228*67e74705SXin Li   };
229*67e74705SXin Li   struct D3 : C_blah {
230*67e74705SXin Li     using C_blah::D_blah;
231*67e74705SXin Li #if __cplusplus < 201103L
232*67e74705SXin Li     // expected-error-re@-2 {{no member named 'D_blah' in 'PR19171::C_blah'{{$}}}}
233*67e74705SXin Li #else
234*67e74705SXin Li     // expected-error@-4 {{no member named 'D_blah' in 'PR19171::C_blah'; did you mean 'C_blah'?}}
235*67e74705SXin Li #endif
236*67e74705SXin Li   };
237*67e74705SXin Li #if __cplusplus >= 201103L
238*67e74705SXin Li   D3 d3(0); // ok
239*67e74705SXin Li #endif
240*67e74705SXin Li 
241*67e74705SXin Li   struct E { };
242*67e74705SXin Li   struct EE { int EE; };
243*67e74705SXin Li   struct F : E {
244*67e74705SXin Li     using E::EE; // expected-error-re {{no member named 'EE' in 'PR19171::E'{{$}}}}
245*67e74705SXin Li   };
246*67e74705SXin Li 
247*67e74705SXin Li   struct TypoDuplicate { // expected-note 0-4{{here}}
248*67e74705SXin Li     TypoDuplicate(int);
249*67e74705SXin Li     void foobar(); // expected-note 2{{here}}
250*67e74705SXin Li   };
251*67e74705SXin Li   struct TypoDuplicateDerived1 : TypoDuplicate {
252*67e74705SXin Li #if __cplusplus >= 201103L
253*67e74705SXin Li     using TypoDuplicate::TypoFuplicate; // expected-error {{did you mean 'TypoDuplicate'}} expected-note {{previous}}
254*67e74705SXin Li     using TypoDuplicate::TypoDuplicate; // expected-error {{redeclaration}}
255*67e74705SXin Li #endif
256*67e74705SXin Li     using TypoDuplicate::goobar; // expected-error {{did you mean 'foobar'}} expected-note {{previous}}
257*67e74705SXin Li     using TypoDuplicate::foobar; // expected-error {{redeclaration}}
258*67e74705SXin Li   };
259*67e74705SXin Li   struct TypoDuplicateDerived2 : TypoDuplicate {
260*67e74705SXin Li #if __cplusplus >= 201103L
261*67e74705SXin Li     using TypoFuplicate::TypoDuplicate; // expected-error {{did you mean 'TypoDuplicate'}} expected-note {{previous}}
262*67e74705SXin Li     using TypoDuplicate::TypoDuplicate; // expected-error {{redeclaration}}
263*67e74705SXin Li #endif
264*67e74705SXin Li   };
265*67e74705SXin Li   struct TypoDuplicateDerived3 : TypoDuplicate {
266*67e74705SXin Li #if __cplusplus >= 201103L
267*67e74705SXin Li     // FIXME: Don't suggest a correction that would lead to a redeclaration
268*67e74705SXin Li     // error here... or at least diagnose the error.
269*67e74705SXin Li     using TypoDuplicate::TypoDuplicate;
270*67e74705SXin Li     using TypoDuplicate::TypoFuplicate; // expected-error {{did you mean 'TypoDuplicate'}}
271*67e74705SXin Li #endif
272*67e74705SXin Li     using TypoDuplicate::foobar;
273*67e74705SXin Li     using TypoDuplicate::goobar; // expected-error {{did you mean 'foobar'}}
274*67e74705SXin Li   };
275*67e74705SXin Li   struct TypoDuplicateDerived4 : TypoDuplicate {
276*67e74705SXin Li #if __cplusplus >= 201103L
277*67e74705SXin Li     using TypoDuplicate::TypoDuplicate; // expected-note {{previous}}
278*67e74705SXin Li     using TypoFuplicate::TypoDuplicate; // expected-error {{did you mean 'TypoDuplicate'}} expected-error {{redeclaration}}
279*67e74705SXin Li #endif
280*67e74705SXin Li   };
281*67e74705SXin Li }
282*67e74705SXin Li 
283*67e74705SXin Li namespace TypoCorrectTemplateMember {
284*67e74705SXin Li   struct A {
285*67e74705SXin Li     template<typename T> void foobar(T); // expected-note {{'foobar' declared here}}
286*67e74705SXin Li   };
287*67e74705SXin Li   struct B : A {
288*67e74705SXin Li     using A::goobar; // expected-error {{no member named 'goobar' in 'TypoCorrectTemplateMember::A'; did you mean 'foobar'?}}
289*67e74705SXin Li   };
290*67e74705SXin Li }
291*67e74705SXin Li 
292*67e74705SXin Li namespace use_instance_in_static {
293*67e74705SXin Li struct A { int n; };
294*67e74705SXin Li struct B : A {
295*67e74705SXin Li   using A::n;
fuse_instance_in_static::B296*67e74705SXin Li   static int f() { return n; } // expected-error {{invalid use of member 'n' in static member function}}
297*67e74705SXin Li };
298*67e74705SXin Li }
299*67e74705SXin Li 
300*67e74705SXin Li namespace PR24030 {
301*67e74705SXin Li   namespace X {
302*67e74705SXin Li     class A; // expected-note {{target}}
303*67e74705SXin Li     int i; // expected-note {{target}}
304*67e74705SXin Li   }
305*67e74705SXin Li   namespace Y {
306*67e74705SXin Li     using X::A; // expected-note {{using}}
307*67e74705SXin Li     using X::i; // expected-note {{using}}
308*67e74705SXin Li     class A {}; // expected-error {{conflicts}}
309*67e74705SXin Li     int i; // expected-error {{conflicts}}
310*67e74705SXin Li   }
311*67e74705SXin Li }
312*67e74705SXin Li 
313*67e74705SXin Li namespace PR24033 {
314*67e74705SXin Li   extern int a; // expected-note 2{{target of using declaration}}
315*67e74705SXin Li   void f(); // expected-note 2{{target of using declaration}}
316*67e74705SXin Li   struct s; // expected-note 2{{target of using declaration}}
317*67e74705SXin Li   enum e {}; // expected-note 2{{target of using declaration}}
318*67e74705SXin Li 
319*67e74705SXin Li   template<typename> extern int vt; // expected-note 2{{target of using declaration}} expected-warning 0-1{{extension}}
320*67e74705SXin Li   template<typename> void ft(); // expected-note 2{{target of using declaration}}
321*67e74705SXin Li   template<typename> struct st; // expected-note 2{{target of using declaration}}
322*67e74705SXin Li 
323*67e74705SXin Li   namespace X {
324*67e74705SXin Li     using PR24033::a; // expected-note {{using declaration}}
325*67e74705SXin Li     using PR24033::f; // expected-note {{using declaration}}
326*67e74705SXin Li     using PR24033::s; // expected-note {{using declaration}}
327*67e74705SXin Li     using PR24033::e; // expected-note {{using declaration}}
328*67e74705SXin Li 
329*67e74705SXin Li     using PR24033::vt; // expected-note {{using declaration}}
330*67e74705SXin Li     using PR24033::ft; // expected-note {{using declaration}}
331*67e74705SXin Li     using PR24033::st; // expected-note {{using declaration}}
332*67e74705SXin Li 
333*67e74705SXin Li     extern int a; // expected-error {{declaration conflicts with target of using declaration already in scope}}
334*67e74705SXin Li     void f(); // expected-error {{declaration conflicts with target of using declaration already in scope}}
335*67e74705SXin Li     struct s; // expected-error {{declaration conflicts with target of using declaration already in scope}}
336*67e74705SXin Li     enum e {}; // expected-error {{declaration conflicts with target of using declaration already in scope}}
337*67e74705SXin Li 
338*67e74705SXin Li     template<typename> extern int vt; // expected-error {{declaration conflicts with target of using declaration already in scope}} expected-warning 0-1{{extension}}
339*67e74705SXin Li     template<typename> void ft(); // expected-error {{declaration conflicts with target of using declaration already in scope}}
340*67e74705SXin Li     template<typename> struct st; // expected-error {{declaration conflicts with target of using declaration already in scope}}
341*67e74705SXin Li   }
342*67e74705SXin Li 
343*67e74705SXin Li   namespace Y {
344*67e74705SXin Li     extern int a; // expected-note {{conflicting declaration}}
345*67e74705SXin Li     void f(); // expected-note {{conflicting declaration}}
346*67e74705SXin Li     struct s; // expected-note {{conflicting declaration}}
347*67e74705SXin Li     enum e {}; // expected-note {{conflicting declaration}}
348*67e74705SXin Li 
349*67e74705SXin Li     template<typename> extern int vt; // expected-note {{conflicting declaration}} expected-warning 0-1{{extension}}
350*67e74705SXin Li     template<typename> void ft(); // expected-note {{conflicting declaration}}
351*67e74705SXin Li     template<typename> struct st; // expected-note {{conflicting declaration}}
352*67e74705SXin Li 
353*67e74705SXin Li     using PR24033::a; // expected-error {{target of using declaration conflicts with declaration already in scope}}
354*67e74705SXin Li     using PR24033::f; // expected-error {{target of using declaration conflicts with declaration already in scope}}
355*67e74705SXin Li     using PR24033::s; // expected-error {{target of using declaration conflicts with declaration already in scope}}
356*67e74705SXin Li     using PR24033::e; // expected-error {{target of using declaration conflicts with declaration already in scope}}
357*67e74705SXin Li 
358*67e74705SXin Li     using PR24033::vt; // expected-error {{target of using declaration conflicts with declaration already in scope}}
359*67e74705SXin Li     using PR24033::ft; // expected-error {{target of using declaration conflicts with declaration already in scope}}
360*67e74705SXin Li     using PR24033::st; // expected-error {{target of using declaration conflicts with declaration already in scope}}
361*67e74705SXin Li   }
362*67e74705SXin Li }
363*67e74705SXin Li 
364*67e74705SXin Li namespace field_use {
365*67e74705SXin Li struct A { int field; };
366*67e74705SXin Li struct B : A {
367*67e74705SXin Li   // Previously Clang rejected this valid C++11 code because it didn't look
368*67e74705SXin Li   // through the UsingShadowDecl.
369*67e74705SXin Li   using A::field;
370*67e74705SXin Li #if __cplusplus < 201103L
371*67e74705SXin Li   // expected-error@+2 {{invalid use of non-static data member 'field'}}
372*67e74705SXin Li #endif
373*67e74705SXin Li   enum { X = sizeof(field) };
374*67e74705SXin Li };
375*67e74705SXin Li }
376*67e74705SXin Li 
377*67e74705SXin Li namespace tag_vs_var {
378*67e74705SXin Li   namespace N {
379*67e74705SXin Li     struct X {};
380*67e74705SXin Li 
381*67e74705SXin Li     struct Y {};
382*67e74705SXin Li     int Y;
383*67e74705SXin Li 
384*67e74705SXin Li     int Z;
385*67e74705SXin Li   }
386*67e74705SXin Li   using N::X;
387*67e74705SXin Li   using N::Y;
388*67e74705SXin Li   using N::Z;
389*67e74705SXin Li 
390*67e74705SXin Li   namespace N {
391*67e74705SXin Li     int X;
392*67e74705SXin Li 
393*67e74705SXin Li     struct Z {};
394*67e74705SXin Li   }
395*67e74705SXin Li   using N::X;
396*67e74705SXin Li   using N::Y;
397*67e74705SXin Li   using N::Z;
398*67e74705SXin Li }
399