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