From patchwork Fri Oct 1 20:58:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1535538 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=YE+UZPA8; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HLjCS1h5hz9t0Y for ; Sat, 2 Oct 2021 06:59:16 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8D4713857416 for ; Fri, 1 Oct 2021 20:59:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D4713857416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633121953; bh=JOneXRSj/bk/u1n0yr9pYhMDdBXKzjp2g1j6Ex08+ns=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=YE+UZPA8GL43PlWdIhSvwj1uE9HGsiaXJt2hSNlHzVNm7namBlwL5MfiF2fEWcfDo 7tvM0Nja6TtoJmxuYoA0bT5QACGQ1v/MEAQisGvbL9Dd5P6axTONY+6Knx2ppmZmEM Hw/ak+Gt/PkisCDee2Q73KBASzIqq+zocrnDkAR0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 768803858410 for ; Fri, 1 Oct 2021 20:58:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 768803858410 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-539-zw8CJ2e3Ms-d_jPAKfbwiA-1; Fri, 01 Oct 2021 16:58:29 -0400 X-MC-Unique: zw8CJ2e3Ms-d_jPAKfbwiA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E038B1006AA2 for ; Fri, 1 Oct 2021 20:58:28 +0000 (UTC) Received: from localhost (unknown [10.33.36.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9453B18B5E; Fri, 1 Oct 2021 20:58:21 +0000 (UTC) Date: Fri, 1 Oct 2021 21:58:20 +0100 To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Do not warn about lifetime of std::initializer_list& [PR102482] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Cc: Marek Polacek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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? commit 474f2ae9c9f8312d9c732094bfb1c8da7342d6a9 Author: Jonathan Wakely Date: Wed Sep 29 21:19:49 2021 c++: Do not warn about lifetime of std::initializer_list& [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 + +struct X { }; + +struct span +{ + span(std::initializer_list& il) + : begin(il.begin()) // { dg-bogus "initializer_list" } + { } + + const int* begin; +}; + +struct span_warn +{ + span_warn(std::initializer_list il) + : begin(il.begin()) // { dg-warning "initializer_list" } + { } + + const int* begin; +}; + +struct span_warn2 +{ + span_warn2(std::initializer_list&& il) + : begin(il.begin()) // { dg-warning "initializer_list" } + { } + + const int* begin; +}; + +struct span_warn3 +{ + span_warn3(std::initializer_list const& il) + : begin(il.begin()) // { dg-warning "initializer_list" } + { } + + const int* begin; +}; + +struct span_warn4 +{ + span_warn4(std::initializer_list const il) + : begin(il.begin()) // { dg-warning "initializer_list" } + { } + + const int* begin; +}; + +struct span_warn5 +{ + span_warn5(std::initializer_list const&& il) + : begin(il.begin()) // { dg-warning "initializer_list" } + { } + + const int* begin; +};