public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: spurious ADDR_EXPR after overload set pruning [PR107461]
@ 2023-02-02 18:09 Patrick Palka
  2023-02-02 21:26 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2023-02-02 18:09 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

Here the ahead-of-time overload set pruning in finish_call_expr is
unintentionally returning a CALL_EXPR whose pruned callee is wrapped in
an ADDR_EXPR, despite the original callee not being wrapped in an
ADDR_EXPR.  This ends up causing a bogus declaration matching error in
the below testcase because the call to min in #1 is expressed as a
CALL_EXPR to ADDR_EXPR to FUNCTION_DECL, whereas the level-lowered call
to min in #2 is expressed instead as a CALL_EXPR to FUNCTION_DECL.

This patch fixes this by stripping this ADDR_EXPR appropriately.
Thus the first call to min now gets expresssed as a CALL_EXPR to
FUNCTION_DECL, matching the form it had before r12-6075-g2decd2cabe5a4f.

Bootstrapped and regtested on x86_64-pc-linu-xgnu, does this look OK
for trunk and 12?

	PR c++/107461

gcc/cp/ChangeLog:

	* semantics.cc (finish_call_expr): Strip ADDR_EXPR from
	the selected callee during overload set pruning.

gcc/testsuite/ChangeLog:

	* g++.dg/template/friend75.C: New test.
---
 gcc/cp/semantics.cc                      | 15 +++++++++-----
 gcc/testsuite/g++.dg/template/friend75.C | 26 ++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/friend75.C

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index b3afea85196..fe9262a257f 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -2957,13 +2957,18 @@ finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
       if (TREE_CODE (result) == CALL_EXPR
 	  && really_overloaded_fn (orig_fn))
 	{
-	  orig_fn = CALL_EXPR_FN (result);
-	  if (TREE_CODE (orig_fn) == COMPONENT_REF)
+	  tree sel_fn = CALL_EXPR_FN (result);
+	  if (TREE_CODE (sel_fn) == COMPONENT_REF)
 	    {
 	      /* The non-dependent result of build_new_method_call.  */
-	      orig_fn = TREE_OPERAND (orig_fn, 1);
-	      gcc_assert (BASELINK_P (orig_fn));
-	    }
+	      sel_fn = TREE_OPERAND (sel_fn, 1);
+	      gcc_assert (BASELINK_P (sel_fn));
+	    }
+	  else if (TREE_CODE (sel_fn) == ADDR_EXPR)
+	    /* Undo the ADDR_EXPR callee wrapping performed by build_over_call
+	       since the original callee didn't have it.  */
+	    sel_fn = TREE_OPERAND (sel_fn, 0);
+	  orig_fn = sel_fn;
 	}
 
       result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
diff --git a/gcc/testsuite/g++.dg/template/friend75.C b/gcc/testsuite/g++.dg/template/friend75.C
new file mode 100644
index 00000000000..800d3043c8a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/friend75.C
@@ -0,0 +1,26 @@
+// PR c++/107461
+// { dg-do compile { target c++11 } }
+
+template<class T>
+constexpr T min(T t0, T t1) {
+  return t0 < t1 ? t0 : t1;
+}
+
+template<int MAX>
+struct Matrix;
+
+template<int MAXOP, int other_MAXOP>
+Matrix<min(MAXOP, other_MAXOP)>
+operator+(Matrix<MAXOP> const& lhs, Matrix<other_MAXOP> const& rhs); // #1
+
+template<int MAX>
+struct Matrix {
+  template<int MAXOP, int other_MAXOP>
+  friend Matrix<min(MAXOP, other_MAXOP)>
+  operator+(Matrix<MAXOP> const& lhs, Matrix<other_MAXOP> const& rhs); // #2
+};
+
+void f() {
+  Matrix<1> a;
+  a+a;
+}
-- 
2.39.1.388.g2fc9e9ca3c


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

* Re: [PATCH] c++: spurious ADDR_EXPR after overload set pruning [PR107461]
  2023-02-02 18:09 [PATCH] c++: spurious ADDR_EXPR after overload set pruning [PR107461] Patrick Palka
@ 2023-02-02 21:26 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-02-02 21:26 UTC (permalink / raw)
  To: Patrick Palka, gcc-patches

