]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: unifying FUNCTION_DECLs [PR93740]
authorPatrick Palka <ppalka@redhat.com>
Wed, 13 Dec 2023 20:55:01 +0000 (15:55 -0500)
committerPatrick Palka <ppalka@redhat.com>
Wed, 13 Dec 2023 20:55:01 +0000 (15:55 -0500)
unify currently always returns success when unifying two FUNCTION_DECLs
(due to the is_overloaded_fn deferment within the default case), which
means for the below testcase we incorrectly unify &A::foo and &A::bar
leading to deduction failure for the index_of calls due to a bogus base
class ambiguity.

This patch makes unify handle FUNCTION_DECL naturally like other decls.

PR c++/93740

gcc/cp/ChangeLog:

* pt.cc (unify) <case FUNCTION_DECL>: Handle it like FIELD_DECL
and TEMPLATE_DECL.

gcc/testsuite/ChangeLog:

* g++.dg/template/ptrmem34.C: New test.

gcc/cp/pt.cc
gcc/testsuite/g++.dg/template/ptrmem34.C [new file with mode: 0644]

index 0dd0a9c644c1e7e906b74f42f7519ad895ee244d..9a21467bf219b72f5a07404e8b89d7fd7fc9b514 100644 (file)
@@ -24964,6 +24964,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
       gcc_unreachable ();
 
     case FIELD_DECL:
+    case FUNCTION_DECL:
     case TEMPLATE_DECL:
       /* Matched cases are handled by the ARG == PARM test above.  */
       return unify_template_argument_mismatch (explain_p, parm, arg);
diff --git a/gcc/testsuite/g++.dg/template/ptrmem34.C b/gcc/testsuite/g++.dg/template/ptrmem34.C
new file mode 100644 (file)
index 0000000..75c911e
--- /dev/null
@@ -0,0 +1,27 @@
+// PR c++/93740
+// { dg-do compile { target c++11 } }
+
+struct A {
+  void foo();
+  void bar();
+};
+
+template<class T, T val>
+struct const_val { };
+
+template<int N, class T>
+struct indexed_elem { };
+
+using mem_fun_A_foo = const_val<decltype(&A::foo), &A::foo>;
+using mem_fun_A_bar = const_val<decltype(&A::bar), &A::bar>;
+
+struct A_indexed_member_funcs
+  : indexed_elem<0, mem_fun_A_foo>,
+    indexed_elem<1, mem_fun_A_bar>
+{ };
+
+template<class T, int N>
+constexpr int index_of(indexed_elem<N, T>) { return N; }
+
+static_assert(index_of<mem_fun_A_foo>(A_indexed_member_funcs{}) == 0, "");
+static_assert(index_of<mem_fun_A_bar>(A_indexed_member_funcs{}) == 1, "");