c++: Disable -Winit-list-lifetime in unevaluated operand [PR97632]
authorMarek Polacek <polacek@redhat.com>
Thu, 29 Oct 2020 19:08:31 +0000 (15:08 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 3 Nov 2020 15:09:00 +0000 (10:09 -0500)
Jon suggested turning this warning off when we're not actually
evaluating the operand.  This patch does that.

gcc/cp/ChangeLog:

PR c++/97632
* init.c (build_new_1): Disable -Winit-list-lifetime for an unevaluated
operand.

gcc/testsuite/ChangeLog:

PR c++/97632
* g++.dg/warn/Winit-list4.C: New test.

gcc/cp/init.c
gcc/testsuite/g++.dg/warn/Winit-list4.C [new file with mode: 0644]

index 1bddb6555dc182294ed0c9c39f66c3be4d4e0b3d..ffb84ea5b092204bba706ac5918cbedf726785d9 100644 (file)
@@ -2957,7 +2957,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
       return error_mark_node;
     }
 
-  if (is_std_init_list (elt_type))
+  if (is_std_init_list (elt_type) && !cp_unevaluated_operand)
     warning (OPT_Winit_list_lifetime,
             "%<new%> of %<initializer_list%> does not "
             "extend the lifetime of the underlying array");
diff --git a/gcc/testsuite/g++.dg/warn/Winit-list4.C b/gcc/testsuite/g++.dg/warn/Winit-list4.C
new file mode 100644 (file)
index 0000000..d136187
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/97632
+// { dg-do compile { target c++20 } }
+// Test we don't warn in an unevaluated operand.
+
+#include <initializer_list>
+
+template<typename _Tp>
+concept default_initializable
+  = requires
+    {
+      _Tp{};
+      (void) ::new _Tp; // { dg-bogus "does not extend the lifetime" }
+    };
+
+static_assert(default_initializable<std::initializer_list<int>>);