Clang Project

clang_source_code/test/SemaCXX/using-decl-1.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
3
4extern "C" { void f(bool); }
5
6namespace std {
7  using ::f;
8  inline void f() { return f(true); }
9}
10
11namespace M {
12  void f(float);
13}
14
15namespace N {
16  using M::f;
17  void f(int) { } // expected-note{{previous}}
18  
19  void f(int) { } // expected-error{{redefinition}}
20}
21
22namespace N {
23  void f(double);
24  void f(long);
25}
26
27struct X0 {
28  void operator()(int);
29  void operator()(long);
30};
31
32struct X1 : X0 {
33  void operator()(float&);
34  using X0::operator();
35  
36  void test() {
37    (*this)(1);
38  }
39};
40
41struct A { void f(); };
42struct B : A { };
43class C : B { using B::f; };
44
45// PR5751: Resolve overloaded functions through using decls.
46namespace O {
47  void f(int i);
48  void f(double d);
49}
50namespace 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);
60  }
61}
62
63// Make sure that ADL can find names brought in by using decls.
64namespace test0 {
65  namespace ns {
66    class Foo {};
67    
68    namespace inner {
69      void foo(char *); // expected-note {{no known conversion}} 
70    }
71
72    using inner::foo;
73  }
74
75  void test(ns::Foo *p) {
76    foo(*p); // expected-error {{no matching function for call to 'foo'}}
77  }
78}
79
80// Redeclarations!
81namespace 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); }
90
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}}
95  }
96}
97
98namespace test2 {
99  namespace ns { int foo; }
100  template <class T> using ns::foo; // expected-error {{cannot template a using declaration}}
101
102  // PR8022
103  struct A {
104    template <typename T> void f(T);
105  };
106  class B : A {
107    template <typename T> using A::f<T>; // expected-error {{cannot template a using declaration}}
108  };
109}
110
111// PR8756
112namespace foo
113{
114  class Class1; // expected-note{{forward declaration}}
115  class Class2
116  {
117    using ::foo::Class1::Function; // expected-error{{incomplete type 'foo::Class1' named in nested name specifier}}
118  };
119}
120
121// Don't suggest non-typenames for positions requiring typenames.
122namespace using_suggestion_tyname_val {
123namespace N { void FFF() {} }
124using typename N::FFG; // expected-error {{no member named 'FFG' in namespace 'using_suggestion_tyname_val::N'}}
125}
126
127namespace using_suggestion_member_tyname_val {
128class CCC { public: void AAA() { } };
129class DDD : public CCC { public: using typename CCC::AAB; }; // expected-error {{no member named 'AAB' in 'using_suggestion_member_tyname_val::CCC'}}
130}
131
132namespace using_suggestion_tyname_val_dropped_specifier {
133void FFF() {}
134namespace N { }
135using typename N::FFG; // expected-error {{no member named 'FFG' in namespace 'using_suggestion_tyname_val_dropped_specifier::N'}}
136}
137
138// Currently hints aren't provided to drop out the incorrect M::.
139namespace using_suggestion_ty_dropped_nested_specifier {
140namespace N {
141class AAA {}; // expected-note {{'N::AAA' declared here}}
142namespace M { }
143}
144using 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}
146
147namespace using_suggestion_tyname_ty_dropped_nested_specifier {
148namespace N {
149class AAA {}; // expected-note {{'N::AAA' declared here}}
150namespace M { }
151}
152using 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}
154
155namespace using_suggestion_val_dropped_nested_specifier {
156namespace N {
157void FFF() {} // expected-note {{'N::FFF' declared here}}
158namespace M { }
159}
160using 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}
162
163namespace 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;
171  }
172
173  namespace B {
174    using A::HiddenTag1;
175    using A::HiddenFn1; // expected-note {{using declaration}}
176    using A::HiddenLocalExtern1;
177
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();
188      }
189    };
190
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;
194  }
195}
196
197namespace PR19171 {
198  struct Z {
199    Z();
200  };
201
202  typedef struct {
203    Z i;
204  } S;
205
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
211  };
212
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;
217
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'{{$}}}}
223  };
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;
228  };
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
236  };
237#if __cplusplus >= 201103L
238  D3 d3(0); // ok
239#endif
240
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'{{$}}}}
245  };
246
247  struct TypoDuplicate { // expected-note 0-4{{here}}
248    TypoDuplicate(int);
249    void foobar(); // expected-note 2{{here}}
250  };
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}}
258  };
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
264  };
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'}}
274  };
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
280  };
281}
282
283namespace TypoCorrectTemplateMember {
284  struct A {
285    template<typename T> void foobar(T); // expected-note {{'foobar' declared here}}
286  };
287  struct B : A {
288    using A::goobar; // expected-error {{no member named 'goobar' in 'TypoCorrectTemplateMember::A'; did you mean 'foobar'?}}
289  };
290}
291
292namespace use_instance_in_static {
293struct A { int n; };
294struct B : A {
295  using A::n;
296  static int f() { return n; } // expected-error {{invalid use of member 'n' in static member function}}
297};
298}
299
300namespace PR24030 {
301  namespace X {
302    class A; // expected-note {{target}}
303    int i; // expected-note {{target}}
304  }
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}}
310  }
311}
312
313namespace 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}}
318
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}}
322
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}}
328
329    using PR24033::vt; // expected-note {{using declaration}}
330    using PR24033::ft; // expected-note {{using declaration}}
331    using PR24033::st; // expected-note {{using declaration}}
332
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}}
337
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}}
341  }
342
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}}
348
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}}
352
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}}
357
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}}
361  }
362}
363
364namespace field_use {
365struct A { int field; };
366struct 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) };
374};
375}
376
377namespace tag_vs_var {
378  namespace N {
379    struct X {};
380
381    struct Y {};
382    int Y;
383
384    int Z;
385  }
386  using N::X;
387  using N::Y;
388  using N::Z;
389
390  namespace N {
391    int X;
392
393    struct Z {};
394  }
395  using N::X;
396  using N::Y;
397  using N::Z;
398}
399