diff mbox series

c++: unifying FUNCTION_DECLs [PR93740]

Message ID 20231212184037.3040106-1-ppalka@redhat.com
State New
Headers show
Series c++: unifying FUNCTION_DECLs [PR93740] | expand

Commit Message

Patrick Palka Dec. 12, 2023, 6:40 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look
OK for trunk?  I considered removing the is_overloaded_fn test now as
well, but it could in theory be hit (and not subsumed by the
type_unknown_p test) for e.g. OVERLOAD of a single FUNCTION_DECL.  I
wonder if that's something we'd see here?  If not, I can remove the
test.  It seems safe to remove as far as the testsuite is concerned.

-- >8 --

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 unify incorrectly matches &A::foo with
&A::bar, which leads to deduction failure for the index_of calls due to
a bogus base class ambiguity.

This patch makes us instead handle unification of FUNCTION_DECL like
other decls, i.e. according to their identity.

	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                             |  1 +
 gcc/testsuite/g++.dg/template/ptrmem34.C | 27 ++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/template/ptrmem34.C

Comments

Jason Merrill Dec. 12, 2023, 7:33 p.m. UTC | #1
On 12/12/23 13:40, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look
> OK for trunk?

OK.

> I considered removing the is_overloaded_fn test now as
> well, but it could in theory be hit (and not subsumed by the
> type_unknown_p test) for e.g. OVERLOAD of a single FUNCTION_DECL.  I
> wonder if that's something we'd see here?  If not, I can remove the
> test.  It seems safe to remove as far as the testsuite is concerned.

Next stage 1, sure.

> -- >8 --
> 
> 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 unify incorrectly matches &A::foo with
> &A::bar, which leads to deduction failure for the index_of calls due to
> a bogus base class ambiguity.
> 
> This patch makes us instead handle unification of FUNCTION_DECL like
> other decls, i.e. according to their identity.
> 
> 	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                             |  1 +
>   gcc/testsuite/g++.dg/template/ptrmem34.C | 27 ++++++++++++++++++++++++
>   2 files changed, 28 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/template/ptrmem34.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index c2ddbff405b..a8966e223f1 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -24967,6 +24967,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
> index 00000000000..c349ca55639
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/ptrmem34.C
> @@ -0,0 +1,27 @@
> +// PR c++/93740
> +// { dg-do compile { target c++11 } }
> +
> +struct A {
> +  void foo();
> +  void bar();
> +};
> +
> +template <typename T, T val>
> +struct const_val{};
> +
> +template <int N, typename 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 <typename 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, "");
diff mbox series

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index c2ddbff405b..a8966e223f1 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -24967,6 +24967,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
index 00000000000..c349ca55639
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/ptrmem34.C
@@ -0,0 +1,27 @@ 
+// PR c++/93740
+// { dg-do compile { target c++11 } }
+
+struct A {
+  void foo();
+  void bar();
+};
+
+template <typename T, T val>
+struct const_val{};
+
+template <int N, typename 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 <typename 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, "");