aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Palka <ppalka@redhat.com>2023-06-03 09:26:43 -0400
committerPatrick Palka <ppalka@redhat.com>2023-06-03 09:26:43 -0400
commit38af81df6aac11d49f9378d1c628ccc3c815c3ff (patch)
tree82b9ee008df9e6cb17f93154487bd8d134d711ed
parent23bee8508816297a9b2ab81a96aa0c79ac88ceba (diff)
downloadgcc-38af81df6aac11d49f9378d1c628ccc3c815c3ff.zip
gcc-38af81df6aac11d49f9378d1c628ccc3c815c3ff.tar.gz
gcc-38af81df6aac11d49f9378d1c628ccc3c815c3ff.tar.bz2
c++: is_specialization_of_friend confusion [PR109923]
The check for a non-template member function of a class template in is_specialization_of_friend is overbroad, and accidentally holds for a non-template hidden friend too, which for the testcase below causes the predicate to bogusly return true for decl = void non_templ_friend(A<int>, A<void>) friend_decl = void non_templ_friend(A<void>, A<void>) This patch refines the check appropriately. PR c++/109923 gcc/cp/ChangeLog: * pt.cc (is_specialization_of_friend): Fix overbroad check for a non-template member function of a class template. gcc/testsuite/ChangeLog: * g++.dg/template/friend79.C: New test.
-rw-r--r--gcc/cp/pt.cc1
-rw-r--r--gcc/testsuite/g++.dg/template/friend79.C20
2 files changed, 21 insertions, 0 deletions
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index dae3815..6b20c58 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -1319,6 +1319,7 @@ is_specialization_of_friend (tree decl, tree friend_decl)
of a template class, we want to check if DECL is a specialization
if this. */
if (TREE_CODE (friend_decl) == FUNCTION_DECL
+ && DECL_CLASS_SCOPE_P (friend_decl)
&& DECL_TEMPLATE_INFO (friend_decl)
&& !DECL_USE_TEMPLATE (friend_decl))
{
diff --git a/gcc/testsuite/g++.dg/template/friend79.C b/gcc/testsuite/g++.dg/template/friend79.C
new file mode 100644
index 0000000..7603adf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/friend79.C
@@ -0,0 +1,20 @@
+// PR c++/109923
+
+template<class T>
+struct A {
+private:
+ int x;
+
+public:
+ A() : x(0) { }
+
+ friend void non_templ_friend(A val, A<void> weird) {
+ val.x++; // always works
+ weird.x++; // { dg-error "private" } should only work when T=void
+ }
+};
+
+int main() {
+ non_templ_friend(A<void>(), A<void>()); // { dg-bogus "" }
+ non_templ_friend(A<int>(), A<void>()); // { dg-message "required from here" }
+}