PR c++/80935 - wrong C++17 error with lambda
authorJason Merrill <jason@redhat.com>
Tue, 29 Aug 2017 19:51:30 +0000 (15:51 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Tue, 29 Aug 2017 19:51:30 +0000 (15:51 -0400)
* decl.c (check_for_uninitialized_const_var): Check
is_instantiation_of_constexpr.
* constexpr.c (ensure_literal_type_for_constexpr_object): Check
is_instantiation_of_constexpr.
(potential_constant_expression_1): Check var_in_maybe_constexpr_fn.

From-SVN: r251429

gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/cp/decl.c
gcc/testsuite/g++.dg/cpp1z/constexpr-lambda16.C [new file with mode: 0644]

index 08a8ac8c1b79ab684e5e69e40a19f6209d8a4be2..4d6df428efd4ca23db5fe5c3a4df70263bb27f29 100644 (file)
@@ -1,3 +1,12 @@
+2017-08-28  Jason Merrill  <jason@redhat.com>
+
+       PR c++/80935 - wrong C++17 error with lambda
+       * decl.c (check_for_uninitialized_const_var): Check
+       is_instantiation_of_constexpr.
+       * constexpr.c (ensure_literal_type_for_constexpr_object): Check
+       is_instantiation_of_constexpr.
+       (potential_constant_expression_1): Check var_in_maybe_constexpr_fn.
+
 2017-08-23  Jason Merrill  <jason@redhat.com>
 
        * lambda.c (build_lambda_object): Check for error_mark_node.
index daeec9dedbcdc9970da9f4ca42ba82fb7c9df0c6..f3e868cff022b7599feee833ddccf32909108d92 100644 (file)
@@ -100,7 +100,7 @@ ensure_literal_type_for_constexpr_object (tree decl)
            }
          else
            {
-             if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
+             if (!is_instantiation_of_constexpr (current_function_decl))
                {
                  error ("variable %qD of non-literal type %qT in %<constexpr%> "
                         "function", decl, type);
@@ -5335,8 +5335,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
         STRIP_NOPS (x);
         if (is_this_parameter (x) && !is_capture_proxy (x))
          {
-           if (DECL_CONTEXT (x)
-               && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
+           if (!var_in_maybe_constexpr_fn (x))
              {
                if (flags & tf_error)
                  error_at (loc, "use of %<this%> in a constant expression");
index ff3127e4a6049b76a4cd52b33a86869c5df9412f..23829b0f18e2968cb9089d7e1fba0202ed71db43 100644 (file)
@@ -5525,9 +5525,10 @@ check_for_uninitialized_const_var (tree decl)
                   "uninitialized const %qD", decl);
       else
        {
-         error_at (DECL_SOURCE_LOCATION (decl),
-                   "uninitialized variable %qD in %<constexpr%> function",
-                   decl);
+         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;
        }
 
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda16.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda16.C
new file mode 100644 (file)
index 0000000..ad5d885
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/80642
+// { dg-do compile { target c++14 } }
+
+int main()
+{
+  [](auto i)
+    {
+      if (i)
+        {
+         int j;
+         static int k;
+         return i + j;
+        }
+      return i;
+    }(0);
+}