diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 9594be4092c3c00fddc9d4c6da5931ea3b7e8792..b510cdac55418ef00dbc0d8b5ef4caffa9226c78 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 b36de2b20bba9f945299566c68b2e2af069bcb2e..5ab5c1fec730c8c36648c6c123bfd1088b909162 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 8fd5733c1e249513adc3e94f38254f44ee005e89..ad03141df430f90d4cdc59675afcd5f4feffaa5d 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 0000000000000000000000000000000000000000..f232cb1075632de4f7a360df4c8b170bfd671914
--- /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;
+}