]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constrained template friend matching ICE [PR105064]
authorPatrick Palka <ppalka@redhat.com>
Mon, 28 Mar 2022 18:15:16 +0000 (14:15 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 7 Apr 2022 19:12:38 +0000 (15:12 -0400)
Here during declaration matching for the two constrained template
friends, we crash from maybe_substitute_reqs_for because the second
friend doesn't yet have DECL_TEMPLATE_INFO set (we're being called
indirectly from push_template_decl).

As far as I can tell, this situation happens only when declaring a
constrained template friend within a non-template class (as in the
testcase), in which case the substitution would be a no-op anyway.
So this patch rearranges maybe_substitute_reqs_for to gracefully
handle missing DECL_TEMPLATE_INFO by just skipping the substitution.

PR c++/105064

gcc/cp/ChangeLog:

* constraint.cc (maybe_substitute_reqs_for): Don't assume
DECL_TEMPLATE_INFO is available.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/concepts-friend9.C: New test.

(cherry picked from commit ecb4882e362e80a1bf172453ac9b366edbb4e89c)

gcc/cp/constraint.cc
gcc/testsuite/g++.dg/cpp2a/concepts-friend9.C [new file with mode: 0644]

index 1519d0f104164e08d9cc96665da8edf1b8dc6e52..03d66810531011698f55bff23f823f15fbcd0a42 100644 (file)
@@ -1288,20 +1288,15 @@ remove_constraints (tree t)
    for declaration matching.  */
 
 tree
-maybe_substitute_reqs_for (tree reqs, const_tree decl_)
+maybe_substitute_reqs_for (tree reqs, const_tree decl)
 {
   if (reqs == NULL_TREE)
     return NULL_TREE;
 
-  tree decl = CONST_CAST_TREE (decl_);
-  tree result = STRIP_TEMPLATE (decl);
-
-  if (DECL_UNIQUE_FRIEND_P (result))
+  decl = STRIP_TEMPLATE (decl);
+  if (DECL_UNIQUE_FRIEND_P (decl) && DECL_TEMPLATE_INFO (decl))
     {
-      tree tmpl = decl;
-      if (TREE_CODE (decl) != TEMPLATE_DECL)
-       tmpl = DECL_TI_TEMPLATE (result);
-
+      tree tmpl = DECL_TI_TEMPLATE (decl);
       tree gargs = generic_targs_for (tmpl);
       processing_template_decl_sentinel s;
       if (uses_template_parms (gargs))
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-friend9.C b/gcc/testsuite/g++.dg/cpp2a/concepts-friend9.C
new file mode 100644 (file)
index 0000000..09054d2
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/105064
+// { dg-do compile { target c++20 } }
+
+struct A {
+  template<class T>
+  friend void f(T) requires true;
+};
+
+struct B {
+  template<class T>
+  friend void f(T) requires true;
+};