On Fri, Nov 21, 2025 at 05:05:04PM -0500, Marek Polacek wrote:
> On Fri, Nov 21, 2025 at 10:53:49PM +0100, Daniele Sahebi wrote:
> > Changed changelog, hopefully everything is right now.
> > Also sorry this is taking so long...
> > Bootstrapped and regtested on x86_64-linux-unknown-gnu.
> > 
> > ---
> > 
> > Currently, build_over_call calls build_cplus_new in template decls, 
> > generating
> > a TARGET_EXPR that it then passes to fold_non_dependent_expr, which ends up
> > calling tsubst_expr, and since tsubst_expr doesn't handle TARGET_EXPRs, it 
> > ICEs.
> > 
> > Since there is no way for this code path to be executed without causing an
> > ICE, I believe it can be removed.
> > 
> >     PR c++/122658
> > 
> > gcc/cp/ChangeLog:
> > 
> >     * call.cc (build_over_call): Don't call build_cplus_new in
> >         template declarations
> 
> This needs a period at the end, and be indented with one \t.  You
> do not need to repost the patch just because of this.

Sorry, I've attached an updated patch.

> 
> Marek
> 
>From 187e47bec95350dd593df3913a337f469e85d5c2 Mon Sep 17 00:00:00 2001
From: Daniele Sahebi <[email protected]>
Date: Tue, 18 Nov 2025 20:58:59 +0100
Subject: [PATCH] c++: fix ICE with consteval functions in template decls
 [PR122658]

Currently, build_over_call calls build_cplus_new in template decls, generating
a TARGET_EXPR that it then passes to fold_non_dependent_expr, which ends up
calling tsubst_expr, and since tsubst_expr doesn't handle TARGET_EXPRs, it ICEs.

Since there is no way for this code path to be executed without causing an
ICE, I believe it can be removed.

	PR c++/122658

gcc/cp/ChangeLog:

	* call.cc (build_over_call): Don't call build_cplus_new in
	template declarations.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/consteval42.C: New test.

Co-authored-by: Jakub Jelinek <[email protected]>
Signed-off-by: Daniele Sahebi <[email protected]>
---
 gcc/cp/call.cc                           | 13 +++----------
 gcc/testsuite/g++.dg/cpp2a/consteval42.C | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/consteval42.C

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index f80d597b3394..7985c2d5e1bb 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -10328,18 +10328,11 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
 	TREE_NO_WARNING (expr) = true;
       if (immediate_invocation_p (fn))
 	{
-	  tree obj_arg = NULL_TREE, exprimm = expr;
+	  tree obj_arg = NULL_TREE;
 	  if (DECL_CONSTRUCTOR_P (fn))
 	    obj_arg = first_arg;
-	  if (obj_arg
-	      && is_dummy_object (obj_arg)
-	      && !type_dependent_expression_p (obj_arg))
-	    {
-	      exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain);
-	      obj_arg = NULL_TREE;
-	    }
 	  /* Look through *(const T *)&obj.  */
-	  else if (obj_arg && INDIRECT_REF_P (obj_arg))
+	  if (obj_arg && INDIRECT_REF_P (obj_arg))
 	    {
 	      tree addr = TREE_OPERAND (obj_arg, 0);
 	      STRIP_NOPS (addr);
@@ -10351,7 +10344,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
 		    obj_arg = TREE_OPERAND (addr, 0);
 		}
 	    }
-	  fold_non_dependent_expr (exprimm, complain,
+	  fold_non_dependent_expr (expr, complain,
 				   /*manifestly_const_eval=*/true,
 				   obj_arg);
 	}
diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval42.C b/gcc/testsuite/g++.dg/cpp2a/consteval42.C
new file mode 100644
index 000000000000..c75bb49f11e7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/consteval42.C
@@ -0,0 +1,21 @@
+// PR c++/122658
+// { dg-do compile { target c++20 } }
+
+struct S {
+  consteval S () noexcept { }
+  consteval S (const S &) = default;
+};
+
+template <typename T>
+S
+foo ()
+{
+  constexpr auto s = S();
+  return s;
+}
+
+S
+bar ()
+{
+  return foo <int> ();
+}
-- 
2.47.3

Reply via email to