From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed 1/3] libstdc++: Fix minor bugs in std::common_iterator
Date: Thu, 21 Jul 2022 00:00:09 +0100 [thread overview]
Message-ID: <20220720230011.2732391-1-jwakely@redhat.com> (raw)
Tested powerpc64le-linux, pushed to trunk.
-- >8 --
The noexcept-specifier for some std::common_iterator constructors was
incorrectly using an rvalue as the first argument of
std::is_nothrow_assignable_v. This gave the wrong answer for some types,
e.g. std::common_iterator<int*, S>, because an rvalue of scalar type
cannot be assigned to.
Also fix the friend declaration to use the same constraints as on the
definition of the class template. G++ fails to diagnose this error, due
to PR c++/96830.
Finally, the copy constructor was using std::move for its argument
in some cases, which should be removed.
libstdc++-v3/ChangeLog:
* include/bits/stl_iterator.h (common_iterator): Fix incorrect
uses of is_nothrow_assignable_v. Fix inconsistent constraints on
friend declaration. Do not move argument in copy constructor.
* testsuite/24_iterators/common_iterator/1.cc: Check for
noexcept constructibnle/assignable.
---
libstdc++-v3/include/bits/stl_iterator.h | 11 +++++----
.../24_iterators/common_iterator/1.cc | 23 ++++++++++++++++++-
2 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h
index 049cb02a4c4..fd0ae3aa771 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -1838,7 +1838,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_S_noexcept1()
{
if constexpr (is_trivially_default_constructible_v<_Tp>)
- return is_nothrow_assignable_v<_Tp, _Up>;
+ return is_nothrow_assignable_v<_Tp&, _Up>;
else
return is_nothrow_constructible_v<_Tp, _Up>;
}
@@ -1932,14 +1932,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
if (_M_index == 0)
{
if constexpr (is_trivially_default_constructible_v<_It>)
- _M_it = std::move(__x._M_it);
+ _M_it = __x._M_it;
else
std::construct_at(std::__addressof(_M_it), __x._M_it);
}
else if (_M_index == 1)
{
if constexpr (is_trivially_default_constructible_v<_Sent>)
- _M_sent = std::move(__x._M_sent);
+ _M_sent = __x._M_sent;
else
std::construct_at(std::__addressof(_M_sent), __x._M_sent);
}
@@ -1964,8 +1964,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
operator=(const common_iterator<_It2, _Sent2>& __x)
noexcept(is_nothrow_constructible_v<_It, const _It2&>
&& is_nothrow_constructible_v<_Sent, const _Sent2&>
- && is_nothrow_assignable_v<_It, const _It2&>
- && is_nothrow_assignable_v<_Sent, const _Sent2&>)
+ && is_nothrow_assignable_v<_It&, const _It2&>
+ && is_nothrow_assignable_v<_Sent&, const _Sent2&>)
{
switch(_M_index << 2 | __x._M_index)
{
@@ -2164,6 +2164,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
private:
template<input_or_output_iterator _It2, sentinel_for<_It2> _Sent2>
+ requires (!same_as<_It2, _Sent2>) && copyable<_It2>
friend class common_iterator;
constexpr bool _M_has_value() const noexcept { return _M_index < 2; }
diff --git a/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc b/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc
index 365ee89c02e..ec4a86c862a 100644
--- a/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc
+++ b/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc
@@ -27,15 +27,30 @@ test01()
using I = std::common_iterator<int*, const int*>;
static_assert( std::is_default_constructible_v<I> );
static_assert( std::is_copy_constructible_v<I> );
+ static_assert( std::is_move_constructible_v<I> );
static_assert( std::is_copy_assignable_v<I> );
+ static_assert( std::is_move_assignable_v<I> );
static_assert( std::is_constructible_v<I, int*> );
static_assert( std::is_constructible_v<I, const int*> );
- struct sentinel { operator int*() const { return nullptr; } };
+ static_assert( std::is_nothrow_copy_constructible_v<I> ); // GCC extension
+ static_assert( std::is_nothrow_move_constructible_v<I> ); // GCC extension
+ static_assert( std::is_nothrow_copy_assignable_v<I> ); // GCC extension
+ static_assert( std::is_nothrow_move_assignable_v<I> ); // GCC extension
+
+ struct sentinel { operator int*() const noexcept { return nullptr; } };
using K = std::common_iterator<int*, sentinel>;
static_assert( std::is_constructible_v<I, const K&> );
static_assert( std::is_assignable_v<I, const K&> );
+ static_assert( std::is_nothrow_assignable_v<I&, const K&> ); // GCC extension
+
+ struct sentinel_throwing { operator int*() const { return nullptr; } };
+ using K_throwing = std::common_iterator<int*, sentinel_throwing>;
+ // Conversion is noexcept(false)
+ static_assert( ! std::is_nothrow_assignable_v<I&, const K_throwing&> );
+
+
struct sentinel2
{
const int* p;
@@ -46,6 +61,12 @@ test01()
using J = std::common_iterator<const int*, sentinel2>;
static_assert( std::is_constructible_v<J, const I&> );
static_assert( std::is_convertible_v<const I&, J> );
+
+ static_assert( std::is_constructible_v<J, I> );
+ static_assert( std::is_convertible_v<I, J> );
+
+ // Constructor is noexcept(false)
+ static_assert( ! std::is_nothrow_constructible_v<J, I> );
}
void
--
2.34.3
next reply other threads:[~2022-07-20 23:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-20 23:00 Jonathan Wakely [this message]
2022-07-20 23:00 ` [committed 2/3] libstdc++: Fix std::common_iterator assignment [PR100823] Jonathan Wakely
2022-07-20 23:00 ` [committed 3/3] libstdc++: Fix std::common_iterator triviality [PR100823] Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220720230011.2732391-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).