re PR c++/83921 (GCC rejects constexpr initialization of empty aggregate.)
authorPaolo Carlini <paolo.carlini@oracle.com>
Wed, 24 Jan 2018 00:57:18 +0000 (00:57 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Wed, 24 Jan 2018 00:57:18 +0000 (00:57 +0000)
/cp
2018-01-23  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/83921
* decl.c (check_for_uninitialized_const_var): Not static; add
bool and tsubst_flags_t parameters; adjust to be used both in
constexpr context and not.
* constexpr.c (potential_constant_expression_1): Use the above.
* cp-tree.h (check_for_uninitialized_const_var): Declare.

/testsuite
2018-01-23  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/83921
* g++.dg/cpp1y/constexpr-83921-1.C: New.
* g++.dg/cpp1y/constexpr-83921-2.C: Likewise.
* g++.dg/cpp1y/constexpr-83921-3.C: Likewise.
* g++.dg/ext/stmtexpr20.C: Likewise.
* g++.dg/ext/stmtexpr21.C: Likewise.

From-SVN: r257009

gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/cp/cp-tree.h
gcc/cp/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1y/constexpr-83921-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-83921-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-83921-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/stmtexpr20.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/stmtexpr21.C [new file with mode: 0644]

index 9c2cba9..bc89041 100644 (file)
@@ -1,3 +1,12 @@
+2018-01-23  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/83921
+       * decl.c (check_for_uninitialized_const_var): Not static; add
+       bool and tsubst_flags_t parameters; adjust to be used both in
+       constexpr context and not.
+       * constexpr.c (potential_constant_expression_1): Use the above.
+       * cp-tree.h (check_for_uninitialized_const_var): Declare.
+
 2018-01-23  Jason Merrill  <jason@redhat.com>
 
        PR c++/83947 - ICE with auto declarations.
index ca7f369..4d2ee4a 100644 (file)
@@ -5707,13 +5707,9 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
                          "%<thread_local%> in %<constexpr%> context", tmp);
              return false;
            }
-         else if (!DECL_NONTRIVIALLY_INITIALIZED_P (tmp))
-           {
-             if (flags & tf_error)
-               error_at (DECL_SOURCE_LOCATION (tmp), "uninitialized "
-                         "variable %qD in %<constexpr%> context", tmp);
-             return false;
-           }
+         else if (!check_for_uninitialized_const_var
+                  (tmp, /*constexpr_context_p=*/true, flags))
+           return false;
        }
       return RECUR (tmp, want_rval);
 
index 46882dc..8983674 100644 (file)
@@ -6221,6 +6221,7 @@ extern tree finish_case_label                     (location_t, tree, tree);
 extern tree cxx_maybe_build_cleanup            (tree, tsubst_flags_t);
 extern bool check_array_designated_initializer  (constructor_elt *,
                                                 unsigned HOST_WIDE_INT);
+extern bool check_for_uninitialized_const_var   (tree, bool, tsubst_flags_t);
 
 /* in decl2.c */
 extern void record_mangling                    (tree, bool);
index 1970865..408a1b7 100644 (file)
@@ -72,7 +72,6 @@ static int check_static_variable_definition (tree, tree);
 static void record_unknown_type (tree, const char *);
 static tree builtin_function_1 (tree, tree, bool);
 static int member_function_or_else (tree, tree, enum overload_flags);
-static void check_for_uninitialized_const_var (tree);
 static tree local_variable_p_walkfn (tree *, int *, void *);
 static const char *tag_name (enum tag_types);
 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
@@ -5545,10 +5544,14 @@ maybe_commonize_var (tree decl)
     }
 }
 
-/* Issue an error message if DECL is an uninitialized const variable.  */
+/* Issue an error message if DECL is an uninitialized const variable.
+   CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
+   context from potential_constant_expression.  Returns true if all is well,
+   false otherwise.  */
 
