public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: broken direct-init with trailing array member [PR114439]
@ 2024-03-25 21:59 Marek Polacek
  2024-03-25 22:16 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2024-03-25 21:59 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
can_init_array_with_p is wrongly saying that the init for 's' here:

  struct S {
    int *list = arr;
    int arr[];
  };

  struct A {
    A() {}
    S s[2]{};
  };

is invalid.  But as process_init_constructor_array says, for "non-constant
initialization of trailing elements with no explicit initializers" we use
a VEC_INIT_EXPR wrapped in a TARGET_EXPR, built in process_init_constructor.

Unfortunately we didn't have a test for this scenario so I didn't
realize can_init_array_with_p must handle it.

	PR c++/114439

gcc/cp/ChangeLog:

	* init.cc (can_init_array_with_p): Return true for a VEC_INIT_EXPR
	wrapped in a TARGET_EXPR.

gcc/testsuite/ChangeLog:

	* g++.dg/init/array65.C: New test.
---
 gcc/cp/init.cc                      |  6 ++++-
 gcc/testsuite/g++.dg/init/array65.C | 38 +++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/init/array65.C

diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index dbd37d47cbf..a93ce00800c 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -950,12 +950,16 @@ can_init_array_with_p (tree type, tree init)
      mem-initializers of a constructor.  */
   if (DECL_DEFAULTED_FN (current_function_decl))
     return true;
-  /* As an extension, we allow copying from a compound literal.  */
   if (TREE_CODE (init) == TARGET_EXPR)
     {
       init = TARGET_EXPR_INITIAL (init);
+      /* As an extension, we allow copying from a compound literal.  */
       if (TREE_CODE (init) == CONSTRUCTOR)
 	return CONSTRUCTOR_C99_COMPOUND_LITERAL (init);
+      /* VEC_INIT_EXPR is used for non-constant initialization of trailing
+	 elements with no explicit initializers.  */
+      else if (TREE_CODE (init) == VEC_INIT_EXPR)
+	return true;
     }
 
   return false;
diff --git a/gcc/testsuite/g++.dg/init/array65.C b/gcc/testsuite/g++.dg/init/array65.C
new file mode 100644
index 00000000000..0b144f45a9d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/array65.C
@@ -0,0 +1,38 @@
+// PR c++/114439
+// { dg-do compile { target c++11 } }
+
+struct S {
+  int *list = arr;
+  __extension__ int arr[];
+};
+
+struct R {
+  int *list = arr;
+  int arr[2];
+};
+
+struct A {
+  A() {}
+  S s[2]{};
+};
+
+struct A2 {
+  A2() {}
+  S s[2]{ {}, {} };
+};
+
+struct B {
+  B() {}
+  R r[2]{};
+};
+
+struct B2 {
+  B2() {}
+  R r[2]{ {}, {} };
+};
+
+struct S1 { S1(); };
+struct S2 {
+  S2() {}
+  S1 a[1] {};
+};

base-commit: 18555b914316e8c1fb11ee821f2ee839d834e58e
-- 
2.44.0


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

* Re: [PATCH] c++: broken direct-init with trailing array member [PR114439]
  2024-03-25 21:59 [PATCH] c++: broken direct-init with trailing array member [PR114439] Marek Polacek
@ 2024-03-25 22:16 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2024-03-25 22:16 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 3/25/24 17:59, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> -- >8 --
> can_init_array_with_p is wrongly saying that the init for 's' here:
> 
>    struct S {
>      int *list = arr;
>      int arr[];
>    };
> 
>    struct A {
>      A() {}
>      S s[2]{};
>    };
> 
> is invalid.  But as process_init_constructor_array says, for "non-constant
> initialization of trailing elements with no explicit initializers" we use
> a VEC_INIT_EXPR wrapped in a TARGET_EXPR, built in process_init_constructor.
> 
> Unfortunately we didn't have a test for this scenario so I didn't
> realize can_init_array_with_p must handle it.
> 
> 	PR c++/114439
> 
> gcc/cp/ChangeLog:
> 
> 	* init.cc (can_init_array_with_p): Return true for a VEC_INIT_EXPR
> 	wrapped in a TARGET_EXPR.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/init/array65.C: New test.
> ---
>   gcc/cp/init.cc                      |  6 ++++-
>   gcc/testsuite/g++.dg/init/array65.C | 38 +++++++++++++++++++++++++++++
>   2 files changed, 43 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/init/array65.C
> 
> diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
> index dbd37d47cbf..a93ce00800c 100644
> --- a/gcc/cp/init.cc
> +++ b/gcc/cp/init.cc
> @@ -950,12 +950,16 @@ can_init_array_with_p (tree type, tree init)
>        mem-initializers of a constructor.  */
>     if (DECL_DEFAULTED_FN (current_function_decl))
>       return true;
> -  /* As an extension, we allow copying from a compound literal.  */
>     if (TREE_CODE (init) == TARGET_EXPR)
>       {
>         init = TARGET_EXPR_INITIAL (init);
> +      /* As an extension, we allow copying from a compound literal.  */
>         if (TREE_CODE (init) == CONSTRUCTOR)
>   	return CONSTRUCTOR_C99_COMPOUND_LITERAL (init);
> +      /* VEC_INIT_EXPR is used for non-constant initialization of trailing
> +	 elements with no explicit initializers.  */
> +      else if (TREE_CODE (init) == VEC_INIT_EXPR)
> +	return true;
>       }
>   
>     return false;
> diff --git a/gcc/testsuite/g++.dg/init/array65.C b/gcc/testsuite/g++.dg/init/array65.C
> new file mode 100644
> index 00000000000..0b144f45a9d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/init/array65.C
> @@ -0,0 +1,38 @@
> +// PR c++/114439
> +// { dg-do compile { target c++11 } }
> +
> +struct S {
> +  int *list = arr;
> +  __extension__ int arr[];
> +};
> +
> +struct R {
> +  int *list = arr;
> +  int arr[2];
> +};
> +
> +struct A {
> +  A() {}
> +  S s[2]{};
> +};
> +
> +struct A2 {
> +  A2() {}
> +  S s[2]{ {}, {} };
> +};
> +
> +struct B {
> +  B() {}
> +  R r[2]{};
> +};
> +
> +struct B2 {
> +  B2() {}
> +  R r[2]{ {}, {} };
> +};
> +
> +struct S1 { S1(); };
> +struct S2 {
> +  S2() {}
> +  S1 a[1] {};
> +};
> 
> base-commit: 18555b914316e8c1fb11ee821f2ee839d834e58e


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

end of thread, other threads:[~2024-03-25 22:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-25 21:59 [PATCH] c++: broken direct-init with trailing array member [PR114439] Marek Polacek
2024-03-25 22:16 ` 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).