From patchwork Wed Aug 24 14:33:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 737 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp1452733wro; Wed, 24 Aug 2022 07:34:36 -0700 (PDT) X-Google-Smtp-Source: AA6agR6iHIHsEG9k6E0dkTEJuWwsasD290jEeWdM5ywxv16IH08JeZEdP+HB4WVUizMdQk56Rs8/ X-Received: by 2002:a05:6402:2996:b0:447:7be7:2d67 with SMTP id eq22-20020a056402299600b004477be72d67mr2946865edb.15.1661351676078; Wed, 24 Aug 2022 07:34:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661351676; cv=none; d=google.com; s=arc-20160816; b=QQmLoHAb49FHM4zDh6HJlBpzAEXzmaW8vOPFt6XtZab/VMJa5FfgYAkjH4jsdjyInD ZXMKxg95A0QTsovRr74oLQ7pCJQBGSGjoUQxp1QNtSS7jlSO1StgQSetGNG4CFvsMjj1 ou8u6STSWm+xUy11vIIuFI6BBenOcOFklj8PF/lq2jt5Q2YX4HvX8vKOaMIZu8L0429v 6/WWY+MpJIZuVFDSYP87b8ISDcQpIoq5MnM2sFNWfug+ae69DAVH8k6/SwDYLxWvB4Dy 4eg1ai1+nEt68BzjRqgrYfwE5zHCMZB4wDOABZsC0pSBm1CeoXb5I1HMYyKt/A7M6oKa ftxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=SjiX0kAEDPfpHM3ud0P8VKVdPjElnWAdVozaMBUsKkU=; b=IPrZTLpCslHVQLgUEupv1QGkoRG7ArTceNagQ61DJvCCuSZ69m2hybmKX/PaNEakxP R91dtlX8a66Uy7CLV63/a3lKQwGm3Wxkd7JLtI593oGCBONU7sNvXH7ZKQKSa/dRC3ST ux8NIt4w6FpmEYKdTzHVn0YFocLehaSynrfkiDUqm+Wpe7atrcec4S1sBakfLtKa7zK0 ufGC6yYj9RxBNezUF4zKonfWcF0GmsspWKWde+vltAXOj5H7G34JD2fuMQJEEMfmp5Xs gzD7DDOuFul68JhzqYCxdhExlfsltUNX47T79RfCsA+IC9DFWs/oAtF+pEAaWaWf1tRm Lqkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=vj5JhsDC; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ji12-20020a170907980c00b0073d8eb7f2f0si1624560ejc.440.2022.08.24.07.34.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 07:34:36 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=vj5JhsDC; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 446BB385EC52 for ; Wed, 24 Aug 2022 14:34:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 446BB385EC52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661351667; bh=SjiX0kAEDPfpHM3ud0P8VKVdPjElnWAdVozaMBUsKkU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=vj5JhsDCAdtT8DKOY/cHT2f8BMIT0aJcI4Tu2XhmjhRGWwzIYCI1I/Sm7u0ua3t6/ 3SzbNG5QKRNPR2nGKyFzG5UcKgWus1sY7I/dKScNVPBcKxEXnMuUuQOAaipynG9+Bn Ipo9my822utnYE0XxC51m+A/V5VFe/B9QFQ3fMEk= 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 52F99385C410 for ; Wed, 24 Aug 2022 14:33:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 52F99385C410 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-156-jRS76X39P3Sc94rPT4mRnw-1; Wed, 24 Aug 2022 10:33:41 -0400 X-MC-Unique: jRS76X39P3Sc94rPT4mRnw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CF57585A589; Wed, 24 Aug 2022 14:33:40 +0000 (UTC) Received: from localhost (unknown [10.33.36.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 94E9B18ECC; Wed, 24 Aug 2022 14:33:40 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix regression in std::stable_sort Date: Wed, 24 Aug 2022 15:33:39 +0100 Message-Id: <20220824143339.121092-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 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=unavailable 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-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742053495284205785?= X-GMAIL-MSGID: =?utf-8?q?1742053495284205785?= Tested powerpc64le-linux, pushed to trunk. No backport needed as the regression is only on trunk. -- >8 -- The recent change to split out the cold path of std::stable_sort caused a regression for some Qt code. The problem is that the library now adds a value of type ptrdiff_t to the iterator, which is ambiguous with -pedantic. The addition could either convert the iterator to a built-in pointer and add the ptrdiff_t to that, or it could convert the ptrdiff_t to the iterator's difference_type and use the iterator's own operator+. The fix is to cast the ptrdiff_t value to the difference type first. libstdc++-v3/ChangeLog: * include/bits/stl_algo.h (__stable_sort): Cast size to iterator's difference type. * testsuite/25_algorithms/stable_sort/4.cc: New test. --- libstdc++-v3/include/bits/stl_algo.h | 5 +- .../testsuite/25_algorithms/stable_sort/4.cc | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/stable_sort/4.cc diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index c6078054514..57fa1c1dc55 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -5026,8 +5026,9 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO _TmpBuf __buf(__first, (__last - __first + 1) / 2); if (__builtin_expect(__buf.requested_size() == __buf.size(), true)) - std::__stable_sort_adaptive(__first, __first + __buf.size(), __last, - __buf.begin(), __comp); + std::__stable_sort_adaptive(__first, + __first + _DistanceType(__buf.size()), + __last, __buf.begin(), __comp); else if (__builtin_expect(__buf.begin() == 0, false)) std::__inplace_stable_sort(__first, __last, __comp); else diff --git a/libstdc++-v3/testsuite/25_algorithms/stable_sort/4.cc b/libstdc++-v3/testsuite/25_algorithms/stable_sort/4.cc new file mode 100644 index 00000000000..b7bda4eeaca --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/stable_sort/4.cc @@ -0,0 +1,51 @@ +// { dg-options "-pedantic" } +// { dg-do compile } + +#include + +/* This type is reduced from QTypedArrayData::iterator which has an implicit + * conversion to its pointer type and a difference type of int. + * The expression Iter() + ptrdiff_t(0) is ambiguous with -pedantic because it + * could either convert the RHS to int and use Iter::operator+(int) + * or it could convert the LHS to pointer and use built-in pointer arithmetic. + */ +struct Iter +{ + struct value_type { bool operator<(value_type) const; }; + typedef value_type* pointer; + typedef value_type& reference; + typedef std::random_access_iterator_tag iterator_category; + typedef int difference_type; + + reference operator*() const; + pointer operator->() const; + + reference operator[](difference_type) const; + + Iter& operator++(); + Iter& operator--(); + Iter operator++(int); + Iter operator--(int); + + Iter& operator+=(difference_type); + Iter& operator-=(difference_type); + + Iter operator+(difference_type) const; + Iter operator-(difference_type) const; + + difference_type operator-(Iter) const; + + operator pointer() const; // XXX this causes the ambiguity + + bool operator==(Iter) const; + bool operator!=(Iter) const; + + bool operator<(Iter) const; +}; + +Iter operator+(Iter::difference_type, Iter); + +int main() +{ + std::stable_sort(Iter(), Iter()); +}