From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 2CFAC3857415; Mon, 9 May 2022 16:40:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2CFAC3857415 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r9-10056] libstdc++: Add self-merge check to std::forward_list::merge [PR103853] X-Act-Checkin: gcc X-Git-Author: Pavel I. Kryukov X-Git-Refname: refs/heads/releases/gcc-9 X-Git-Oldrev: 756d1581d11c583d9cbfc2e0b7ce1a095845085e X-Git-Newrev: 79b7b824346d3cb3101d1896f81ed16392b7471c Message-Id: <20220509164000.2CFAC3857415@sourceware.org> Date: Mon, 9 May 2022 16:40:00 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 May 2022 16:40:00 -0000 https://gcc.gnu.org/g:79b7b824346d3cb3101d1896f81ed16392b7471c commit r9-10056-g79b7b824346d3cb3101d1896f81ed16392b7471c Author: Pavel I. Kryukov Date: Thu Jan 6 12:32:36 2022 +0000 libstdc++: Add self-merge check to std::forward_list::merge [PR103853] This implements the proposed resolution of LWG 3088, so that x.merge(x) is a no-op, consistent with std::list::merge. Signed-off-by: Pavel I. Kryukov Co-authored-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/103853 * include/bits/forward_list.tcc (forward_list::merge): Check for self-merge. * testsuite/23_containers/forward_list/operations/merge.cc: New test. (cherry picked from commit 52ebc2be0990d6d3a46bb716164f9cef6f661021) Diff: --- libstdc++-v3/include/bits/forward_list.tcc | 5 +++ .../23_containers/forward_list/operations/merge.cc | 48 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/libstdc++-v3/include/bits/forward_list.tcc b/libstdc++-v3/include/bits/forward_list.tcc index 088111e3330..b74c56a173e 100644 --- a/libstdc++-v3/include/bits/forward_list.tcc +++ b/libstdc++-v3/include/bits/forward_list.tcc @@ -372,6 +372,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER forward_list<_Tp, _Alloc>:: merge(forward_list&& __list, _Comp __comp) { + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3088. forward_list::merge behavior unclear when passed *this + if (std::__addressof(__list) == this) + return; + _Node_base* __node = &this->_M_impl._M_head; while (__node->_M_next && __list._M_impl._M_head._M_next) { diff --git a/libstdc++-v3/testsuite/23_containers/forward_list/operations/merge.cc b/libstdc++-v3/testsuite/23_containers/forward_list/operations/merge.cc new file mode 100644 index 00000000000..0f6f520c33b --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/forward_list/operations/merge.cc @@ -0,0 +1,48 @@ +// { dg-do run { target c++11 } } +// C++11 23.3.4.6 Operations [forwardlist.ops] + +#include +#include + +void +test_stable() +{ + std::forward_list a{1.5, 2.0, 3.5, 4.1}; + std::forward_list b{1.0, 2.5, 3.0, 4.3, 4.2, 5.0}; + + a.merge(b, std::less{}); + + // result is sorted with respect to std::less, so 1.0 and 1.5 are + // equivalent, and stability guarantee means the element from a comes first. + const std::forward_list r { 1.5, 1.0, + 2.0, 2.5, + 3.5, 3.0, + 4.1, 4.3, 4.2, + 5.0}; + + VERIFY(a == r); +} + +void +test_lwg3088() +{ + // LWG 3088: forward_list::merge behavior unclear when passed *this + // PR libstdc++/103853 + std::forward_list c1{ 1, 2, 3 }; + const std::forward_list c2 = c1; + c1.merge(c1); + VERIFY( c1 == c2 ); + c1.merge(c1, std::less{}); + VERIFY( c1 == c2 ); + c1.merge(std::move(c1)); + VERIFY( c1 == c2 ); + c1.merge(std::move(c1), std::less{}); + VERIFY( c1 == c2 ); +} + +int +main() +{ + test_stable(); + test_lwg3088(); +}