From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0E97C3858D37 for ; Tue, 23 Aug 2022 21:53:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0E97C3858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661291600; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2oYXKI5dTAarhHnvmcErBRS1EdXv8XWTUhe6+RC3Cgg=; b=b2XMfQrLjOn9yFRrCoUtHI9SNuipB25VaT77N+xl20bRrjYAOyBTBuKi+tTABpaOKJWJ6E 1vr1V1O/TRPiGf4aJiVqqvMkcE/Ihvuaoyu927C3jl8QJzgTbVG/ZGxgqPhXd+aycDhOi8 hGwFSdtIpWUq3KO99d7cvCS1l3KYAEg= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-547-TtU3enQaPA-RgFn20dBHSQ-1; Tue, 23 Aug 2022 17:53:19 -0400 X-MC-Unique: TtU3enQaPA-RgFn20dBHSQ-1 Received: by mail-qk1-f200.google.com with SMTP id bp11-20020a05620a458b00b006bbeffab91dso8815642qkb.11 for ; Tue, 23 Aug 2022 14:53:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=2oYXKI5dTAarhHnvmcErBRS1EdXv8XWTUhe6+RC3Cgg=; b=cz1kt9Fed6GnZnelhQ/X0YafM8I3bDRPhGGgvx8c2uOJr3DNBcoV6hkIpihHv3t2zY mMAJjAKSzWDH077mM3HVQqZZoyfTOZGTUgn3P8p6J1++M+ueUL5Ade9W/kg4xzOI4FdD znn6ejViYkwfGEiLVwGltjNnDrLKLMQ8iOYEfEMsW394YHdyCREFYw0sIArldzFWB7yN I/gh6wvf72ImY5aDpLZkx0kZ+YiqYH8aPE5sYQM4HRKx8Ot6WYVjS3w4moSMGSC7ty5r dl+gCbEvGkmmKq67RvZ4Bb2g026dDEbW6+u+SdRfhkD/xkRMA1l7JRomcaJg4ScBNbhd kwPA== X-Gm-Message-State: ACgBeo3+avg9J5AX1a+wGoeGt7pYjBiXxuwI/CjAXF7N9bZdIhirpDt3 JIW8YPBfBZBDhwpXKYCVpR/QD9cb2MhBXJq3QQMWhJDkZeX4AKupQneUL44sys/t9fPZDSYgBW7 o1uPKjMNvPMdQOGiIEQ== X-Received: by 2002:a05:6214:27cb:b0:497:e38:fa3d with SMTP id ge11-20020a05621427cb00b004970e38fa3dmr2616490qvb.34.1661291598907; Tue, 23 Aug 2022 14:53:18 -0700 (PDT) X-Google-Smtp-Source: AA6agR6+r5XDSYD/07AsN1K3gQVgMWnztdN94egI5xFXr2Maynu+kSBxC3e76MFsAaSmBGs13QRmMQ== X-Received: by 2002:a05:6214:27cb:b0:497:e38:fa3d with SMTP id ge11-20020a05621427cb00b004970e38fa3dmr2616479qvb.34.1661291598628; Tue, 23 Aug 2022 14:53:18 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id o10-20020ac87c4a000000b0034305a91aaesm12624875qtv.83.2022.08.23.14.53.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Aug 2022 14:53:18 -0700 (PDT) Message-ID: <9dee175c-dcae-80f7-2976-949740c7c616@redhat.com> Date: Tue, 23 Aug 2022 17:53:16 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH] c++: Quash bogus -Wredundant-move warning To: Marek Polacek , GCC Patches References: <20220823001026.109061-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20220823001026.109061-1-polacek@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,TRACKER_ID,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 8/22/22 17:10, Marek Polacek wrote: > This patch fixes a pretty stoopid thinko. When I added code to warn > about pessimizing std::move in initializations like > > T t{std::move(T())}; > > I also added code to unwrap the expression from { }. But when we have > > return {std::move(t)}; > > we cannot warn about a redundant std::move because the implicit move > wouldn't happen for "return {t};" because the expression isn't just > a name. However, we still want to warn about > > return {std::move(T())}; > > so let's not disable the -Wpessimizing-move warning. Tests added for > both cases. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > gcc/cp/ChangeLog: > > * typeck.cc (maybe_warn_pessimizing_move): Don't warn about > redundant std::move when the expression was wrapped in { }. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/Wpessimizing-move10.C: New test. > * g++.dg/cpp0x/Wredundant-move12.C: New test. > --- > gcc/cp/typeck.cc | 13 +++++-- > .../g++.dg/cpp0x/Wpessimizing-move10.C | 30 ++++++++++++++++ > .../g++.dg/cpp0x/Wredundant-move12.C | 36 +++++++++++++++++++ > 3 files changed, 77 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move10.C > create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wredundant-move12.C > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index 992ebfd99fb..7fde65adaa4 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -10397,11 +10397,15 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) > if (!CLASS_TYPE_P (type)) > return; > > + bool wrapped_p = false; > /* A a = std::move (A()); */ > if (TREE_CODE (expr) == TREE_LIST) > { > if (list_length (expr) == 1) > - expr = TREE_VALUE (expr); > + { > + expr = TREE_VALUE (expr); > + wrapped_p = true; > + } > else > return; > } > @@ -10410,7 +10414,10 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) > else if (TREE_CODE (expr) == CONSTRUCTOR) > { > if (CONSTRUCTOR_NELTS (expr) == 1) > - expr = CONSTRUCTOR_ELT (expr, 0)->value; > + { > + expr = CONSTRUCTOR_ELT (expr, 0)->value; > + wrapped_p = true; > + } > else > return; > } > @@ -10458,6 +10465,8 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) > /* Warn if the move is redundant. It is redundant when we would > do maybe-rvalue overload resolution even without std::move. */ > else if (warn_redundant_move > + /* This doesn't apply for return {std::move (t)};. */ > + && !wrapped_p > && !warning_suppressed_p (expr, OPT_Wredundant_move) > && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true))) > { > diff --git a/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move10.C b/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move10.C > new file mode 100644 > index 00000000000..77314141da3 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move10.C > @@ -0,0 +1,30 @@ > +// { dg-do compile { target c++11 } } > +// { dg-options "-Wpessimizing-move" } > + > +// Define std::move. > +namespace std { > + template > + struct remove_reference > + { typedef _Tp type; }; > + > + template > + struct remove_reference<_Tp&> > + { typedef _Tp type; }; > + > + template > + struct remove_reference<_Tp&&> > + { typedef _Tp type; }; > + > + template > + constexpr typename std::remove_reference<_Tp>::type&& > + move(_Tp&& __t) noexcept > + { return static_cast::type&&>(__t); } > +} > + > +struct S { }; > + > +S > +f () > +{ > + return {std::move(S())}; // { dg-warning "moving a temporary object prevents copy elision" } > +} > diff --git a/gcc/testsuite/g++.dg/cpp0x/Wredundant-move12.C b/gcc/testsuite/g++.dg/cpp0x/Wredundant-move12.C > new file mode 100644 > index 00000000000..192d981ba5e > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/Wredundant-move12.C > @@ -0,0 +1,36 @@ > +// { dg-do compile { target c++11 } } > +// { dg-options "-Wredundant-move" } > + > +// Define std::move. > +namespace std { > + template > + struct remove_reference > + { typedef _Tp type; }; > + > + template > + struct remove_reference<_Tp&> > + { typedef _Tp type; }; > + > + template > + struct remove_reference<_Tp&&> > + { typedef _Tp type; }; > + > + template > + constexpr typename std::remove_reference<_Tp>::type&& > + move(_Tp&& __t) noexcept > + { return static_cast::type&&>(__t); } > +} > + > +struct S1 { > + S1(); > + S1(S1 const &) = delete; > + S1(S1 &&); > + S1 operator =(S1 const &) = delete; > + S1 operator =(S1 &&); > +}; > + > +struct S2 { S2(S1); }; > +S2 f() { > + S1 s; > + return { std::move(s) }; // { dg-bogus "redundant move" } > +} > > base-commit: cc4fa7a210b638d6a46f14dab17f2361389d18e1