xref: /llvm-project/clang/test/SemaCXX/nested-name-spec.cpp (revision 8282c58d9b1cd5b6df89ee3f68438fe0ee672f7f)
1 // RUN: %clang_cc1 -fsyntax-only -std=c++98 -verify -fblocks %s
2 namespace A {
3   struct C {
4     static int cx;
5 
6     static int cx2;
7 
8     static int Ag1();
9     static int Ag2();
10   };
11   int ax; // expected-note {{'ax' declared here}}
12   void Af();
13 }
14 
15 A:: ; // expected-error {{expected unqualified-id}}
16 ::A::ax::undef ex3; // expected-error {{'ax' is not a class, namespace, or enumeration}}
17 A::undef1::undef2 ex4; // expected-error {{no member named 'undef1'}}
18 
19 int A::C::Ag1() { return 0; }
20 
21 static int A::C::Ag2() { return 0; } // expected-error{{'static' can}}
22 
23 int A::C::cx = 17;
24 
25 
26 static int A::C::cx2 = 17; // expected-error{{'static' can}}
27 
28 class C2 { // #defined-here-C2
29   void m(); // expected-note{{member declaration does not match because it is not const qualified}}
30 
31   void f(const int& parm); // expected-note{{type of 1st parameter of member declaration does not match definition ('const int &' vs 'int')}}
32   void f(int) const; // expected-note{{member declaration does not match because it is const qualified}}
33   void f(float);
34 
35   int x;
36 };
37 
38 void C2::m() const { } // expected-error{{out-of-line definition of 'm' does not match any declaration in 'C2'}}
39                        // expected-note@#defined-here-C2{{defined here}}
40 
41 void C2::f(int) { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'C2'}}
42                     // expected-note@#defined-here-C2{{defined here}}
43 
44 void C2::m() {
45   x = 0;
46 }
47 
48 namespace B {
49   void ::A::Af() {} // expected-error {{cannot define or redeclare 'Af' here because namespace 'B' does not enclose namespace 'A'}}
50 }
51 
52 void f1() {
53   void A::Af(); // expected-error {{definition or redeclaration of 'Af' not allowed inside a function}}
54   void (^x)() = ^{ void A::Af(); }; // expected-error {{definition or redeclaration of 'Af' not allowed inside a block}}
55 }
56 
57 void f2() {
58   A:: ; // expected-error {{expected unqualified-id}}
59   A::C::undef = 0; // expected-error {{no member named 'undef'}}
60   ::A::C::cx = 0;
61   int x = ::A::ax = A::C::cx;
62   x = sizeof(A::C);
63   x = sizeof(::A::C::cx);
64 }
65 
66 A::C c1;
67 struct A::C c2;
68 struct S : public A::C {};
69 struct A::undef; // expected-error {{no struct named 'undef' in namespace 'A'}}
70                  // expected-error@-1 {{forward declaration of struct cannot have a nested name specifier}}
71 namespace A2 {
72   typedef int INT;
73   struct RC;
74   struct CC {
75     struct NC;
76   };
77 }
78 
79 struct A2::RC {
80   INT x;
81 };
82 
83 struct A2::CC::NC {
84   void m() {}
85 };
86 
87 void f3() {
88   N::x = 0; // expected-error {{use of undeclared identifier 'N'}}
89   // FIXME: Consider including the kind of entity that 'N' is ("variable 'N'
90   // declared here", "template 'X' declared here", etc) to help explain what it
91   // is if it's 'not a class, namespace, or scoped enumeration'.
92   int N; // expected-note {{'N' declared here}}
93   N::x = 0; // expected-error {{'N' is not a class, namespace, or enumeration}}
94   { int A;           A::ax = 0; }
95   { typedef int A;   A::ax = 0; } // expected-error{{'A' (aka 'int') is not a class, namespace, or enumeration}}
96   { typedef A::C A;  A::ax = 0; } // expected-error {{no member named 'ax'}}
97   { typedef A::C A;  A::cx = 0; }
98 }
99 
100 // make sure the following doesn't hit any asserts
101 void f4(undef::C); // expected-error {{use of undeclared identifier 'undef'}}
102 
103 typedef void C2::f5(int); // expected-error{{typedef declarator cannot be qualified}}
104 
105 void f6(int A2::RC::x); // expected-error{{parameter declarator cannot be qualified}}
106 
107 int A2::RC::x; // expected-error{{non-static data member defined out-of-line}}
108 
109 void A2::CC::NC::m(); // expected-error{{out-of-line declaration of a member must be a definition}}
110 
111 
112 namespace E {
113   int X = 5;
114 
115   namespace Nested {
116     enum E {
117       X = 0
118     };
119 
120     int f() {
121       return E::X; // expected-warning{{use of enumeration in a nested name specifier is a C++11 extension}}
122     }
123   }
124 }
125 
126 
127 class Operators { // #defined-here-Operators
128   Operators operator+(const Operators&) const; // expected-note{{member declaration does not match because it is const qualified}}
129   operator bool();
130 };
131 
132 Operators Operators::operator+(const Operators&) { // expected-error{{out-of-line definition of 'operator+' does not match any declaration in 'Operators'}}
133                                                    // expected-note@#defined-here-Operators{{defined here}}
134   Operators ops;
135   return ops;
136 }
137 
138 Operators Operators::operator+(const Operators&) const {
139   Operators ops;
140   return ops;
141 }
142 
143 Operators::operator bool() {
144   return true;
145 }
146 
147 namespace A {
148   void g(int&); // expected-note{{type of 1st parameter of member declaration does not match definition ('int &' vs 'const int &')}}
149 }
150 
151 void A::f() {} // expected-error-re{{out-of-line definition of 'f' does not match any declaration in namespace 'A'{{$}}}}
152 
153 void A::g(const int&) { } // expected-error{{out-of-line definition of 'g' does not match any declaration in namespace 'A'}}
154 
155 struct Struct { }; // #defined-here-Struct
156 
157 void Struct::f() { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'Struct'}}
158                      // expected-note@#defined-here-Struct{{defined here}}
159 
160 void global_func(int);
161 void global_func2(int);
162 
163 namespace N {
164   void ::global_func(int) { } // expected-error{{definition or redeclaration of 'global_func' cannot name the global scope}}
165 
166   void f();
167   // FIXME: if we move this to a separate definition of N, things break!
168 }
169 void ::global_func2(int) { } // expected-warning{{extra qualification on member 'global_func2'}}
170 
171 void N::f() { } // okay
172 
173 struct Y;  // expected-note{{forward declaration of 'Y'}}
174 Y::foo y; // expected-error{{incomplete type 'Y' named in nested name specifier}}
175 
176 namespace PR25156 {
177 struct Y;  // expected-note{{forward declaration of 'PR25156::Y'}}
178 void foo() {
179   Y::~Y(); // expected-error{{incomplete type 'PR25156::Y' named in nested name specifier}}
180 }
181 }
182 
183 X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}}
184 
185 struct foo_S {
186   static bool value;
187 };
188 bool (foo_S::value);
189 
190 
191 namespace somens {
192   struct a { }; // expected-note{{candidate constructor (the implicit copy constructor)}}
193 }
194 
195 template <typename T>
196 class foo {
197 };
198 
199 
200 // PR4452 / PR4451
201 foo<somens:a> a2;  // expected-error {{unexpected ':' in nested name specifier}}
202 
203 somens::a a3 = a2; // expected-error {{no viable conversion}}
204 
205 // typedefs and using declarations.
206 namespace test1 {
207   namespace ns {
208     class Counter { public: static int count; };
209     typedef Counter counter;
210   }
211   using ns::counter;
212 
213   class Test {
214     void test1() {
215       counter c;
216       c.count++;
217       counter::count++;
218     }
219   };
220 }
221 
222 // We still need to do lookup in the lexical scope, even if we push a
223 // non-lexical scope.
224 namespace test2 {
225   namespace ns {
226     extern int *count_ptr;
227   }
228   namespace {
229     int count = 0;
230   }
231 
232   int *ns::count_ptr = &count;
233 }
234 
235 // PR6259, invalid case
236 namespace test3 {
237   class A; // expected-note {{forward declaration}}
238   void foo(const char *path) {
239     A::execute(path); // expected-error {{incomplete type 'test3::A' named in nested name specifier}}
240   }
241 }
242 
243 namespace PR7133 {
244   namespace A {
245     class Foo;
246   }
247 
248   namespace A {
249     namespace B {
250       bool foo(Foo &);
251     }
252   }
253 
254   bool A::B::foo(Foo &) {
255     return false;
256   }
257 }
258 
259 class CLASS {
260   void CLASS::foo2(); // expected-error {{extra qualification on member 'foo2'}}
261 };
262 
263 namespace PR8159 {
264   class B { };
265 
266   class A {
267     int A::a; // expected-error{{extra qualification on member 'a'}}
268     static int A::b; // expected-error{{extra qualification on member 'b'}}
269     int ::c; // expected-error{{non-friend class member 'c' cannot have a qualified name}}
270   };
271 }
272 
273 namespace rdar7980179 {
274   class A { void f0(); }; // expected-note {{previous}}
275   int A::f0() {} // expected-error {{return type of out-of-line definition of 'rdar7980179::A::f0' differs}}
276 }
277 
278 namespace alias = A;
279 double *dp = (alias::C*)0; // expected-error{{cannot initialize a variable of type 'double *' with an rvalue of type 'alias::C *'}}
280 
281 // http://llvm.org/PR10109
282 namespace PR10109 {
283 template<typename T>
284 struct A {
285 protected:
286   struct B;
287   struct B::C;
288   // expected-error@-1 {{requires a template parameter list}}
289   // expected-error@-2 {{no struct named 'C'}}
290   // expected-error@-3 {{non-friend class member 'C' cannot have a qualified name}}
291   // expected-error@-4 {{forward declaration of struct cannot have a nested name specifier}}
292 };
293 
294 template<typename T>
295 struct A2 {
296 protected:
297   struct B;
298 };
299 template <typename T>
300 struct A2<T>::B::C; // expected-error {{no struct named 'C'}}
301                     // expected-error@-1 {{forward declaration of struct cannot have a nested name specifier}}
302 }
303 
304 namespace PR13033 {
305 namespace NS {
306  int a; // expected-note {{'NS::a' declared here}}
307  int longer_b; //expected-note {{'NS::longer_b' declared here}}
308 }
309 
310 // Suggest adding a namespace qualifier to both variable names even though one
311 // is only a single character long.
312 int foobar = a + longer_b; // expected-error {{use of undeclared identifier 'a'; did you mean 'NS::a'?}} \
313                            // expected-error {{use of undeclared identifier 'longer_b'; did you mean 'NS::longer_b'?}}
314 }
315 
316 namespace N {
317   struct X { };
318   namespace N {
319     struct Foo {
320       struct N::X *foo(); // expected-error{{no struct named 'X' in namespace 'N::N'}}
321     };
322   }
323 }
324 
325 namespace TypedefNamespace { typedef int F; };
326 TypedefNamespace::F::NonexistentName BadNNSWithCXXScopeSpec; // expected-error {{'TypedefNamespace::F' (aka 'int') is not a class, namespace, or enumeration}}
327 
328 namespace PR18587 {
329 
330 struct C1 {
331   int a, b, c;
332   typedef int C2;
333   struct B1 {
334     struct B2 {
335       int a, b, c;
336     };
337   };
338 };
339 struct C2 { static const unsigned N1 = 1; };
340 struct B1 {
341   enum E1 { B2 = 2 };
342   static const int B3 = 3;
343 };
344 const int N1 = 2;
345 
346 // Function declarators
347 struct S1a { int f(C1::C2); };
348 struct S1b { int f(C1:C2); };  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
349 
350 struct S2a {
351   C1::C2 f(C1::C2);
352 };
353 struct S2c {
354   C1::C2 f(C1:C2);  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
355 };
356 
357 struct S3a {
358   int f(C1::C2), C2 : N1;
359   int g : B1::B2;
360 };
361 struct S3b {
362   int g : B1:B2;  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
363 };
364 
365 // Inside square brackets
366 struct S4a {
367   int f[C2::N1];
368 };
369 struct S4b {
370   int f[C2:N1];  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
371 };
372 
373 struct S5a {
374   int f(int xx[B1::B3 ? C2::N1 : B1::B2]);
375 };
376 struct S5b {
377   int f(int xx[B1::B3 ? C2::N1 : B1:B2]);  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
378 };
379 struct S5c {
380   int f(int xx[B1:B3 ? C2::N1 : B1::B2]);  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
381 };
382 
383 // Bit fields
384 struct S6a {
385   C1::C2 m1 : B1::B2;
386 };
387 struct S6c {
388   C1::C2 m1 : B1:B2;  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
389 };
390 struct S6d {
391   int C2:N1;
392 };
393 struct S6e {
394   static const int N = 3;
395   B1::E1 : N;
396 };
397 struct S6g {
398   C1::C2 : B1:B2;  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
399   B1::E1 : B1:B2;  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
400 };
401 
402 // Template parameters
403 template <int N> struct T1 {
404   int a,b,c;
405   static const unsigned N1 = N;
406   typedef unsigned C1;
407 };
408 T1<C2::N1> var_1a;
409 T1<C2:N1> var_1b;  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
410 template<int N> int F() {}
411 int (*X1)() = (B1::B2 ? F<1> : F<2>);
412 int (*X2)() = (B1:B2 ? F<1> : F<2>);  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
413 
414 // Bit fields + templates
415 struct S7a {
416   T1<B1::B2>::C1 m1 : T1<B1::B2>::N1;
417 };
418 struct S7b {
419   T1<B1:B2>::C1 m1 : T1<B1::B2>::N1;  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
420 };
421 struct S7c {
422   T1<B1::B2>::C1 m1 : T1<B1:B2>::N1;  // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
423 };
424 
425 }
426 
427 namespace PR16951 {
428   namespace ns {
429     enum an_enumeration {
430       ENUMERATOR  // expected-note{{'ENUMERATOR' declared here}}
431     };
432   }
433 
434   int x1 = ns::an_enumeration::ENUMERATOR; // expected-warning{{use of enumeration in a nested name specifier is a C++11 extension}}
435 
436   int x2 = ns::an_enumeration::ENUMERATOR::vvv; // expected-warning{{use of enumeration in a nested name specifier is a C++11 extension}} \
437                                                 // expected-error{{'ENUMERATOR' is not a class, namespace, or enumeration}} \
438 
439   int x3 = ns::an_enumeration::X; // expected-warning{{use of enumeration in a nested name specifier is a C++11 extension}} \
440                                   // expected-error{{no member named 'X'}}
441 
442   enum enumerator_2 {
443     ENUMERATOR_2
444   };
445 
446   int x4 = enumerator_2::ENUMERATOR_2; // expected-warning{{use of enumeration in a nested name specifier is a C++11 extension}}
447   int x5 = enumerator_2::X2; // expected-warning{{use of enumeration in a nested name specifier is a C++11 extension}} \
448                              // expected-error{{no member named 'X2' in 'PR16951::enumerator_2'}}
449 
450 }
451 
452 namespace PR30619 {
453 c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d;
454 // expected-error@-1 16{{unknown type name 'c'}}
455 c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d;
456 // expected-error@-1 16{{unknown type name 'c'}}
457 c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d;
458 // expected-error@-1 16{{unknown type name 'c'}}
459 c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d;
460 // expected-error@-1 16{{unknown type name 'c'}}
461 namespace A {
462 class B {
463   typedef C D; // expected-error{{unknown type name 'C'}}
464   A::D::F;
465   // expected-error@-1{{'PR30619::A::B::D' (aka 'int') is not a class, namespace, or enumeration}}
466 };
467 }
468 }
469 
470 namespace DependentTemplateInTrivialNNSLoc {
471   // This testcase causes us to create trivial type source info when doing
472   // substitution into T::template g<>. That trivial type source info contained
473   // a NestedNameSpecifierLoc with no location information.
474   //
475   // Previously, creating a CXXScopeSpec from that resulted in an invalid scope
476   // spec, leading to crashes. Ensure we don't crash here.
477   template <typename T> void f(T &x) {
478     for (typename T::template g<> i : x) {} // expected-warning 0-1{{extension}}
479     x: goto x;
480   }
481 }
482 
483 template <typename T>
484 struct x; // expected-note {{template is declared here}}
485 
486 template <typename T>
487 int issue55962 = x::a; // expected-error {{use of class template 'x' requires template arguments}} \
488                        // expected-warning {{variable templates are a C++14 extension}}
489