public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Do not warn about lifetime of std::initializer_list<T>& [PR102482]
@ 2021-10-01 20:58 Jonathan Wakely
  2021-10-06 23:49 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2021-10-01 20:58 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill, Marek Polacek

[-- Attachment #1: Type: text/plain, Size: 460 bytes --]

An initializer-list constructor taking a non-const lvalue cannot be
called with a temporary, so the array's lifetime probably doesn't end
with the full expression. -Winit-list-lifetime should not warn for that
case.

	PR c++/102482

gcc/cp/ChangeLog:

	* init.c (maybe_warn_list_ctor): Do not warn for a reference to
	a non-const std::initializer_list.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Winit-list5.C: New test.



Tested x86_64-linux. OK for trunk?



[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2876 bytes --]

commit 474f2ae9c9f8312d9c732094bfb1c8da7342d6a9
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Sep 29 21:19:49 2021

    c++: Do not warn about lifetime of std::initializer_list<T>& [PR102482]
    
    An initializer-list constructor taking a non-const lvalue cannot be
    called with a temporary, so the array's lifetime probably doesn't end
    with the full expression. -Winit-list-lifetime should not warn for that
    case.
    
            PR c++/102482
    
    gcc/cp/ChangeLog:
    
            * init.c (maybe_warn_list_ctor): Do not warn for a reference to
            a non-const std::initializer_list.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/warn/Winit-list5.C: New test.

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 1426f9a5434..771a19bc402 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -749,8 +749,15 @@ maybe_warn_list_ctor (tree member, tree init)
       || !is_list_ctor (current_function_decl))
     return;
 
-  tree parms = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl);
-  tree initlist = non_reference (TREE_VALUE (parms));
+  tree parm = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl);
+  parm = TREE_VALUE (parm);
+  tree initlist = non_reference (parm);
+
+  /* Do not warn if the parameter is an lvalue reference to non-const.  */
+  if (TYPE_REF_P (parm) && !TYPE_REF_IS_RVALUE (parm)
+      && !CP_TYPE_CONST_P (initlist))
+    return;
+
   tree targs = CLASSTYPE_TI_ARGS (initlist);
   tree elttype = TREE_VEC_ELT (targs, 0);
 
diff --git a/gcc/testsuite/g++.dg/warn/Winit-list5.C b/gcc/testsuite/g++.dg/warn/Winit-list5.C
new file mode 100644
index 00000000000..07b3a69e46b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Winit-list5.C
@@ -0,0 +1,61 @@
+// PR c++/102482
+// { dg-do compile { target c++11 } }
+// Test we don't warn for non-const lvalue refs.
+
+#include <initializer_list>
+
+struct X { };
+
+struct span
+{
+  span(std::initializer_list<int>& il)
+  : begin(il.begin()) // { dg-bogus "initializer_list" }
+  { }
+
+  const int* begin;
+};
+
+struct span_warn
+{
+  span_warn(std::initializer_list<int> il)
+  : begin(il.begin()) // { dg-warning "initializer_list" }
+  { }
+
+  const int* begin;
+};
+
+struct span_warn2
+{
+  span_warn2(std::initializer_list<int>&& il)
+  : begin(il.begin()) // { dg-warning "initializer_list" }
+  { }
+
+  const int* begin;
+};
+
+struct span_warn3
+{
+  span_warn3(std::initializer_list<int> const& il)
+  : begin(il.begin()) // { dg-warning "initializer_list" }
+  { }
+
+  const int* begin;
+};
+
+struct span_warn4
+{
+  span_warn4(std::initializer_list<int> const il)
+  : begin(il.begin()) // { dg-warning "initializer_list" }
+  { }
+
+  const int* begin;
+};
+
+struct span_warn5
+{
+  span_warn5(std::initializer_list<int> const&& il)
+  : begin(il.begin()) // { dg-warning "initializer_list" }
+  { }
+
+  const int* begin;
+};

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

* Re: [PATCH] c++: Do not warn about lifetime of std::initializer_list<T>& [PR102482]
  2021-10-01 20:58 [PATCH] c++: Do not warn about lifetime of std::initializer_list<T>& [PR102482] Jonathan Wakely
@ 2021-10-06 23:49 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2021-10-06 23:49 UTC (permalink / raw)
  To: Jonathan Wakely, gcc-patches; +Cc: Marek Polacek

On 10/1/21 16:58, Jonathan Wakely wrote:
> An initializer-list constructor taking a non-const lvalue cannot be
> called with a temporary, so the array's lifetime probably doesn't end
> with the full expression. -Winit-list-lifetime should not warn for that
> case.
> 
> 	PR c++/102482
> 
> gcc/cp/ChangeLog:
> 
> 	* init.c (maybe_warn_list_ctor): Do not warn for a reference to
> 	a non-const std::initializer_list.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Winit-list5.C: New test.
> 
> 
> 
> Tested x86_64-linux. OK for trunk?

OK, thanks.

Jason


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

end of thread, other threads:[~2021-10-06 23:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-01 20:58 [PATCH] c++: Do not warn about lifetime of std::initializer_list<T>& [PR102482] Jonathan Wakely
2021-10-06 23:49 ` 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).