xref: /llvm-project/clang/test/SemaCXX/nested-name-spec.cpp (revision fb8b7b9a1c524c420728d1814997d6e988f992de)
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;
12   void Af();
13 }
14 
15 A:: ; // expected-error {{expected unqualified-id}}
16 // FIXME: there is a member 'ax'; it's just not a class.
17 ::A::ax::undef ex3; // expected-error {{no member named 'ax'}}
18 A::undef1::undef2 ex4; // expected-error {{no member named 'undef1'}}
19 
20 int A::C::Ag1() { return 0; }
21 
22 static int A::C::Ag2() { return 0; } // expected-error{{'static' can}}
23 
24 int A::C::cx = 17;
25 
26 
27 static int A::C::cx2 = 17; // expected-error{{'static' can}}
28 
29 class C2 {
30   void m(); // expected-note{{member declaration does not match because it is not const qualified}}
31 
32   void f(const int& parm); // expected-note{{type of 1st parameter of member declaration does not match definition ('const int &' vs 'int')}}
33   void f(int) const; // expected-note{{member declaration does not match because it is const qualified}}
34   void f(float);
35 
36   int x;
37 };
38 
39 void C2::m() const { } // expected-error{{out-of-line definition of 'm' does not match any declaration in 'C2'}}
40 
41 void C2::f(int) { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'C2'}}
42 
43 void C2::m() {
44   x = 0;
45 }
46 
47 namespace B {
48   void ::A::Af() {} // expected-error {{cannot define or redeclare 'Af' here because namespace 'B' does not enclose namespace 'A'}}
49 }
50 
51 void f1() {
52   void A::Af(); // expected-error {{definition or redeclaration of 'Af' not allowed inside a function}}
53   void (^x)() = ^{ void A::Af(); }; // expected-error {{definition or redeclaration of 'Af' not allowed inside a block}}
54 }
55 
56 void f2() {
57   A:: ; // expected-error {{expected unqualified-id}}
58   A::C::undef = 0; // expected-error {{no member named 'undef'}}
59   ::A::C::cx = 0;
60   int x = ::A::ax = A::C::cx;
61   x = sizeof(A::C);
62   x = sizeof(::A::C::cx);
63 }
64 
65 A::C c1;
66 struct A::C c2;
67 struct S : public A::C {};
68 struct A::undef; // expected-error {{no struct named 'undef' in namespace 'A'}}
69 
70 namespace A2 {
71   typedef int INT;
72   struct RC;
73   struct CC {
74     struct NC;
75   };
76 }
77 
78 struct A2::RC {
79   INT x;
80 };
81 
82 struct A2::CC::NC {
83   void m() {}
84 };
85 
86 void f3() {
87   N::x = 0; // expected-error {{use of undeclared identifier 'N'}}
88   int N;
89   N::x = 0; // expected-error {{expected a class or namespace}}
90   { int A;           A::ax = 0; }
91   { typedef int A;   A::ax = 0; } // expected-error{{expected a class or namespace}}
92   { typedef A::C A;  A::ax = 0; } // expected-error {{no member named 'ax'}}
93   { typedef A::C A;  A::cx = 0; }
94 }
95 
96 // make sure the following doesn't hit any asserts
97 void f4(undef::C); // expected-error {{use of undeclared identifier 'undef'}}
98 
99 typedef void C2::f5(int); // expected-error{{typedef declarator cannot be qualified}}
100 
101 void f6(int A2::RC::x); // expected-error{{parameter declarator cannot be qualified}}
102 
103 int A2::RC::x; // expected-error{{non-static data member defined out-of-line}}
104 
105 void A2::CC::NC::m(); // expected-error{{out-of-line declaration of a member must be a definition}}
106 
107 
108 namespace E {
109   int X = 5;
110 
111   namespace Nested {
112     enum E {
113       X = 0
114     };
115 
116     void f() {
117       return E::X; // expected-error{{expected a class or namespace}}
118     }
119   }
120 }
121 
122 
123 class Operators {
124   Operators operator+(const Operators&) const; // expected-note{{member declaration does not match because it is const qualified}}
125   operator bool();
126 };
127 
128 Operators Operators::operator+(const Operators&) { // expected-error{{out-of-line definition of 'operator+' does not match any declaration in 'Operators'}}
129   Operators ops;
130   return ops;
131 }
132 
133 Operators Operators::operator+(const Operators&) const {
134   Operators ops;
135   return ops;
136 }
137 
138 Operators::operator bool() {
139   return true;
140 }
141 
142 namespace A {
143   void g(int&); // expected-note{{type of 1st parameter of member declaration does not match definition ('int &' vs 'const int &')}}
144 }
145 
146 void A::f() {} // expected-error-re{{out-of-line definition of 'f' does not match any declaration in namespace 'A'$}}
147 
148 void A::g(const int&) { } // expected-error{{out-of-line definition of 'g' does not match any declaration in namespace 'A'}}
149 
150 struct Struct { };
151 
152 void Struct::f() { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'Struct'}}
153 
154 void global_func(int);
155 void global_func2(int);
156 
157 namespace N {
158   void ::global_func(int) { } // expected-error{{definition or redeclaration of 'global_func' cannot name the global scope}}
159 
160   void f();
161   // FIXME: if we move this to a separate definition of N, things break!
162 }
163 void ::global_func2(int) { } // expected-error{{extra qualification on member 'global_func2'}}
164 
165 void N::f() { } // okay
166 
167 struct Y;  // expected-note{{forward declaration of 'Y'}}
168 Y::foo y; // expected-error{{incomplete type 'Y' named in nested name specifier}}
169 
170 X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}}
171 
172 struct foo_S {
173   static bool value;
174 };
175 bool (foo_S::value);
176 
177 
178 namespace somens {
179   struct a { }; // expected-note{{candidate constructor (the implicit copy constructor)}}
180 }
181 
182 template <typename T>
183 class foo {
184 };
185 
186 
187 // PR4452 / PR4451
188 foo<somens:a> a2;  // expected-error {{unexpected ':' in nested name specifier}}
189 
190 somens::a a3 = a2; // expected-error {{no viable conversion}}
191 
192 // typedefs and using declarations.
193 namespace test1 {
194   namespace ns {
195     class Counter { public: static int count; };
196     typedef Counter counter;
197   }
198   using ns::counter;
199 
200   class Test {
201     void test1() {
202       counter c;
203       c.count++;
204       counter::count++;
205     }
206   };
207 }
208 
209 // We still need to do lookup in the lexical scope, even if we push a
210 // non-lexical scope.
211 namespace test2 {
212   namespace ns {
213     extern int *count_ptr;
214   }
215   namespace {
216     int count = 0;
217   }
218 
219   int *ns::count_ptr = &count;
220 }
221 
222 // PR6259, invalid case
223 namespace test3 {
224   class A; // expected-note {{forward declaration}}
225   void foo(const char *path) {
226     A::execute(path); // expected-error {{incomplete type 'test3::A' named in nested name specifier}}
227   }
228 }
229 
230 namespace PR7133 {
231   namespace A {
232     class Foo;
233   }
234 
235   namespace A {
236     namespace B {
237       bool foo(Foo &);
238     }
239   }
240 
241   bool A::B::foo(Foo &) {
242     return false;
243   }
244 }
245 
246 class CLASS {
247   void CLASS::foo2(); // expected-error {{extra qualification on member 'foo2'}}
248 };
249 
250 namespace PR8159 {
251   class B { };
252 
253   class A {
254     int A::a; // expected-error{{extra qualification on member 'a'}}
255     static int A::b; // expected-error{{extra qualification on member 'b'}}
256     int ::c; // expected-error{{non-friend class member 'c' cannot have a qualified name}}
257   };
258 }
259 
260 namespace rdar7980179 {
261   class A { void f0(); }; // expected-note {{previous}}
262   int A::f0() {} // expected-error {{return type of out-of-line definition of 'rdar7980179::A::f0' differs}}
263 }
264 
265 namespace alias = A;
266 double *dp = (alias::C*)0; // expected-error{{cannot initialize a variable of type 'double *' with an rvalue of type 'alias::C *'}}
267 
268 // http://llvm.org/PR10109
269 namespace PR10109 {
270 template<typename T>
271 struct A {
272 protected:
273   struct B;
274   struct B::C; // expected-error {{requires a template parameter list}} \
275                // expected-error {{no struct named 'C'}} \
276     // expected-error{{non-friend class member 'C' cannot have a qualified name}}
277 };
278 
279 template<typename T>
280 struct A2 {
281 protected:
282   struct B;
283 };
284 template <typename T>
285 struct A2<T>::B::C; // expected-error {{no struct named 'C'}}
286 }
287 
288 namespace PR13033 {
289 namespace NS {
290  int a; // expected-note {{'NS::a' declared here}}
291  int longer_b; //expected-note {{'NS::longer_b' declared here}}
292 }
293 
294 // Suggest adding a namespace qualifier to both variable names even though one
295 // is only a single character long.
296 int foobar = a + longer_b; // expected-error {{use of undeclared identifier 'a'; did you mean 'NS::a'?}} \
297                            // expected-error {{use of undeclared identifier 'longer_b'; did you mean 'NS::longer_b'?}}
298 }
299 
300 // <rdar://problem/13853540>
301 namespace N {
302   struct X { };
303   namespace N {
304     struct Foo {
305       struct N::X *foo(); // expected-error{{no struct named 'X' in namespace 'N::N'}}
306     };
307   }
308 }
309 
310 namespace TypedefNamespace { typedef int F; };
311 TypedefNamespace::F::NonexistentName BadNNSWithCXXScopeSpec; // expected-error {{expected a class or namespace}}
312