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.133.124]) by sourceware.org (Postfix) with ESMTPS id 83EB538560A9 for ; Tue, 23 Aug 2022 00:10:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 83EB538560A9 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-386-fZiLUtMJMXOlLtPAo3gNSg-1; Mon, 22 Aug 2022 20:10:30 -0400 X-MC-Unique: fZiLUtMJMXOlLtPAo3gNSg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A164F803903 for ; Tue, 23 Aug 2022 00:10:30 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.170]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8177340466A7; Tue, 23 Aug 2022 00:10:30 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Quash bogus -Wredundant-move warning Date: Mon, 22 Aug 2022 20:10:26 -0400 Message-Id: <20220823001026.109061-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.2 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_NONE, SPF_HELO_NONE, SPF_NONE, 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 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-List-Received-Date: Tue, 23 Aug 2022 00:10:34 -0000 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? 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 -- 2.37.2