diff mbox series

c++: another build_new_1 folding fix [PR111929]

Message ID 20231025160345.358175-1-ppalka@redhat.com
State New
Headers show
Series c++: another build_new_1 folding fix [PR111929] | expand

Commit Message

Patrick Palka Oct. 25, 2023, 4:03 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look
OK for trunk?

-- >8 --

We also need to avoid folding 'outer_nelts_check' when in a template
context to prevent an ICE on the below testcase.  This patch achieves
this by replacing the fold_build2 call with build2 (cp_fully_fold will
later fold the overall expression if appropriate).

In passing, this patch removes an unnecessary call to convert on 'nelts'
since it should always already be a size_t (and 'convert' isn't the best
conversion entry point to use anyway since it doesn't take a complain
parameter.)

	PR c++/111929

gcc/cp/ChangeLog:

	* init.cc (build_new_1): Remove unnecessary call to convert
	on 'nelts'.  Use build2 instead of fold_build2 for
	'outer_nelts_checks'.

gcc/testsuite/ChangeLog:

	* g++.dg/template/non-dependent28a.C: New test.
---
 gcc/cp/init.cc                                   | 8 ++++----
 gcc/testsuite/g++.dg/template/non-dependent28a.C | 8 ++++++++
 2 files changed, 12 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/non-dependent28a.C

Comments

Jason Merrill Oct. 25, 2023, 7:37 p.m. UTC | #1
On 10/25/23 12:03, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look
> OK for trunk?
> 
> -- >8 --
> 
> We also need to avoid folding 'outer_nelts_check' when in a template
> context to prevent an ICE on the below testcase.  This patch achieves
> this by replacing the fold_build2 call with build2 (cp_fully_fold will
> later fold the overall expression if appropriate).
> 
> In passing, this patch removes an unnecessary call to convert on 'nelts'
> since it should always already be a size_t (and 'convert' isn't the best
> conversion entry point to use anyway since it doesn't take a complain
> parameter.)
> 
> 	PR c++/111929
> 
> gcc/cp/ChangeLog:
> 
> 	* init.cc (build_new_1): Remove unnecessary call to convert
> 	on 'nelts'.

OK.

>  Use build2 instead of fold_build2 for 'outer_nelts_checks'.

Also OK, but can we skip that whole block when processing_template_decl? 
  Seems no point to build runtime checks.

Jason
diff mbox series

Patch

diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index 65d37c3c0c7..6444f0a8518 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -3261,7 +3261,7 @@  build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
       max_outer_nelts = wi::udiv_trunc (max_size, inner_size);
       max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts);
 
-      size = build2 (MULT_EXPR, sizetype, size, convert (sizetype, nelts));
+      size = build2 (MULT_EXPR, sizetype, size, nelts);
 
       if (TREE_CODE (cst_outer_nelts) == INTEGER_CST)
 	{
@@ -3293,9 +3293,9 @@  build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
 	    - wi::clz (max_outer_nelts);
 	  max_outer_nelts = (max_outer_nelts >> shift) << shift;
 
-          outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
-					   outer_nelts,
-					   max_outer_nelts_tree);
+	  outer_nelts_check = build2 (LE_EXPR, boolean_type_node,
+				      outer_nelts,
+				      max_outer_nelts_tree);
 	}
     }
 
diff --git a/gcc/testsuite/g++.dg/template/non-dependent28a.C b/gcc/testsuite/g++.dg/template/non-dependent28a.C
new file mode 100644
index 00000000000..d32520c38ee
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/non-dependent28a.C
@@ -0,0 +1,8 @@ 
+// PR c++/111929
+
+struct A { operator int(); };
+
+template<class>
+void f() {
+  new int[A()];
+}