-static void
-check_for_uninitialized_const_var (tree decl)
+bool
+check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
+                                  tsubst_flags_t complain)
 {
   tree type = strip_array_types (TREE_TYPE (decl));
 
@@ -5557,26 +5560,38 @@ check_for_uninitialized_const_var (tree decl)
      7.1.6 */
   if (VAR_P (decl)
       && TREE_CODE (type) != REFERENCE_TYPE
-      && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
-      && !DECL_INITIAL (decl))
+      && (constexpr_context_p
+         || CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
+      && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
     {
       tree field = default_init_uninitialized_part (type);
       if (!field)
-       return;
+       return true;
 
-      if (CP_TYPE_CONST_P (type))
-       permerror (DECL_SOURCE_LOCATION (decl),
-                  "uninitialized const %qD", decl);
-      else
+      if (!constexpr_context_p)
        {
-         if (!is_instantiation_of_constexpr (current_function_decl))
-           error_at (DECL_SOURCE_LOCATION (decl),
-                     "uninitialized variable %qD in %<constexpr%> function",
-                     decl);
-         cp_function_chain->invalid_constexpr = true;
+         if (CP_TYPE_CONST_P (type))
+           {
+             if (complain & tf_error)
+               permerror (DECL_SOURCE_LOCATION (decl),
+                          "uninitialized const %qD", decl);
+           }
+         else
+           {
+             if (!is_instantiation_of_constexpr (current_function_decl)
+                 && (complain & tf_error))
+               error_at (DECL_SOURCE_LOCATION (decl),
+                         "uninitialized variable %qD in %<constexpr%> "
+                         "function", decl);
+             cp_function_chain->invalid_constexpr = true;
+           }
        }
+      else if (complain & tf_error)
+       error_at (DECL_SOURCE_LOCATION (decl),
+                 "uninitialized variable %qD in %<constexpr%> context",
+                 decl);
 
-      if (CLASS_TYPE_P (type))
+      if (CLASS_TYPE_P (type) && (complain & tf_error))
        {
          tree defaulted_ctor;
 
@@ -5591,7 +5606,11 @@ check_for_uninitialized_const_var (tree decl)
                  "and the implicitly-defined constructor does not "
                  "initialize %q#D", field);
        }
+
+      return false;
     }
+
+  return true;
 }
 \f
 /* Structure holding the current initializer being processed by reshape_init.
@@ -6252,7 +6271,8 @@ check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
          flags |= LOOKUP_ALREADY_DIGESTED;
        }
       else if (!init)
-       check_for_uninitialized_const_var (decl);
+       check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
+                                          tf_warning_or_error);
       /* Do not reshape constructors of vectors (they don't need to be
         reshaped.  */
       else if (BRACE_ENCLOSED_INITIALIZER_P (init))
@@ -6379,7 +6399,8 @@ check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
        diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
                                                  /*complain=*/true);
 
-      check_for_uninitialized_const_var (decl);
+      check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
+                                        tf_warning_or_error);
     }
 
   if (init && init != error_mark_node)
index fb7e457..23fdb9f 100644 (file)
@@ -1,3 +1,12 @@
+2018-01-23  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/83921
+       * g++.dg/cpp1y/constexpr-83921-1.C: New.
+       * g++.dg/cpp1y/constexpr-83921-2.C: Likewise.
+       * g++.dg/cpp1y/constexpr-83921-3.C: Likewise.
+       * g++.dg/ext/stmtexpr20.C: Likewise.
+       * g++.dg/ext/stmtexpr21.C: Likewise.
+
 2018-01-23  David Malcolm  <dmalcolm@redhat.com>
 
        PR c++/83974
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-83921-1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-83921-1.C
new file mode 100644 (file)
index 0000000..e4b8d1f
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/83921
+// { dg-do compile { target c++14 } }
+
+struct Foo { };
+constexpr void test() { Foo f; }
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-83921-2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-83921-2.C
new file mode 100644 (file)
index 0000000..80393fc
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/83921
+// { dg-do compile { target c++14 } }
+
+struct Foo { Foo() = default; };
+constexpr void test() { Foo f; }
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-83921-3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-83921-3.C
new file mode 100644 (file)
index 0000000..4b1ed5c
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/83921
+// { dg-do compile { target c++14 } }
+
+struct Foo { int m; };
+constexpr void test() { Foo f; }  // { dg-error "uninitialized" }
diff --git a/gcc/testsuite/g++.dg/ext/stmtexpr20.C b/gcc/testsuite/g++.dg/ext/stmtexpr20.C
new file mode 100644 (file)
index 0000000..33355e3
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/83921
+// { dg-options "" }
+// { dg-do compile { target c++11 } }
+
+struct test { const int *addr; };
+
+const test* setup()
+{
+  static constexpr test atest =
+    { ({ int inner = 1; (const int*)(0); }) };
+
+  return &atest;
+}
diff --git a/gcc/testsuite/g++.dg/ext/stmtexpr21.C b/gcc/testsuite/g++.dg/ext/stmtexpr21.C
new file mode 100644 (file)
index 0000000..259cb2f
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/83921
+// { dg-options "" }
+// { dg-do compile { target c++11 } }
+
+struct test { const int *addr; };
+
+const test* setup()
+{
+  static constexpr test atest =
+    { ({ int inner; (const int*)(0); }) };  // { dg-error "uninitialized" }
+
+  return &atest;
+}