]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Friend classes don't shadow enclosing template class paramater [PR118255]
authorSimon Martin <simon@nasilyan.com>
Sun, 5 Jan 2025 09:36:47 +0000 (10:36 +0100)
committerSimon Martin <simon@nasilyan.com>
Fri, 17 Jan 2025 08:30:19 +0000 (09:30 +0100)
commitb5a069203fc074ab75d994c4a7e0f2db6a0a00fd
treedd585c24df6ef1b465505c0dd686947f8ff4989c
parent44d21551362f9076617200595f49d4260d1f40a9
c++: Friend classes don't shadow enclosing template class paramater [PR118255]

We currently reject the following code

=== code here ===
template <int non_template> struct S { friend class non_template; };
class non_template {};
S<0> s;
=== code here ===

While EDG agrees with the current behaviour, clang and MSVC don't (see
https://godbolt.org/z/69TGaabhd), and I believe that this code is valid,
since the friend clause does not actually declare a type, so it cannot
shadow anything. The fact that we didn't error out if the non_template
class was declared before S backs this up as well.

This patch fixes this by skipping the call to check_template_shadow for
hidden bindings.

PR c++/118255

gcc/cp/ChangeLog:

* name-lookup.cc (pushdecl): Don't call check_template_shadow
for hidden bindings.

gcc/testsuite/ChangeLog:

* g++.dg/lookup/pr99116-1.C: Adjust test expectation.
* g++.dg/template/friend84.C: New test.
gcc/cp/name-lookup.cc
gcc/testsuite/g++.dg/lookup/pr99116-1.C
gcc/testsuite/g++.dg/template/friend84.C [new file with mode: 0644]