From patchwork Wed Nov 3 16:51:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 47019 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 2C6173858420 for ; Wed, 3 Nov 2021 16:52:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C6173858420 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635958364; bh=UySt6dpO3QOqjp7riS7KFWOef1deaOwXtTguT1kzczg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=n68Pm2cZRKtNDy3Qhbm58V1bse6v0Q97RGAlyc/evQDF5RCC5Geds+JPrXlwyR1sV +uyjk/9gwMla8rgflrxrgk46aXBmvljI/kPOkzA4I6ZSs1bbi7Kwy7BT3n2r+b4HKw h0fmezUw7Pr+jZXwboEJmC+ruGvwd3QfCK3KzCZg= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id C2F85385840A for ; Wed, 3 Nov 2021 16:51:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C2F85385840A Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-573-t8FLxpEJPEuVHyQzHsYx8A-1; Wed, 03 Nov 2021 12:51:51 -0400 X-MC-Unique: t8FLxpEJPEuVHyQzHsYx8A-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 EC90A800053; Wed, 3 Nov 2021 16:51:49 +0000 (UTC) Received: from localhost (unknown [10.33.36.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id 866BA19D9F; Wed, 3 Nov 2021 16:51:49 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix regression in std::list::sort [PR66742] Date: Wed, 3 Nov 2021 16:51:48 +0000 Message-Id: <20211103165148.1788896-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=-14.1 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_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham 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 Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The standard does not require const-correct comparisons in list::sort. Tested powerpc64le-linux, committed to trunk. libstdc++-v3/ChangeLog: PR libstdc++/66742 * include/bits/list.tcc (list::sort): Use mutable iterators for comparisons. * include/bits/stl_list.h (_Scratch_list::_Ptr_cmp): Likewise. * testsuite/23_containers/list/operations/66742.cc: Check non-const comparisons. --- libstdc++-v3/include/bits/list.tcc | 4 ++-- libstdc++-v3/include/bits/stl_list.h | 8 +++---- .../23_containers/list/operations/66742.cc | 23 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/list.tcc b/libstdc++-v3/include/bits/list.tcc index 7f4e1569ab1..cc750c98a2d 100644 --- a/libstdc++-v3/include/bits/list.tcc +++ b/libstdc++-v3/include/bits/list.tcc @@ -499,7 +499,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Scratch_list* __fill = __tmp; _Scratch_list* __counter; - _Scratch_list::_Ptr_cmp __ptr_comp; + _Scratch_list::_Ptr_cmp __ptr_comp; __try { @@ -623,7 +623,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Scratch_list* __fill = __tmp; _Scratch_list* __counter; - _Scratch_list::_Ptr_cmp __ptr_comp + _Scratch_list::_Ptr_cmp __ptr_comp = { __comp }; __try diff --git a/libstdc++-v3/include/bits/stl_list.h b/libstdc++-v3/include/bits/stl_list.h index 96d2a2f0f69..ffaaa6dfd97 100644 --- a/libstdc++-v3/include/bits/stl_list.h +++ b/libstdc++-v3/include/bits/stl_list.h @@ -174,8 +174,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Cmp _M_cmp; bool - operator()(const __detail::_List_node_base* __lhs, - const __detail::_List_node_base* __rhs) /* not const */ + operator()(__detail::_List_node_base* __lhs, + __detail::_List_node_base* __rhs) /* not const */ { return _M_cmp(*_Iter(__lhs), *_Iter(__rhs)); } }; @@ -183,8 +183,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct _Ptr_cmp<_Iter, void> { bool - operator()(const __detail::_List_node_base* __lhs, - const __detail::_List_node_base* __rhs) const + operator()(__detail::_List_node_base* __lhs, + __detail::_List_node_base* __rhs) const { return *_Iter(__lhs) < *_Iter(__rhs); } }; diff --git a/libstdc++-v3/testsuite/23_containers/list/operations/66742.cc b/libstdc++-v3/testsuite/23_containers/list/operations/66742.cc index 24bda3920d8..94a37a31de7 100644 --- a/libstdc++-v3/testsuite/23_containers/list/operations/66742.cc +++ b/libstdc++-v3/testsuite/23_containers/list/operations/66742.cc @@ -48,8 +48,31 @@ test01() VERIFY( is_sorted(l, std::greater()) ); } +void +test02() +{ + // The standard doesn't require comparisons to be const-correct. + // The initial fix for PR 66742 caused a regression here. + + struct X + { + bool operator<(X&) /* non-const */ { return false; } + }; + + struct Cmp + { + bool operator()(X&, X&) /* non-const */ { return false; } + }; + + std::list l; + l.sort(); + Cmp c; + l.sort(c); +} + int main() { test01(); + test02(); }