From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id DD164385C41D; Wed, 24 Aug 2022 14:24:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD164385C41D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661351071; bh=5OAkWMu6O8sIrjSB/SP6NAl6wuD3WGnFA5TZJDgajq0=; h=From:To:Subject:Date:From; b=vOTb5rrMVlTmkaOrkKdlrA0dRK4YrHO5J4eWZeaHZjAvvO6KM4mm10WF8dhLCycoG 1ROhRNmwErqPqUamcVfL+wRABZB9A3RymrCVZdnxfLodMxZ4v7CYF26S5QBi8k5Q8R MzjJN/QJeOatRKFCbJbJpI4aD3ODGv//vC1IwyVU= 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 r13-2176] libstdc++: Fix regression in std::stable_sort X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 0b7c9254998b3fb2c39f6b86b5b196f415530205 X-Git-Newrev: 530f80451a9e76896a0294e0f4bd59baff1ac27f Message-Id: <20220824142431.DD164385C41D@sourceware.org> Date: Wed, 24 Aug 2022 14:24:31 +0000 (GMT) List-Id: https://gcc.gnu.org/g:530f80451a9e76896a0294e0f4bd59baff1ac27f commit r13-2176-g530f80451a9e76896a0294e0f4bd59baff1ac27f Author: Jonathan Wakely Date: Wed Aug 24 13:53:27 2022 +0100 libstdc++: Fix regression in std::stable_sort 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. Diff: --- libstdc++-v3/include/bits/stl_algo.h | 5 ++- .../testsuite/25_algorithms/stable_sort/4.cc | 51 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) 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()); +}