From patchwork Thu Jan 6 15:00:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 49629 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E6F0C385802B for ; Thu, 6 Jan 2022 15:05:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E6F0C385802B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641481526; bh=huRPyjNmFuU4G+KAYGvLK3orajoVywxY8obtMn5St+4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=baxtY30CbJqmdouiFzKlM/1ZCrDSqducN9PMCGUQDouQjv8HwpuL2nYIeYuR7bA/V pU7aQCC97wcSshRqzRzDoF8TN8Y7aOug6IQaoPkh6KiRPQmRzydpYqLru9GfdpTLcC RWye3LIgUffrWpQHxsPdf5+whWCCWTSXZ9HS0DcY= 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 9049B3858029 for ; Thu, 6 Jan 2022 15:00:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9049B3858029 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-624-9Rm6MUAHP1i8JWVvAAo2og-1; Thu, 06 Jan 2022 10:00:52 -0500 X-MC-Unique: 9Rm6MUAHP1i8JWVvAAo2og-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ACA50100CC8C; Thu, 6 Jan 2022 15:00:18 +0000 (UTC) Received: from localhost (unknown [10.33.36.252]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2C06E2B465; Thu, 6 Jan 2022 15:00:18 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add self-merge check to std::forward_list::merge [PR103853] Date: Thu, 6 Jan 2022 15:00:17 +0000 Message-Id: <20220106150017.1932565-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable 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: "Pavel I. Kryukov" Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: "Pavel I. Kryukov" Tested powerpc64le-linux, pushed to trunk. 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. --- libstdc++-v3/include/bits/forward_list.tcc | 5 ++ .../forward_list/operations/merge.cc | 48 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 libstdc++-v3/testsuite/23_containers/forward_list/operations/merge.cc diff --git a/libstdc++-v3/include/bits/forward_list.tcc b/libstdc++-v3/include/bits/forward_list.tcc index 7d90e82de39..b0a65457404 100644 --- a/libstdc++-v3/include/bits/forward_list.tcc +++ b/libstdc++-v3/include/bits/forward_list.tcc @@ -367,6 +367,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(); +}