diff mbox series

c++: array new with value-initialization [PR115645]

Message ID 20240702204359.492020-1-polacek@redhat.com
State New
Headers show
Series c++: array new with value-initialization [PR115645] | expand

Commit Message

Marek Polacek July 2, 2024, 8:43 p.m. UTC
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/branches?

-- >8 --
This extends the r11-5179 fix which doesn't work with multidimensional
arrays.  In particular,

  struct S {
    explicit S() { }
  };
  auto p = new S[1][1]();

should not say "converting to S from initializer list would use
explicit constructor" because there's no {}.  However, since we
went into the block where we create a {}, we got confused.  We
should not have gotten there but we did because array_p was true.

This patch refines the check once more.

	PR c++/115645

gcc/cp/ChangeLog:

	* init.cc (build_new): Don't do any deduction for arrays with
	bounds if it's value-initialized.

gcc/testsuite/ChangeLog:

	* g++.dg/expr/anew7.C: New test.
---
 gcc/cp/init.cc                    | 12 ++++++++----
 gcc/testsuite/g++.dg/expr/anew7.C | 13 +++++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/expr/anew7.C


base-commit: 1250540a98e0a1dfa4d7834672d88d8543ea70b1

Comments

Jason Merrill July 3, 2024, 5:23 p.m. UTC | #1
On 7/2/24 4:43 PM, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/branches?

OK.

> -- >8 --
> This extends the r11-5179 fix which doesn't work with multidimensional
> arrays.  In particular,
> 
>    struct S {
>      explicit S() { }
>    };
>    auto p = new S[1][1]();
> 
> should not say "converting to S from initializer list would use
> explicit constructor" because there's no {}.  However, since we
> went into the block where we create a {}, we got confused.  We
> should not have gotten there but we did because array_p was true.
> 
> This patch refines the check once more.
> 
> 	PR c++/115645
> 
> gcc/cp/ChangeLog:
> 
> 	* init.cc (build_new): Don't do any deduction for arrays with
> 	bounds if it's value-initialized.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/expr/anew7.C: New test.
> ---
>   gcc/cp/init.cc                    | 12 ++++++++----
>   gcc/testsuite/g++.dg/expr/anew7.C | 13 +++++++++++++
>   2 files changed, 21 insertions(+), 4 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/expr/anew7.C
> 
> diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
> index 826a31c4a84..e9561c146d7 100644
> --- a/gcc/cp/init.cc
> +++ b/gcc/cp/init.cc
> @@ -4005,10 +4005,14 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type,
>     /* P1009: Array size deduction in new-expressions.  */
>     const bool array_p = TREE_CODE (type) == ARRAY_TYPE;
>     if (*init
> -      /* If ARRAY_P, we have to deduce the array bound.  For C++20 paren-init,
> -	 we have to process the parenthesized-list.  But don't do it for (),
> -	 which is value-initialization, and INIT should stay empty.  */
> -      && (array_p || (cxx_dialect >= cxx20 && nelts && !(*init)->is_empty ())))
> +      /* If the array didn't specify its bound, we have to deduce it.  */
> +      && ((array_p && !TYPE_DOMAIN (type))
> +	  /* For C++20 array with parenthesized-init, we have to process
> +	     the parenthesized-list.  But don't do it for (), which is
> +	     value-initialization, and INIT should stay empty.  */
> +	  || (cxx_dialect >= cxx20
> +	      && (array_p || nelts)
> +	      && !(*init)->is_empty ())))
>       {
>         /* This means we have 'new T[]()'.  */
>         if ((*init)->is_empty ())
> diff --git a/gcc/testsuite/g++.dg/expr/anew7.C b/gcc/testsuite/g++.dg/expr/anew7.C
> new file mode 100644
> index 00000000000..ead5536e109
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/expr/anew7.C
> @@ -0,0 +1,13 @@
> +// PR c++/115645
> +// { dg-do compile { target c++11 } }
> +
> +struct S {
> +  explicit S() { }
> +};
> +
> +auto p = new S[1][1]();
> +auto q = new S[1][1]{}; // { dg-error "explicit" }
> +auto r = new S[1]();
> +auto s = new S[1]{}; // { dg-error "explicit" }
> +auto t = new S[1][1][1]();
> +auto u = new S[1][1][1]{}; // { dg-error "explicit" }
> 
> base-commit: 1250540a98e0a1dfa4d7834672d88d8543ea70b1
diff mbox series

Patch

diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index 826a31c4a84..e9561c146d7 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -4005,10 +4005,14 @@  build_new (location_t loc, vec<tree, va_gc> **placement, tree type,
   /* P1009: Array size deduction in new-expressions.  */
   const bool array_p = TREE_CODE (type) == ARRAY_TYPE;
   if (*init
-      /* If ARRAY_P, we have to deduce the array bound.  For C++20 paren-init,
-	 we have to process the parenthesized-list.  But don't do it for (),
-	 which is value-initialization, and INIT should stay empty.  */
-      && (array_p || (cxx_dialect >= cxx20 && nelts && !(*init)->is_empty ())))
+      /* If the array didn't specify its bound, we have to deduce it.  */
+      && ((array_p && !TYPE_DOMAIN (type))
+	  /* For C++20 array with parenthesized-init, we have to process
+	     the parenthesized-list.  But don't do it for (), which is
+	     value-initialization, and INIT should stay empty.  */
+	  || (cxx_dialect >= cxx20
+	      && (array_p || nelts)
+	      && !(*init)->is_empty ())))
     {
       /* This means we have 'new T[]()'.  */
       if ((*init)->is_empty ())
diff --git a/gcc/testsuite/g++.dg/expr/anew7.C b/gcc/testsuite/g++.dg/expr/anew7.C
new file mode 100644
index 00000000000..ead5536e109
--- /dev/null
+++ b/gcc/testsuite/g++.dg/expr/anew7.C
@@ -0,0 +1,13 @@ 
+// PR c++/115645
+// { dg-do compile { target c++11 } }
+
+struct S {
+  explicit S() { }
+};
+
+auto p = new S[1][1]();
+auto q = new S[1][1]{}; // { dg-error "explicit" }
+auto r = new S[1]();
+auto s = new S[1]{}; // { dg-error "explicit" }
+auto t = new S[1][1][1]();
+auto u = new S[1][1][1]{}; // { dg-error "explicit" }