[gcc(refs/vendors/redhat/heads/gcc-8-branch)] c++: Fix constexpr vs. omitted aggregate init.

Jakub Jelinek jakub@gcc.gnu.org
Thu Sep 17 16:45:17 GMT 2020


https://gcc.gnu.org/g:a61be43ef6942e4232137a6beadef099d5c7ebc3

commit a61be43ef6942e4232137a6beadef099d5c7ebc3
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Feb 24 16:22:45 2020 -0500

    c++: Fix constexpr vs. omitted aggregate init.
    
    Value-initialization is importantly different from {}-initialization for
    this testcase, where the former calls the deleted S constructor and the
    latter initializes S happily.
    
    gcc/cp/ChangeLog
    2020-02-24  Jason Merrill  <jason@redhat.com>
    
            PR c++/90951
            * constexpr.c (cxx_eval_array_reference): {}-initialize missing
            elements instead of value-initializing them.

Diff:
---
 gcc/cp/ChangeLog                               |  6 ++++++
 gcc/cp/constexpr.c                             | 12 ++++++++++--
 gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C | 10 ++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 116445ba46f..ffb25f3ab95 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2020-02-24  Jason Merrill  <jason@redhat.com>
+
+	PR c++/90951
+	* constexpr.c (cxx_eval_array_reference): {}-initialize missing
+	elements instead of value-initializing them.
+
 2020-02-24  Jason Merrill  <jason@redhat.com>
 
 	PR c++/93140
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 4ae97d2afba..fc2010cb548 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -2486,8 +2486,16 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
     }
 
   /* If it's within the array bounds but doesn't have an explicit
-     initializer, it's value-initialized.  */
-  tree val = build_value_init (elem_type, tf_warning_or_error);
+     initializer, it's initialized from {}.  But use build_value_init
+     directly for non-aggregates to avoid creating a garbage CONSTRUCTOR.  */
+  tree val;
+  if (CP_AGGREGATE_TYPE_P (elem_type))
+    {
+      tree empty_ctor = build_constructor (init_list_type_node, NULL);
+      val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
+    }
+  else
+    val = build_value_init (elem_type, tf_warning_or_error);
   return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
 				       overflow_p);
 }
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C
new file mode 100644
index 00000000000..538969830ba
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C
@@ -0,0 +1,10 @@
+// PR c++/90951
+// { dg-do compile { target c++11 } }
+
+#define assert(expr) static_assert (expr, #expr)
+
+struct S { const char a[2]; };
+
+constexpr struct S a[1][1][1] = { };
+
+assert ('\0' == *a[0][0][0].a);


More information about the Gcc-cvs mailing list