public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: equivalence of non-dependent calls [PR107461]
@ 2023-02-04 20:31 Patrick Palka
  2023-02-04 23:42 ` Jason Merrill
  0 siblings, 1 reply; 8+ messages in thread
From: Patrick Palka @ 2023-02-04 20:31 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent
CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of FUNCTION_DECL.
This innocent change revealed that cp_tree_equal doesn't first check
dependentness of a CALL_EXPR before treating the callee as a dependent
name, which manifests as us incorrectly accepting the first two
testcases below and rejecting the third:

 * In the first testcase, cp_tree_equal incorrectly returns true for
   the two non-dependent CALL_EXPRs f(0) and f(0) (whose CALL_EXPR_FN
   are different FUNCTION_DECLs) and so we treat #2 as a redeclaration
   of #1.

 * Same issue in the second testcase, for f<int*>() and f<char>().

 * In the third testcase, cp_tree_equal incorrectly returns true for
   f<int>() and f<void(*)(int)>() which causes us to conflate the two
   dependent specializations A<decltype(f<int>()(U()))> and
   A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error.

This patch fixes this by making called_fns_equal treat two callees as
dependent names only if the CALL_EXPRs in question are dependent.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk/12?  Patch generated with -w to ignore noisy whitespace changes.

	PR c++/107461

gcc/cp/ChangeLog:

	* pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Treat
	the callee as a dependent name only if the CALL_EXPR is
	dependent.
	* tree.cc (called_fns_equal): Take two CALL_EXPRs instead of
	CALL_EXPR_FNs thereof.  As above.
	(cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/overload5.C: New test.
	* g++.dg/cpp0x/overload5a.C: New test.
	* g++.dg/cpp0x/overload6.C: New test.
---
 gcc/cp/pt.cc                            |  1 +
 gcc/cp/tree.cc                          | 33 ++++++++++++++-----------
 gcc/testsuite/g++.dg/cpp0x/overload5.C  | 12 +++++++++
 gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++
 gcc/testsuite/g++.dg/cpp0x/overload6.C  | 16 ++++++++++++
 5 files changed, 58 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 255332dc0c1..c9360240cd2 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t val)
     case CALL_EXPR:
       {
 	tree fn = CALL_EXPR_FN (arg);
+	if (TREE_TYPE (arg) == NULL_TREE)
 	  if (tree name = dependent_name (fn))
 	    {
 	      if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index c1da868732b..3a57e71b76e 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -3870,16 +3870,21 @@ decl_internal_context_p (const_tree decl)
   return !TREE_PUBLIC (decl);
 }
 
-/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
-   CALL_EXPRS.  Return whether they are equivalent.  */
+/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs.
+   Return whether their CALL_EXPR_FNs are equivalent.  */
 
 static bool
 called_fns_equal (tree t1, tree t2)
+{
+  tree fn1 = CALL_EXPR_FN (t1);
+  tree fn2 = CALL_EXPR_FN (t2);
+  if (TREE_TYPE (t1) == NULL_TREE
+      && TREE_TYPE (t2) == NULL_TREE)
     {
       /* Core 1321: dependent names are equivalent even if the overload sets
 	 are different.  But do compare explicit template arguments.  */
-  tree name1 = dependent_name (t1);
-  tree name2 = dependent_name (t2);
+      tree name1 = dependent_name (fn1);
+      tree name2 = dependent_name (fn2);
       if (name1 || name2)
 	{
 	  tree targs1 = NULL_TREE, targs2 = NULL_TREE;
@@ -3891,19 +3896,19 @@ called_fns_equal (tree t1, tree t2)
 	     of whether the function was named with a qualified- or unqualified-id.
 	     Until that's fixed, check that we aren't looking at overload sets from
 	     different scopes.  */
-      if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
-	  && (DECL_CONTEXT (get_first_fn (t1))
-	      != DECL_CONTEXT (get_first_fn (t2))))
+	  if (is_overloaded_fn (fn1) && is_overloaded_fn (fn2)
+	      && (DECL_CONTEXT (get_first_fn (fn1))
+		  != DECL_CONTEXT (get_first_fn (fn2))))
 	    return false;
 
-      if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
-	targs1 = TREE_OPERAND (t1, 1);
-      if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
-	targs2 = TREE_OPERAND (t2, 1);
+	  if (TREE_CODE (fn1) == TEMPLATE_ID_EXPR)
+	    targs1 = TREE_OPERAND (fn1, 1);
+	  if (TREE_CODE (fn2) == TEMPLATE_ID_EXPR)
+	    targs2 = TREE_OPERAND (fn2, 1);
 	  return cp_tree_equal (targs1, targs2);
 	}
-  else
-    return cp_tree_equal (t1, t2);
+    }
+  return cp_tree_equal (fn1, fn2);
 }
 
 bool comparing_override_contracts;
@@ -4037,7 +4042,7 @@ cp_tree_equal (tree t1, tree t2)
 	if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
 	  return false;
 
-	if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
+	if (!called_fns_equal (t1, t2))
 	  return false;
 
 	call_expr_arg_iterator iter1, iter2;
diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5.C b/gcc/testsuite/g++.dg/cpp0x/overload5.C
new file mode 100644
index 00000000000..e05b1594f51
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/overload5.C
@@ -0,0 +1,12 @@
+// PR c++/107461
+// { dg-do compile { target c++11 } }
+
+int f(...);
+template<class T> decltype(T() + f(0)) g(); // #1
+
+char f(int);
+template<class T> decltype(T() + f(0)) g(); // #2, distinct from #1
+
+int main() {
+  g<int>(); // { dg-error "ambiguous" }
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5a.C b/gcc/testsuite/g++.dg/cpp0x/overload5a.C
new file mode 100644
index 00000000000..037114f199c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/overload5a.C
@@ -0,0 +1,10 @@
+// PR c++/107461
+// { dg-do compile { target c++11 } }
+
+template<class T> T f();
+template<class T> decltype(T() + f<int*>()) g(); // #1
+template<class T> decltype(T() + f<char>()) g(); // #2, distinct from #1
+
+int main() {
+  g<int>(); // { dg-error "ambiguous" }
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/overload6.C b/gcc/testsuite/g++.dg/cpp0x/overload6.C
new file mode 100644
index 00000000000..1fbee0501de
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/overload6.C
@@ -0,0 +1,16 @@
+// PR c++/107461
+// { dg-do compile { target c++11 } }
+
+template<class T> T f();
+
+template<class> struct A { };
+
+template<class T> struct B {
+  template<class U, class = A<decltype(f<T>()(U()))>>
+  static void g(U);
+};
+
+int main() {
+  B<int> b;
+  B<void(*)(int)>::g(0); // { dg-bogus "no match" }
+}
-- 
2.39.1.388.g2fc9e9ca3c


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-02-06 17:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-04 20:31 [PATCH] c++: equivalence of non-dependent calls [PR107461] Patrick Palka
2023-02-04 23:42 ` Jason Merrill
2023-02-05  1:08   ` Patrick Palka
2023-02-05  1:41     ` Jason Merrill
2023-02-05  2:02       ` Jason Merrill
2023-02-05 14:57         ` Patrick Palka
2023-02-05 19:30           ` Jason Merrill
2023-02-06 17:25         ` Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).