From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id C56A53858D32; Tue, 23 Aug 2022 22:41:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C56A53858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661294495; bh=lEHmDYfgOa+DFnWAWP/XoMHfdAfONjfo8cMqlgQ/gwA=; h=From:To:Subject:Date:From; b=ODUAFKcfFck44TwIGB+UnogrzOnfJFgFvBxiOTs/1heuzdcXRwlOa0buHmm+GxlbY V4K6OGUb32bcv3OZiSFB+Gxq0SX58/7YYrj7LW04nSWHUMntsANl26suMcRjyHn3Fu LMdCTZJ7QKgxBd/ux0WzojlrSkXx4fs0MPO17K0E= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2162] c++: Quash bogus -Wredundant-move warning X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 4ade41de1a6c80db6bb61399da6bff2126813d75 X-Git-Newrev: 8567d9491d06832ea34b564c6bace9f7d43099ae Message-Id: <20220823224135.C56A53858D32@sourceware.org> Date: Tue, 23 Aug 2022 22:41:35 +0000 (GMT) List-Id: https://gcc.gnu.org/g:8567d9491d06832ea34b564c6bace9f7d43099ae commit r13-2162-g8567d9491d06832ea34b564c6bace9f7d43099ae Author: Marek Polacek Date: Mon Aug 22 19:13:16 2022 -0400 c++: Quash bogus -Wredundant-move warning 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. 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. Diff: --- gcc/cp/typeck.cc | 13 +++++++-- gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move10.C | 30 ++++++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/Wredundant-move12.C | 36 ++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) 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" } +}