Skip to content

Commit 4a505e1

Browse files
authored
[clang] Add tests for CWG issues about friend declaration matching (#106117)
This patch covers CWG issues regarding declaration matching when `friend` declarations are involved: [CWG138](https://cplusplus.github.io/CWG/issues/138.html), [CWG386](https://cplusplus.github.io/CWG/issues/386.html), [CWG1477](https://cplusplus.github.io/CWG/issues/1477.html), and [CWG1900](https://cplusplus.github.io/CWG/issues/1900.html). Atypical for our CWG tests, the ones in this patch are quite extensively commented in-line, explaining the mechanics. PR description focuses on high-level concerns and references. [CWG138](https://cplusplus.github.io/CWG/issues/138.html) "Friend declaration name lookup" ----------- [P1787R6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1787r6.html): > [CWG138](https://cplusplus.github.io/CWG/issues/138.html) is resolved according to [N1229](http://wg21.link/n1229), except that using-directives that nominate nested namespaces are considered. I find it hard to pin down the scope of this issue, so I'm relying on three examples from the filing to define it. Because of that, it's also hard to pinpoint exact wording changes that resolve it. Relevant references are: [[dcl.meaning.general]/2](http://eel.is/c++draft/dcl.meaning#general-2), [[namespace.udecl]/10](https://eel.is/c++draft/namespace.udecl#10), [[dcl.type.elab]/3](https://eel.is/c++draft/dcl.type.elab#3), [[basic.lookup.elab]/1](https://eel.is/c++draft/basic.lookup.elab#1). [CWG386](https://cplusplus.github.io/CWG/issues/386.html) "Friend declaration of name brought in by _using-declaration_" ----------- [P1787R6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1787r6.html): > [CWG386](https://cplusplus.github.io/CWG/issues/386.html), [CWG1839](https://cplusplus.github.io/CWG/issues/1839.html), [CWG1818](https://cplusplus.github.io/CWG/issues/1818.html), [CWG2058](https://cplusplus.github.io/CWG/issues/2058.html), [CWG1900](https://cplusplus.github.io/CWG/issues/1900.html), and Richard’s observation in [“are non-type names ignored in a class-head-name or enum-head-name?”](http://lists.isocpp.org/core/2017/01/1604.php) are resolved by describing the limited lookup that occurs for a declarator-id, including the changes in Richard’s [proposed resolution for CWG1839](http://wiki.edg.com/pub/Wg21cologne2019/CoreWorkingGroup/cwg1839.html) (which also resolves CWG1818 and what of CWG2058 was not resolved along with CWG2059) and rejecting the example from [CWG1477](https://cplusplus.github.io/CWG/issues/1477.html). Wording ([[dcl.meaning.general]/2](http://eel.is/c++draft/dcl.meaning#general-2)): > — If the [id-expression](http://eel.is/c++draft/expr.prim.id.general#nt:id-expression) E in the [declarator-id](http://eel.is/c++draft/dcl.decl.general#nt:declarator-id) of the [declarator](http://eel.is/c++draft/dcl.decl.general#nt:declarator) is a [qualified-id](http://eel.is/c++draft/expr.prim.id.qual#nt:qualified-id) or a [template-id](http://eel.is/c++draft/temp.names#nt:template-id): >      — [...] >      — The [declarator](http://eel.is/c++draft/dcl.decl.general#nt:declarator) shall correspond to one or more declarations found by the lookup; they shall all have the same target scope, and the target scope of the [declarator](http://eel.is/c++draft/dcl.decl.general#nt:declarator) is that scope[.](http://eel.is/c++draft/dcl.meaning#general-2.2.2.sentence-1) This issue focuses on interaction of `friend` declarations with template-id and qualified-id with using-declarations. The short answer is that terminal name in such declarations undergo lookup, and using-declarations do what they usually do helping that lookup. Target scope of such friend declaration is the target scope of lookup result, so no conflicts arise with the using-declarations. [CWG1477](https://cplusplus.github.io/CWG/issues/1477.html) "Definition of a `friend` outside its namespace" ----------- [P1787R6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1787r6.html): > [...] and rejecting the example from [CWG1477](https://cplusplus.github.io/CWG/issues/1477.html). Wording ([[dcl.meaning.general]/3.4](http://eel.is/c++draft/dcl.meaning#general-3.4)): > Otherwise, the terminal name of the [declarator-id](http://eel.is/c++draft/dcl.decl.general#nt:declarator-id) is not looked up[.](http://eel.is/c++draft/dcl.meaning#general-3.4.sentence-1) If it is a qualified name, the [declarator](http://eel.is/c++draft/dcl.decl.general#nt:declarator) shall correspond to one or more declarations nominable in S; all the declarations shall have the same target scope and the target scope of the [declarator](http://eel.is/c++draft/dcl.decl.general#nt:declarator) is that scope[.](http://eel.is/c++draft/dcl.meaning#general-3.4.sentence-2) This issue focuses on befriending a function in one scope, then defining it from other scope using qualified-id. Contrary to what P1787R6 says in prose, this example is accepted by the wording in that paper. In the wording quote above, note the absence of a statement like "terminal name of the declarator-id is not bound", contrary to similar statements made before that in [dcl.meaning.general] about friend declarations and template-ids. There's also a note in [basic.scope.scope] that supports the rejection, but it's considered incorrect and expected to be removed in the future. This is tracked in cplusplus/draft#7238. [CWG1900](https://cplusplus.github.io/CWG/issues/1900.html) "Do `friend` declarations count as “previous declarations”?" ------------------ [P1787R6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1787r6.html): > [CWG386](https://cplusplus.github.io/CWG/issues/386.html), [CWG1839](https://cplusplus.github.io/CWG/issues/1839.html), [CWG1818](https://cplusplus.github.io/CWG/issues/1818.html), [CWG2058](https://cplusplus.github.io/CWG/issues/2058.html), [CWG1900](https://cplusplus.github.io/CWG/issues/1900.html), and Richard’s observation in [“are non-type names ignored in a class-head-name or enum-head-name?”](http://lists.isocpp.org/core/2017/01/1604.php) are resolved by describing the limited lookup that occurs for a declarator-id, including the changes in Richard’s [proposed resolution for CWG1839](http://wiki.edg.com/pub/Wg21cologne2019/CoreWorkingGroup/cwg1839.html) (which also resolves CWG1818 and what of CWG2058 was not resolved along with CWG2059) and rejecting the example from [CWG1477](https://cplusplus.github.io/CWG/issues/1477.html). Wording ([[dcl.meaning.general]/2.3](http://eel.is/c++draft/dcl.meaning#general-2.3)): > The declaration's target scope is the innermost enclosing namespace scope; if the declaration is contained by a block scope, the declaration shall correspond to a reachable ([[module.reach]](http://eel.is/c++draft/module.reach)) declaration that inhabits the innermost block scope[.](http://eel.is/c++draft/dcl.meaning#general-2.3.sentence-2) Wording ([[basic.scope.scope]/7](http://eel.is/c++draft/basic.scope#scope-7)): > A declaration is [nominable](http://eel.is/c++draft/basic.scope#def:nominable) in a class, class template, or namespace E at a point P if it precedes P, it does not inhabit a block scope, and its target scope is the scope associated with E or, if E is a namespace, any element of the inline namespace set of E ([[namespace.def]](http://eel.is/c++draft/namespace.def))[.](http://eel.is/c++draft/basic.scope#scope-7.sentence-1) Wording ([[dcl.meaning.general]/3.4](http://eel.is/c++draft/dcl.meaning#general-3.4)): > If it is a qualified name, the [declarator](http://eel.is/c++draft/dcl.decl.general#nt:declarator) shall correspond to one or more declarations nominable in S; [...] In the new wording it's clear that while `friend` declarations of functions do not bind names, declaration is still introduced, and is nominable, making it eligible for a later definition by qualified-id.
1 parent b6a4ab5 commit 4a505e1

File tree

5 files changed

+184
-4
lines changed

5 files changed

+184
-4
lines changed

clang/test/CXX/drs/cwg14xx.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,28 @@ namespace cwg1467 { // cwg1467: 3.7 c++11
603603
#endif
604604
} // cwg1467
605605

606+
namespace cwg1477 { // cwg1477: 2.7
607+
namespace N {
608+
struct A {
609+
// Name "f" is not bound in N,
610+
// so single searches of 'f' in N won't find it,
611+
// but the targets scope of this declaration is N,
612+
// making it nominable in N.
613+
// (_N4988_.[dcl.meaning]/2.1, [basic.scope.scope]/7,
614+
// [basic.lookup.general]/3)
615+
friend int f();
616+
};
617+
}
618+
// Corresponds to the friend declaration,
619+
// because it's nominable in N,
620+
// and binds name 'f' in N.
621+
// (_N4988_.[dcl.meaning]/3.4, [basic.scope.scope]/2.5)
622+
int N::f() { return 0; }
623+
// Name 'f' is bound in N,
624+
// so the search performed by qualified lookup finds it.
625+
int i = N::f();
626+
} // namespace cwg1477
627+
606628
namespace cwg1479 { // cwg1479: 3.1
607629
#if __cplusplus >= 201103L
608630
int operator"" _a(const char*, std::size_t = 0);

clang/test/CXX/drs/cwg19xx.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88

99
namespace std { struct type_info; }
1010

11+
namespace cwg1900 { // cwg1900: 2.7
12+
// See the test for CWG1477 for detailed analysis
13+
namespace N {
14+
struct A {
15+
friend int f();
16+
};
17+
}
18+
int N::f() { return 0; }
19+
int N::g() { return 0; }
20+
// expected-error@-1 {{out-of-line definition of 'g' does not match any declaration in namespace 'cwg1900::N'}}
21+
} // namespace cwg1900
22+
1123
namespace cwg1902 { // cwg1902: 3.7
1224
struct A {};
1325
struct B {

clang/test/CXX/drs/cwg1xx.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,80 @@ namespace cwg137 { // cwg137: yes
568568
const volatile int *cvqcv = static_cast<const volatile int*>(cvp);
569569
}
570570

571+
namespace cwg138 { // cwg138: partial
572+
namespace example1 {
573+
void foo(); // #cwg138-ex1-foo
574+
namespace A {
575+
using example1::foo; // #cwg138-ex1-using
576+
class X {
577+
static const int i = 10;
578+
// This friend declaration is using neither qualified-id nor template-id,
579+
// so name 'foo' is not looked up, which means the using-declaration has no effect.
580+
// Target scope of this declaration is A, so this is grating friendship to
581+
// (hypothetical) A::foo instead of 'example1::foo' using declaration refers to.
582+
// A::foo corresponds to example1::foo named by the using declaration,
583+
// and since A::foo is a different entity, they potentially conflict.
584+
// FIXME: This is ill-formed, but not for the reason diagnostic says.
585+
friend void foo();
586+
// expected-error@-1 {{cannot befriend target of using declaration}}
587+
// expected-note@#cwg138-ex1-foo {{target of using declaration}}
588+
// expected-note@#cwg138-ex1-using {{using declaration}}
589+
};
590+
}
591+
} // namespace example1
592+
593+
namespace example2 {
594+
void f();
595+
void g();
596+
class B {
597+
void g();
598+
};
599+
class A : public B {
600+
static const int i = 10;
601+
void f();
602+
// Both friend declaration are not using qualified-ids or template-ids,
603+
// so 'f' and 'g' are not looked up, which means that presence of A::f
604+
// and base B have no effect.
605+
// Both target scope of namespace 'example2', and grant friendship to
606+
// example2::f and example2::g respectively.
607+
friend void f();
608+
friend void g();
609+
};
610+
void f() {
611+
int i2 = A::i;
612+
}
613+
void g() {
614+
int i3 = A::i;
615+
}
616+
} // namespace example2
617+
618+
namespace example3 {
619+
struct Base {
620+
private:
621+
static const int i = 10; // #cwg138-ex3-Base-i
622+
623+
public:
624+
struct Data;
625+
// Elaborated type specifier is not the sole constituent of declaration,
626+
// so 'Data' undergoes unqualified type-only lookup, which finds Base::Data.
627+
friend class Data;
628+
629+
struct Data {
630+
void f() {
631+
int i2 = Base::i;
632+
}
633+
};
634+
};
635+
struct Data {
636+
void f() {
637+
int i2 = Base::i;
638+
// expected-error@-1 {{'i' is a private member of 'cwg138::example3::Base'}}
639+
// expected-note@#cwg138-ex3-Base-i {{declared private here}}
640+
}
641+
};
642+
} // namespace example3
643+
} // namespace cwg138
644+
571645
namespace cwg139 { // cwg139: yes
572646
namespace example1 {
573647
typedef int f; // #cwg139-typedef-f

clang/test/CXX/drs/cwg3xx.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,78 @@ namespace cwg385 { // cwg385: 2.8
13691369
// expected-note@#cwg385-n {{member is declared here}}
13701370
}
13711371

1372+
namespace cwg386 { // cwg386: no
1373+
namespace example1 {
1374+
namespace N1 {
1375+
// Binds name 'f' in N1. Target scope is N1.
1376+
template<typename T> void f( T* x ) {
1377+
// ... other stuff ...
1378+
delete x;
1379+
}
1380+
}
1381+
1382+
namespace N2 {
1383+
// Bind name 'f' in N2. When a single search find this declaration,
1384+
// it's replaced with N1::f declaration.
1385+
using N1::f;
1386+
1387+
// According to _N4988_.[dcl.meaning]/3.3:
1388+
// `f<int>` is not a qualified-id, so its target scope is N2.
1389+
// `f<int>` is a template-id, so 'f' undergoes (unqualified) lookup.
1390+
// Search performed by unqualified lookup finds N1::f via using-declaration,
1391+
// but this result is not considered, because it's not nominable in N2,
1392+
// which is because its target scope is N1.
1393+
// So unqualified lookup doesn't find anything, making this declaration ill-formed.
1394+
template<> void f<int>( int* );
1395+
// expected-error@-1 {{no function template matches function template specialization 'f'}}
1396+
1397+
class Test {
1398+
~Test() { }
1399+
// According to _N4988_.[dcl.meaning]/2.2:
1400+
// `f<>` is a template-id and not a template declaration,
1401+
// so its terminal name 'f' undergoes (unqualified) lookup.
1402+
// Search in N2 performed by unqualified lookup finds
1403+
// (single) N1::f declaration via using-declaration.
1404+
// N1::f is replaced with N1::f<> specialization after deduction,
1405+
// and this is the result of the unqualified lookup.
1406+
// This friend declaration correspond to the result of the lookup.
1407+
// All lookup results target the same scope, which is N1,
1408+
// so target scope of this friend declaration is also N1.
1409+
// FIXME: This is well-formed.
1410+
friend void f<>( Test* x );
1411+
// expected-error@-1 {{no function template matches function template specialization 'f'}}
1412+
};
1413+
}
1414+
} // namespace example1
1415+
1416+
namespace example2 {
1417+
namespace N1 {
1418+
// Binds name 'f' in N1. Target scope is N1.
1419+
void f(); // #cwg386-ex2-N1-f
1420+
}
1421+
1422+
namespace N2 {
1423+
// Bind name 'f' in N2. When a single search finds this declaration,
1424+
// it's replaced with N1::f declaration.
1425+
using N1::f; // #cwg386-ex2-using
1426+
class A {
1427+
// According to _N4988_.[dcl.meaning]/2.2:
1428+
// `N2::f` is a qualified-id, so its terminal name 'f' undergoes (qualified) lookup.
1429+
// Search in N2 performed by qualified lookup finds N1::f via using-declaration,
1430+
// which is the (only) result of qualified lookup.
1431+
// This friend declaration corresponds to the result of the lookup.
1432+
// All lookup results target the same scope, which is N1,
1433+
// so target scope of this friend declaration is also N1.
1434+
// FIXME: This is well-formed.
1435+
friend void N2::f();
1436+
// expected-error@-1 {{cannot befriend target of using declaration}}
1437+
// expected-note@#cwg386-ex2-N1-f {{target of using declaration}}
1438+
// expected-note@#cwg386-ex2-using {{using declaration}}
1439+
};
1440+
}
1441+
} // namespace example2
1442+
} // namespace cwg386
1443+
13721444
namespace cwg387 { // cwg387: 2.8
13731445
namespace old {
13741446
template<typename T> class number {

clang/www/cxx_dr_status.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
873873
<td><a href="https://cplusplus.github.io/CWG/issues/138.html">138</a></td>
874874
<td>CD6</td>
875875
<td>Friend declaration name lookup</td>
876-
<td class="unknown" align="center">Unknown</td>
876+
<td class="partial" align="center">Partial</td>
877877
</tr>
878878
<tr id="139">
879879
<td><a href="https://cplusplus.github.io/CWG/issues/139.html">139</a></td>
@@ -2363,7 +2363,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
23632363
<td><a href="https://cplusplus.github.io/CWG/issues/386.html">386</a></td>
23642364
<td>CD6</td>
23652365
<td>Friend declaration of name brought in by <I>using-declaration</I></td>
2366-
<td class="unknown" align="center">Unknown</td>
2366+
<td class="none" align="center">No</td>
23672367
</tr>
23682368
<tr id="387">
23692369
<td><a href="https://cplusplus.github.io/CWG/issues/387.html">387</a></td>
@@ -8685,7 +8685,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
86858685
<td><a href="https://cplusplus.github.io/CWG/issues/1477.html">1477</a></td>
86868686
<td>CD3</td>
86878687
<td>Definition of a <TT>friend</TT> outside its namespace</td>
8688-
<td class="unknown" align="center">Unknown</td>
8688+
<td class="full" align="center">Clang 2.7</td>
86898689
</tr>
86908690
<tr id="1478">
86918691
<td><a href="https://cplusplus.github.io/CWG/issues/1478.html">1478</a></td>
@@ -11231,7 +11231,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
1123111231
<td><a href="https://cplusplus.github.io/CWG/issues/1900.html">1900</a></td>
1123211232
<td>CD6</td>
1123311233
<td>Do <TT>friend</TT> declarations count as &#8220;previous declarations&#8221;?</td>
11234-
<td class="unknown" align="center">Unknown</td>
11234+
<td class="full" align="center">Clang 2.7</td>
1123511235
</tr>
1123611236
<tr class="open" id="1901">
1123711237
<td><a href="https://cplusplus.github.io/CWG/issues/1901.html">1901</a></td>

0 commit comments

Comments
 (0)