aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2023-05-03 13:32:55 -0400
committerJason Merrill <jason@redhat.com>2023-05-03 15:24:52 -0400
commit7ce078ceca42f184f6f60c3ca921b6e07cf2c4bd (patch)
tree4d4b2dcddf80327b364c5f9747e61ac3a6e50579 /gcc
parent3b7eecca20bcb05e0220b3d012df012902a23534 (diff)
downloadgcc-7ce078ceca42f184f6f60c3ca921b6e07cf2c4bd.zip
gcc-7ce078ceca42f184f6f60c3ca921b6e07cf2c4bd.tar.gz
gcc-7ce078ceca42f184f6f60c3ca921b6e07cf2c4bd.tar.bz2
c++: over-eager friend matching [PR109649]
A bug in the simplification I did around 91618; at this point X<int>::f has DECL_IMPLICIT_INSTANTIATION set, but we've already identified what template it corresponds to, so we don't want to call check_explicit_specialization. To distinguish this case we need to look at DECL_TI_TEMPLATE. grokfndecl has for a long time set it to the OVERLOAD in this case, while the new cases I added for 91618 were leaving DECL_TEMPLATE_INFO null; let's adjust them to match. PR c++/91618 PR c++/109649 gcc/cp/ChangeLog: * friend.cc (do_friend): Don't call check_explicit_specialization if DECL_TEMPLATE_INFO is already set. * decl2.cc (check_classfn): Set DECL_TEMPLATE_INFO. * name-lookup.cc (set_decl_namespace): Likewise. gcc/testsuite/ChangeLog: * g++.dg/template/friend77.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/decl2.cc1
-rw-r--r--gcc/cp/friend.cc3
-rw-r--r--gcc/cp/name-lookup.cc1
-rw-r--r--gcc/testsuite/g++.dg/template/friend77.C19
4 files changed, 23 insertions, 1 deletions
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 9594be4..b510cda 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -855,6 +855,7 @@ check_classfn (tree ctype, tree function, tree template_parms)
So tell check_explicit_specialization to look for a match. */
SET_DECL_IMPLICIT_INSTANTIATION (function);
+ DECL_TEMPLATE_INFO (function) = build_template_info (fns, NULL_TREE);
matched = function;
}
diff --git a/gcc/cp/friend.cc b/gcc/cp/friend.cc
index b36de2b..5ab5c1f 100644
--- a/gcc/cp/friend.cc
+++ b/gcc/cp/friend.cc
@@ -661,7 +661,8 @@ do_friend (tree scope, tree declarator, tree decl,
if (decl == error_mark_node)
return error_mark_node;
- if (!class_template_depth && DECL_IMPLICIT_INSTANTIATION (decl))
+ if (!class_template_depth && DECL_IMPLICIT_INSTANTIATION (decl)
+ && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
/* "[if no non-template match is found,] each remaining function template
is replaced with the specialization chosen by deduction from the
friend declaration or discarded if deduction fails."
diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 8fd5733..ad03141 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -5938,6 +5938,7 @@ set_decl_namespace (tree decl, tree scope, bool friendp)
So tell check_explicit_specialization to look for a match. */
SET_DECL_IMPLICIT_INSTANTIATION (decl);
+ DECL_TEMPLATE_INFO (decl) = build_template_info (old, NULL_TREE);
return;
}
diff --git a/gcc/testsuite/g++.dg/template/friend77.C b/gcc/testsuite/g++.dg/template/friend77.C
new file mode 100644
index 0000000..f232cb1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/friend77.C
@@ -0,0 +1,19 @@
+// PR c++/109649
+
+template <typename>
+class X
+{
+ void f(){}
+};
+
+class Y
+{
+ friend void X<int>::f(); // { dg-error "private" }
+};
+
+int main()
+{
+ X<int> t;
+ t.f(); // { dg-error "private" }
+ Y b;
+}