On 2/2/23 13:09, Patrick Palka wrote:
> Here the ahead-of-time overload set pruning in finish_call_expr is
> unintentionally returning a CALL_EXPR whose pruned callee is wrapped in
> an ADDR_EXPR, despite the original callee not being wrapped in an
> ADDR_EXPR.  This ends up causing a bogus declaration matching error in
> the below testcase because the call to min in #1 is expressed as a
> CALL_EXPR to ADDR_EXPR to FUNCTION_DECL, whereas the level-lowered call
> to min in #2 is expressed instead as a CALL_EXPR to FUNCTION_DECL.
> 
> This patch fixes this by stripping this ADDR_EXPR appropriately.
> Thus the first call to min now gets expresssed as a CALL_EXPR to
> FUNCTION_DECL, matching the form it had before r12-6075-g2decd2cabe5a4f.
> 
> Bootstrapped and regtested on x86_64-pc-linu-xgnu, does this look OK
> for trunk and 12?

OK.  As a future direction, I'd think we want to do the same pruning for 
other forms of "FN".

Jason

> 	PR c++/107461
> 
> gcc/cp/ChangeLog:
> 
> 	* semantics.cc (finish_call_expr): Strip ADDR_EXPR from
> 	the selected callee during overload set pruning.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/friend75.C: New test.
> ---
>   gcc/cp/semantics.cc                      | 15 +++++++++-----
>   gcc/testsuite/g++.dg/template/friend75.C | 26 ++++++++++++++++++++++++
>   2 files changed, 36 insertions(+), 5 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/template/friend75.C
> 
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index b3afea85196..fe9262a257f 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -2957,13 +2957,18 @@ finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
>         if (TREE_CODE (result) == CALL_EXPR
>   	  && really_overloaded_fn (orig_fn))
>   	{
> -	  orig_fn = CALL_EXPR_FN (result);
> -	  if (TREE_CODE (orig_fn) == COMPONENT_REF)
> +	  tree sel_fn = CALL_EXPR_FN (result);
> +	  if (TREE_CODE (sel_fn) == COMPONENT_REF)
>   	    {
>   	      /* The non-dependent result of build_new_method_call.  */
> -	      orig_fn = TREE_OPERAND (orig_fn, 1);
> -	      gcc_assert (BASELINK_P (orig_fn));
> -	    }
> +	      sel_fn = TREE_OPERAND (sel_fn, 1);
> +	      gcc_assert (BASELINK_P (sel_fn));
> +	    }
> +	  else if (TREE_CODE (sel_fn) == ADDR_EXPR)
> +	    /* Undo the ADDR_EXPR callee wrapping performed by build_over_call
> +	       since the original callee didn't have it.  */
> +	    sel_fn = TREE_OPERAND (sel_fn, 0);
> +	  orig_fn = sel_fn;
>   	}
>   
>         result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
> diff --git a/gcc/testsuite/g++.dg/template/friend75.C b/gcc/testsuite/g++.dg/template/friend75.C
> new file mode 100644
> index 00000000000..800d3043c8a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/friend75.C
> @@ -0,0 +1,26 @@
> +// PR c++/107461
> +// { dg-do compile { target c++11 } }
> +
> +template<class T>
> +constexpr T min(T t0, T t1) {
> +  return t0 < t1 ? t0 : t1;
> +}
> +
> +template<int MAX>
> +struct Matrix;
> +
> +template<int MAXOP, int other_MAXOP>
> +Matrix<min(MAXOP, other_MAXOP)>
> +operator+(Matrix<MAXOP> const& lhs, Matrix<other_MAXOP> const& rhs); // #1
> +
> +template<int MAX>
> +struct Matrix {
> +  template<int MAXOP, int other_MAXOP>
> +  friend Matrix<min(MAXOP, other_MAXOP)>
> +  operator+(Matrix<MAXOP> const& lhs, Matrix<other_MAXOP> const& rhs); // #2
> +};
> +
> +void f() {
> +  Matrix<1> a;
> +  a+a;
> +}


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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-02 18:09 [PATCH] c++: spurious ADDR_EXPR after overload set pruning [PR107461] Patrick Palka
2023-02-02 21:26 ` Jason Merrill

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).