diff mbox series

[_GLIBCXX_DEBUG] Fix std::__niter_base behavior

Message ID 4c78d538-da4d-4a54-987d-3c4257913035@gmail.com
State New
Headers show
Series [_GLIBCXX_DEBUG] Fix std::__niter_base behavior | expand

Commit Message

François Dumont Feb. 14, 2024, 6:39 p.m. UTC
libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_base behavior

std::__niter_base is used in _GLIBCXX_DEBUG mode to remove _Safe_iterator<>
wrapper on random access iterators. But doing so it should also preserve 
original
behavior to remove __normal_iterator wrapper.

libstdc++-v3/ChangeLog:

     * include/bits/stl_algobase.h (std::__niter_base): Redefine the 
overload
     definitions for __gnu_debug::_Safe_iterator.
     * include/debug/safe_iterator.tcc (std::__niter_base): Adapt 
declarations.

Ok to commit once all tests completed (still need to check pre-c++11) ?

François

Comments

Jonathan Wakely Feb. 14, 2024, 7:44 p.m. UTC | #1
On Wed, 14 Feb 2024 at 18:39, François Dumont <frs.dumont@gmail.com> wrote:

> libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_base behavior
>
> std::__niter_base is used in _GLIBCXX_DEBUG mode to remove _Safe_iterator<>
> wrapper on random access iterators. But doing so it should also preserve
> original
> behavior to remove __normal_iterator wrapper.
>
> libstdc++-v3/ChangeLog:
>
>      * include/bits/stl_algobase.h (std::__niter_base): Redefine the
> overload
>      definitions for __gnu_debug::_Safe_iterator.
>      * include/debug/safe_iterator.tcc (std::__niter_base): Adapt
> declarations.
>
> Ok to commit once all tests completed (still need to check pre-c++11) ?
>


The declaration in  include/bits/stl_algobase.h has a noexcept-specifier
but the definition in include/debug/safe_iterator.tcc does not have one -
that seems wrong (I'm surprised it even compiles).
Just using std::is_nothrow_copy_constructible<_Ite> seems simpler, that
will be true for __normal_iterator<I, C> if
is_nothrow_copy_constructible<I> is true.

The definition in include/debug/safe_iterator.tcc should use
std::declval<_Ite>() not declval<_Ite>(). Is there any reason why the
definition uses a late-specified-return-type (i.e. auto and ->) when the
declaration doesn't?
François Dumont Feb. 14, 2024, 9:48 p.m. UTC | #2
On 14/02/2024 20:44, Jonathan Wakely wrote:
>
>
> On Wed, 14 Feb 2024 at 18:39, François Dumont <frs.dumont@gmail.com> 
> wrote:
>
>     libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_base behavior
>
>     std::__niter_base is used in _GLIBCXX_DEBUG mode to remove
>     _Safe_iterator<>
>     wrapper on random access iterators. But doing so it should also
>     preserve
>     original
>     behavior to remove __normal_iterator wrapper.
>
>     libstdc++-v3/ChangeLog:
>
>          * include/bits/stl_algobase.h (std::__niter_base): Redefine the
>     overload
>          definitions for __gnu_debug::_Safe_iterator.
>          * include/debug/safe_iterator.tcc (std::__niter_base): Adapt
>     declarations.
>
>     Ok to commit once all tests completed (still need to check
>     pre-c++11) ?
>
>
>
> The declaration in  include/bits/stl_algobase.h has a 
> noexcept-specifier but the definition in 
> include/debug/safe_iterator.tcc does not have one - that seems wrong 
> (I'm surprised it even compiles).

It does ! I thought it was only necessary at declaration, and I also had 
troubles doing it right at definition because of the interaction with 
the auto and ->. Now simplified and consistent in this new proposal.


> Just using std::is_nothrow_copy_constructible<_Ite> seems simpler, 
> that will be true for __normal_iterator<I, C> if 
> is_nothrow_copy_constructible<I> is true.
>
Ok


> The definition in include/debug/safe_iterator.tcc should use 
> std::declval<_Ite>() not declval<_Ite>(). Is there any reason why the 
> definition uses a late-specified-return-type (i.e. auto and ->) when 
> the declaration doesn't?
>
>
I initially plan to use '-> 
std::decltype(std::__niter_base(__it.base()))' but this did not compile, 
ambiguity issue. So I resort to using std::declval and I could have then 
done it the same way as declaration, done now.

Attached is what I'm testing, ok to commit once fully tested ?

François
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index e7207f67266..0f73da13172 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -317,12 +317,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)
     { return __it; }
 
+#if __cplusplus < 201103L
   template<typename _Ite, typename _Seq>
-    _GLIBCXX20_CONSTEXPR
     _Ite
     __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
 		 std::random_access_iterator_tag>&);
 
+ template<typename _Ite, typename _Cont, typename _Seq>
+    _Ite
+    __niter_base(const ::__gnu_debug::_Safe_iterator<
+		 ::__gnu_cxx::__normal_iterator<_Ite, _Cont>, _Seq,
+		 std::random_access_iterator_tag>&);
+#else
+  template<typename _Ite, typename _Seq>
+    _GLIBCXX20_CONSTEXPR
+    decltype(std::__niter_base(std::declval<_Ite>()))
+    __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
+		 std::random_access_iterator_tag>&)
+    noexcept(std::is_nothrow_copy_constructible<_Ite>::value);
+#endif
+
   // Reverse the __niter_base transformation to get a
   // __normal_iterator back again (this assumes that __normal_iterator
   // is only used to wrap random access iterators, like pointers).
diff --git a/libstdc++-v3/include/debug/safe_iterator.tcc b/libstdc++-v3/include/debug/safe_iterator.tcc
index 6eb70cbda04..a8b24233e85 100644
--- a/libstdc++-v3/include/debug/safe_iterator.tcc
+++ b/libstdc++-v3/include/debug/safe_iterator.tcc
@@ -235,13 +235,29 @@ namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
+#if __cplusplus < 201103L
   template<typename _Ite, typename _Seq>
-    _GLIBCXX20_CONSTEXPR
     _Ite
     __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
 		 std::random_access_iterator_tag>& __it)
     { return __it.base(); }
 
+  template<typename _Ite, typename _Cont, typename _DbgSeq>
+    _Ite
+    __niter_base(const ::__gnu_debug::_Safe_iterator<
+		 ::__gnu_cxx::__normal_iterator<_Ite, _Cont>, _DbgSeq,
+		 std::random_access_iterator_tag>& __it)
+    { return __it.base().base(); }
+#else
+  template<typename _Ite, typename _Seq>
+    _GLIBCXX20_CONSTEXPR
+    decltype(std::__niter_base(std::declval<_Ite>()))
+    __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
+		 std::random_access_iterator_tag>& __it)
+    noexcept(std::is_nothrow_copy_constructible<_Ite>::value)
+    { return std::__niter_base(__it.base()); }
+#endif
+
   template<bool _IsMove,
 	   typename _Ite, typename _Seq, typename _Cat, typename _OI>
     _GLIBCXX20_CONSTEXPR
Jonathan Wakely Feb. 15, 2024, 1:17 p.m. UTC | #3
On Wed, 14 Feb 2024 at 21:48, François Dumont <frs.dumont@gmail.com> wrote:

>
> On 14/02/2024 20:44, Jonathan Wakely wrote:
>
>
>
> On Wed, 14 Feb 2024 at 18:39, François Dumont <frs.dumont@gmail.com>
> wrote:
>
>> libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_base behavior
>>
>> std::__niter_base is used in _GLIBCXX_DEBUG mode to remove
>> _Safe_iterator<>
>> wrapper on random access iterators. But doing so it should also preserve
>> original
>> behavior to remove __normal_iterator wrapper.
>>
>> libstdc++-v3/ChangeLog:
>>
>>      * include/bits/stl_algobase.h (std::__niter_base): Redefine the
>> overload
>>      definitions for __gnu_debug::_Safe_iterator.
>>      * include/debug/safe_iterator.tcc (std::__niter_base): Adapt
>> declarations.
>>
>> Ok to commit once all tests completed (still need to check pre-c++11) ?
>>
>
>
> The declaration in  include/bits/stl_algobase.h has a noexcept-specifier
> but the definition in include/debug/safe_iterator.tcc does not have one -
> that seems wrong (I'm surprised it even compiles).
>
> It does !
>

The diagnostic is suppressed without -Wsystem-headers:

/home/jwakely/gcc/14/include/c++/14.0.1/debug/safe_iterator.tcc:255:5: warning:
declaration of 'template<class _Ite, class _Seq> constexpr decltype (std::__
niter_base(declval<_Ite>())) std::__niter_base(const
__gnu_debug::_Safe_iterator<_Iterator, _Sequence,
random_access_iterator_tag>&)' has a different except
ion specifier [-Wsystem-headers]
 255 |     __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
     |     ^~~~~~~~~~~~
/home/jwakely/gcc/14/include/c++/14.0.1/bits/stl_algobase.h:335:5: note: from
previous declaration 'template<class _Ite, class _Seq> constexpr decltype
(std
::__niter_base(declval<_Ite>())) std::__niter_base(const
__gnu_debug::_Safe_iterator<_Iterator, _Sequence,
random_access_iterator_tag>&) noexcept (noexcept
(is_nothrow_copy_constructible<decltype
(std::__niter_base(declval<_Ite>()))>::value))'
 335 |     __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
     |     ^~~~~~~~~~~~


It's a hard error with Clang though:

deb.cc:7:10: error: call to '__niter_base' is ambiguous






> I thought it was only necessary at declaration, and I also had troubles
> doing it right at definition because of the interaction with the auto and
> ->.
>

The trailing-return-type has to come after the noexcept-specifier.



> Now simplified and consistent in this new proposal.
>
>
> Just using std::is_nothrow_copy_constructible<_Ite> seems simpler, that
> will be true for __normal_iterator<I, C> if
> is_nothrow_copy_constructible<I> is true.
>
> Ok
>
>
> The definition in include/debug/safe_iterator.tcc should use
> std::declval<_Ite>() not declval<_Ite>(). Is there any reason why the
> definition uses a late-specified-return-type (i.e. auto and ->) when the
> declaration doesn't?
>
>
> I initially plan to use '-> std::decltype(std::__niter_base(__it.base()))'
> but this did not compile, ambiguity issue. So I resort to using
> std::declval and I could have then done it the same way as declaration,
> done now.
>
> Attached is what I'm testing, ok to commit once fully tested ?
>

OK, thanks.
François Dumont Feb. 15, 2024, 6:38 p.m. UTC | #4
On 15/02/2024 14:17, Jonathan Wakely wrote:
>
>
> On Wed, 14 Feb 2024 at 21:48, François Dumont <frs.dumont@gmail.com> 
> wrote:
>
>
>     On 14/02/2024 20:44, Jonathan Wakely wrote:
>>
>>
>>     On Wed, 14 Feb 2024 at 18:39, François Dumont
>>     <frs.dumont@gmail.com> wrote:
>>
>>         libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_base behavior
>>
>>         std::__niter_base is used in _GLIBCXX_DEBUG mode to remove
>>         _Safe_iterator<>
>>         wrapper on random access iterators. But doing so it should
>>         also preserve
>>         original
>>         behavior to remove __normal_iterator wrapper.
>>
>>         libstdc++-v3/ChangeLog:
>>
>>              * include/bits/stl_algobase.h (std::__niter_base):
>>         Redefine the
>>         overload
>>              definitions for __gnu_debug::_Safe_iterator.
>>              * include/debug/safe_iterator.tcc (std::__niter_base):
>>         Adapt
>>         declarations.
>>
>>         Ok to commit once all tests completed (still need to check
>>         pre-c++11) ?
>>
>>
>>
>>     The declaration in  include/bits/stl_algobase.h has a
>>     noexcept-specifier but the definition in
>>     include/debug/safe_iterator.tcc does not have one - that seems
>>     wrong (I'm surprised it even compiles).
>
>     It does !
>
>
> The diagnostic is suppressed without -Wsystem-headers:
>
> /home/jwakely/gcc/14/include/c++/14.0.1/debug/safe_iterator.tcc:255:5:warning: 
> declaration of 'template<class _Ite, class _Seq> constexpr decltype 
> (std::__
> niter_base(declval<_Ite>())) std::__niter_base(const 
> __gnu_debug::_Safe_iterator<_Iterator, _Sequence, 
> random_access_iterator_tag>&)' has a different except
> ion specifier [-Wsystem-headers]
>  255 | __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
>      | ^~~~~~~~~~~~
> /home/jwakely/gcc/14/include/c++/14.0.1/bits/stl_algobase.h:335:5:note: 
> from previous declaration 'template<class _Ite, class _Seq> constexpr 
> decltype (std
> ::__niter_base(declval<_Ite>())) std::__niter_base(const 
> __gnu_debug::_Safe_iterator<_Iterator, _Sequence, 
> random_access_iterator_tag>&) noexcept (noexcept
> (is_nothrow_copy_constructible<decltype 
> (std::__niter_base(declval<_Ite>()))>::value))'
>  335 | __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
>      | ^~~~~~~~~~~~
>
>
> It's a hard error with Clang though:
>
> deb.cc:7:10: error: call to '__niter_base' is ambiguous
>
>
Yes, I eventually got the error too, I hadn't run enough tests yet.


>
>
>     I thought it was only necessary at declaration, and I also had
>     troubles doing it right at definition because of the interaction
>     with the auto and ->.
>
>
> The trailing-return-type has to come after the noexcept-specifier.
>
>     Now simplified and consistent in this new proposal.
>
>
>>     Just using std::is_nothrow_copy_constructible<_Ite> seems
>>     simpler, that will be true for __normal_iterator<I, C> if
>>     is_nothrow_copy_constructible<I> is true.
>>
>     Ok
>
>
>>     The definition in include/debug/safe_iterator.tcc should use
>>     std::declval<_Ite>() not declval<_Ite>(). Is there any reason why
>>     the definition uses a late-specified-return-type (i.e. auto and
>>     ->) when the declaration doesn't?
>>
>>
>     I initially plan to use '->
>     std::decltype(std::__niter_base(__it.base()))' but this did not
>     compile, ambiguity issue. So I resort to using std::declval and I
>     could have then done it the same way as declaration, done now.
>
>     Attached is what I'm testing, ok to commit once fully tested ?
>
>
> OK, thanks.
>
Thanks for validation but I have a problem to test for c++98.

When I do:

make CXXFLAGS=-std=c++98 check-debug

I see in debug/libstdc++.log for example:

Executing on host: /home/fdumont/dev/gcc/build/./gcc/xg++ -shared-libgcc 
... -mshstk -std=c++98 -g -O2 -DLOCALEDIR="." -nostdinc++ 
-I/home/fdumont/dev/gcc/... 
/home/fdumont/dev/gcc/git/libstdc++-v3/testsuite/25_algorithms/copy/3.cc 
-D_GLIBCXX_DEBUG   -std=gnu++17  -include bits/stdc++.h ...  -lm -o 
./3.exe    (timeout = 360)

The -std=c++98 is there but later comes the -std=gnu++17 so I think it 
runs in C++17, no ?

I also tried the documented alternative:

make check 'RUNTESTFLAGS=--target_board=unix/-O3\"{-std=gnu++98,-std=gnu++11,-std=gnu++14}\"'

but same problem, -std=gnu++17 comes last.

I'll try to rebuild all from scratch but I won't commit soon then.
Jonathan Wakely Feb. 15, 2024, 6:40 p.m. UTC | #5
On Thu, 15 Feb 2024 at 18:38, François Dumont <frs.dumont@gmail.com> wrote:

>
> On 15/02/2024 14:17, Jonathan Wakely wrote:
>
>
>
> On Wed, 14 Feb 2024 at 21:48, François Dumont <frs.dumont@gmail.com>
> wrote:
>
>>
>> On 14/02/2024 20:44, Jonathan Wakely wrote:
>>
>>
>>
>> On Wed, 14 Feb 2024 at 18:39, François Dumont <frs.dumont@gmail.com>
>> wrote:
>>
>>> libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_base behavior
>>>
>>> std::__niter_base is used in _GLIBCXX_DEBUG mode to remove
>>> _Safe_iterator<>
>>> wrapper on random access iterators. But doing so it should also preserve
>>> original
>>> behavior to remove __normal_iterator wrapper.
>>>
>>> libstdc++-v3/ChangeLog:
>>>
>>>      * include/bits/stl_algobase.h (std::__niter_base): Redefine the
>>> overload
>>>      definitions for __gnu_debug::_Safe_iterator.
>>>      * include/debug/safe_iterator.tcc (std::__niter_base): Adapt
>>> declarations.
>>>
>>> Ok to commit once all tests completed (still need to check pre-c++11) ?
>>>
>>
>>
>> The declaration in  include/bits/stl_algobase.h has a noexcept-specifier
>> but the definition in include/debug/safe_iterator.tcc does not have one -
>> that seems wrong (I'm surprised it even compiles).
>>
>> It does !
>>
>
> The diagnostic is suppressed without -Wsystem-headers:
>
> /home/jwakely/gcc/14/include/c++/14.0.1/debug/safe_iterator.tcc:255:5: warning:
> declaration of 'template<class _Ite, class _Seq> constexpr decltype
> (std::__
> niter_base(declval<_Ite>())) std::__niter_base(const
> __gnu_debug::_Safe_iterator<_Iterator, _Sequence,
> random_access_iterator_tag>&)' has a different except
> ion specifier [-Wsystem-headers]
>  255 |     __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
>      |     ^~~~~~~~~~~~
> /home/jwakely/gcc/14/include/c++/14.0.1/bits/stl_algobase.h:335:5: note: from
> previous declaration 'template<class _Ite, class _Seq> constexpr decltype
> (std
> ::__niter_base(declval<_Ite>())) std::__niter_base(const
> __gnu_debug::_Safe_iterator<_Iterator, _Sequence,
> random_access_iterator_tag>&) noexcept (noexcept
> (is_nothrow_copy_constructible<decltype
> (std::__niter_base(declval<_Ite>()))>::value))'
>  335 |     __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
>      |     ^~~~~~~~~~~~
>
>
> It's a hard error with Clang though:
>
> deb.cc:7:10: error: call to '__niter_base' is ambiguous
>
>
> Yes, I eventually got the error too, I hadn't run enough tests yet.
>
>
>
>
>
>
>> I thought it was only necessary at declaration, and I also had troubles
>> doing it right at definition because of the interaction with the auto and
>> ->.
>>
>
> The trailing-return-type has to come after the noexcept-specifier.
>
>
>
>> Now simplified and consistent in this new proposal.
>>
>>
>> Just using std::is_nothrow_copy_constructible<_Ite> seems simpler, that
>> will be true for __normal_iterator<I, C> if
>> is_nothrow_copy_constructible<I> is true.
>>
>> Ok
>>
>>
>> The definition in include/debug/safe_iterator.tcc should use
>> std::declval<_Ite>() not declval<_Ite>(). Is there any reason why the
>> definition uses a late-specified-return-type (i.e. auto and ->) when the
>> declaration doesn't?
>>
>>
>> I initially plan to use '->
>> std::decltype(std::__niter_base(__it.base()))' but this did not compile,
>> ambiguity issue. So I resort to using std::declval and I could have then
>> done it the same way as declaration, done now.
>>
>> Attached is what I'm testing, ok to commit once fully tested ?
>>
>
> OK, thanks.
>
> Thanks for validation but I have a problem to test for c++98.
>
> When I do:
>
> make CXXFLAGS=-std=c++98 check-debug
>

That doesn't work any more, see
https://gcc.gnu.org/onlinedocs/libstdc++/manual/test.html#test.run.permutations



> I see in debug/libstdc++.log for example:
>
> Executing on host: /home/fdumont/dev/gcc/build/./gcc/xg++ -shared-libgcc
> ... -mshstk -std=c++98 -g -O2 -DLOCALEDIR="." -nostdinc++
> -I/home/fdumont/dev/gcc/...
> /home/fdumont/dev/gcc/git/libstdc++-v3/testsuite/25_algorithms/copy/3.cc
> -D_GLIBCXX_DEBUG   -std=gnu++17  -include bits/stdc++.h ...  -lm  -o
> ./3.exe    (timeout = 360)
>
> The -std=c++98 is there but later comes the -std=gnu++17 so I think it
> runs in C++17, no ?
>
> I also tried the documented alternative:
>
> make check 'RUNTESTFLAGS=--target_board=unix/-O3\"{-std=gnu++98,-std=gnu++11,-std=gnu++14}\"'
>
>
> but same problem, -std=gnu++17 comes last.
>
> I'll try to rebuild all from scratch but I won't commit soon then.
>
>
>
François Dumont Feb. 17, 2024, 2:14 p.m. UTC | #6
Thanks for the link, tested and committed.

On 15/02/2024 19:40, Jonathan Wakely wrote:
>
>
> On Thu, 15 Feb 2024 at 18:38, François Dumont <frs.dumont@gmail.com> 
> wrote:
>
>
>     On 15/02/2024 14:17, Jonathan Wakely wrote:
>>
>>
>>     On Wed, 14 Feb 2024 at 21:48, François Dumont
>>     <frs.dumont@gmail.com> wrote:
>>
>>
>>         On 14/02/2024 20:44, Jonathan Wakely wrote:
>>>
>>>
>>>         On Wed, 14 Feb 2024 at 18:39, François Dumont
>>>         <frs.dumont@gmail.com> wrote:
>>>
>>>             libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_base behavior
>>>
>>>             std::__niter_base is used in _GLIBCXX_DEBUG mode to
>>>             remove _Safe_iterator<>
>>>             wrapper on random access iterators. But doing so it
>>>             should also preserve
>>>             original
>>>             behavior to remove __normal_iterator wrapper.
>>>
>>>             libstdc++-v3/ChangeLog:
>>>
>>>                  * include/bits/stl_algobase.h (std::__niter_base):
>>>             Redefine the
>>>             overload
>>>                  definitions for __gnu_debug::_Safe_iterator.
>>>                  * include/debug/safe_iterator.tcc
>>>             (std::__niter_base): Adapt
>>>             declarations.
>>>
>>>             Ok to commit once all tests completed (still need to
>>>             check pre-c++11) ?
>>>
>>>
>>>
>>>         The declaration in include/bits/stl_algobase.h has a
>>>         noexcept-specifier but the definition in
>>>         include/debug/safe_iterator.tcc does not have one - that
>>>         seems wrong (I'm surprised it even compiles).
>>
>>         It does !
>>
>>
>>     The diagnostic is suppressed without -Wsystem-headers:
>>
>>     /home/jwakely/gcc/14/include/c++/14.0.1/debug/safe_iterator.tcc:255:5:warning:
>>     declaration of 'template<class _Ite, class _Seq> constexpr
>>     decltype (std::__
>>     niter_base(declval<_Ite>())) std::__niter_base(const
>>     __gnu_debug::_Safe_iterator<_Iterator, _Sequence,
>>     random_access_iterator_tag>&)' has a different except
>>     ion specifier [-Wsystem-headers]
>>      255 | __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
>>          | ^~~~~~~~~~~~
>>     /home/jwakely/gcc/14/include/c++/14.0.1/bits/stl_algobase.h:335:5:note:
>>     from previous declaration 'template<class _Ite, class _Seq>
>>     constexpr decltype (std
>>     ::__niter_base(declval<_Ite>())) std::__niter_base(const
>>     __gnu_debug::_Safe_iterator<_Iterator, _Sequence,
>>     random_access_iterator_tag>&) noexcept (noexcept
>>     (is_nothrow_copy_constructible<decltype
>>     (std::__niter_base(declval<_Ite>()))>::value))'
>>      335 | __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
>>          | ^~~~~~~~~~~~
>>
>>
>>     It's a hard error with Clang though:
>>
>>     deb.cc:7:10: error: call to '__niter_base' is ambiguous
>>
>>
>     Yes, I eventually got the error too, I hadn't run enough tests yet.
>
>
>>
>>
>>         I thought it was only necessary at declaration, and I also
>>         had troubles doing it right at definition because of the
>>         interaction with the auto and ->.
>>
>>
>>     The trailing-return-type has to come after the noexcept-specifier.
>>
>>         Now simplified and consistent in this new proposal.
>>
>>
>>>         Just using std::is_nothrow_copy_constructible<_Ite> seems
>>>         simpler, that will be true for __normal_iterator<I, C> if
>>>         is_nothrow_copy_constructible<I> is true.
>>>
>>         Ok
>>
>>
>>>         The definition in include/debug/safe_iterator.tcc should use
>>>         std::declval<_Ite>() not declval<_Ite>(). Is there any
>>>         reason why the definition uses a late-specified-return-type
>>>         (i.e. auto and ->) when the declaration doesn't?
>>>
>>>
>>         I initially plan to use '->
>>         std::decltype(std::__niter_base(__it.base()))' but this did
>>         not compile, ambiguity issue. So I resort to using
>>         std::declval and I could have then done it the same way as
>>         declaration, done now.
>>
>>         Attached is what I'm testing, ok to commit once fully tested ?
>>
>>
>>     OK, thanks.
>>
>     Thanks for validation but I have a problem to test for c++98.
>
>     When I do:
>
>     make CXXFLAGS=-std=c++98 check-debug
>
>
> That doesn't work any more, see 
> https://gcc.gnu.org/onlinedocs/libstdc++/manual/test.html#test.run.permutations
>
>     I see in debug/libstdc++.log for example:
>
>     Executing on host: /home/fdumont/dev/gcc/build/./gcc/xg++
>     -shared-libgcc ... -mshstk -std=c++98 -g -O2 -DLOCALEDIR="."
>     -nostdinc++ -I/home/fdumont/dev/gcc/...
>     /home/fdumont/dev/gcc/git/libstdc++-v3/testsuite/25_algorithms/copy/3.cc
>     -D_GLIBCXX_DEBUG   -std=gnu++17  -include bits/stdc++.h ...  -lm 
>     -o ./3.exe    (timeout = 360)
>
>     The -std=c++98 is there but later comes the -std=gnu++17 so I
>     think it runs in C++17, no ?
>
>     I also tried the documented alternative:
>
>     make check 'RUNTESTFLAGS=--target_board=unix/-O3\"{-std=gnu++98,-std=gnu++11,-std=gnu++14}\"'
>
>     but same problem, -std=gnu++17 comes last.
>
>     I'll try to rebuild all from scratch but I won't commit soon then.
>
>
Stephan Bergmann Feb. 19, 2024, 7:07 a.m. UTC | #7
On 2/17/24 15:14, François Dumont wrote:
> Thanks for the link, tested and committed.

I assume this is the cause for the below failure now,

> $ cat test.cc
> #include <algorithm>
> #include <vector>
> void f(std::vector<void const *> &v, void const * p) {
>     std::erase(v, p);
> }

> $ ~/gcc/inst/bin/g++ -std=c++20 -D_GLIBCXX_DEBUG -fsyntax-only test.cc
> In file included from ~/gcc/inst/include/c++/14.0.1/algorithm:60,
>                  from test.cc:1:
> ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h: In instantiation of ‘constexpr _From std::__niter_wrap(_From, _To) [with _From = __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<const void**, __cxx1998::vector<const void*, allocator<const void*> > >, __debug::vector<const void*>, random_access_iterator_tag>; _To = __gnu_cxx::__normal_iterator<const void**, __cxx1998::vector<const void*, allocator<const void*> > >]’:
> ~/gcc/inst/include/c++/14.0.1/vector:144:29:   required from ‘constexpr typename std::__debug::vector<_Tp, _Allocator>::size_type std::erase(__debug::vector<_Tp, _Alloc>&, const _Up&) [with _Tp = const void*; _Alloc = allocator<const void*>; _Up = const void*; typename __debug::vector<_Tp, _Allocator>::size_type = long unsigned int]’
>   144 |           __cont.erase(__niter_wrap(__cont.begin(), __removed),
>       |                        ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
> test.cc:4:15:   required from here
>     4 |     std::erase(v, p);
>       |     ~~~~~~~~~~^~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: error: no match for ‘operator-’ (operand types are ‘__gnu_cxx::__normal_iterator<const void**, std::__cxx1998::vector<const void*, std::allocator<const void*> > >’ and ‘const void**’)
>   347 |     { return __from + (__res - std::__niter_base(__from)); }
>       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In file included from ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:67:
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1148:7: note: candidate: ‘constexpr __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-(difference_type) const [with _Iterator = const void**; _Container = std::__cxx1998::vector<const void*, std::allocator<const void*> >; difference_type = long int]’ (near match)
>  1148 |       operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
>       |       ^~~~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1148:7: note:   conversion of argument 1 would be ill-formed:
> ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:49: error: invalid conversion from ‘const void**’ to ‘__gnu_cxx::__normal_iterator<const void**, std::__cxx1998::vector<const void*, std::allocator<const void*> > >::difference_type’ {aka ‘long int’} [-fpermissive]
>   347 |     { return __from + (__res - std::__niter_base(__from)); }
>       |                                ~~~~~~~~~~~~~~~~~^~~~~~~~
>       |                                                 |
>       |                                                 const void**
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:618:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> constexpr decltype ((__y.base() - __x.base())) std::operator-(const reverse_iterator<_IteratorL>&, const reverse_iterator<_IteratorR>&)’
>   618 |     operator-(const reverse_iterator<_IteratorL>& __x,
>       |     ^~~~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:618:5: note:   template argument deduction/substitution failed:
> ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: note:   ‘__gnu_cxx::__normal_iterator<const void**, std::__cxx1998::vector<const void*, std::allocator<const void*> > >’ is not derived from ‘const std::reverse_iterator<_IteratorL>’
>   347 |     { return __from + (__res - std::__niter_base(__from)); }
>       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1789:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> constexpr decltype ((__x.base() - __y.base())) std::operator-(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)’
>  1789 |     operator-(const move_iterator<_IteratorL>& __x,
>       |     ^~~~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1789:5: note:   template argument deduction/substitution failed:
> ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: note:   ‘__gnu_cxx::__normal_iterator<const void**, std::__cxx1998::vector<const void*, std::allocator<const void*> > >’ is not derived from ‘const std::move_iterator<_IteratorL>’
>   347 |     { return __from + (__res - std::__niter_base(__from)); }
>       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1312:5: note: candidate: ‘template<class _IteratorL, class _IteratorR, class _Container> constexpr decltype ((__lhs.base() - __rhs.base())) __gnu_cxx::operator-(const __normal_iterator<_IteratorL, _Container>&, const __normal_iterator<_IteratorR, _Container>&)’
>  1312 |     operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
>       |     ^~~~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1312:5: note:   template argument deduction/substitution failed:
> ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: note:   mismatched types ‘const __gnu_cxx::__normal_iterator<_IteratorR, _Container>’ and ‘const void**’
>   347 |     { return __from + (__res - std::__niter_base(__from)); }
>       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1325:5: note: candidate: ‘template<class _Iterator, class _Container> constexpr typename __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type __gnu_cxx::operator-(const __normal_iterator<_Iterator, _Container>&, const __normal_iterator<_Iterator, _Container>&)’
>  1325 |     operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
>       |     ^~~~~~~~
> ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1325:5: note:   template argument deduction/substitution failed:
> ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: note:   mismatched types ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’ and ‘const void**’
>   347 |     { return __from + (__res - std::__niter_base(__from)); }
>       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan Wakely Feb. 19, 2024, 8:12 a.m. UTC | #8
On Mon, 19 Feb 2024, 07:08 Stephan Bergmann, <sberg.fun@gmail.com> wrote:

> On 2/17/24 15:14, François Dumont wrote:
> > Thanks for the link, tested and committed.
>
> I assume this is the cause for the below failure now,
>

Yes, the new >= C++11 overload of __niter_base recursively unwraps multiple
layers of wrapping, so that a safe iterator wrapping a normal iterator
wrapping a pointer is unwrapped to just a pointer. But then __niter_wrap
doesn't restore both layers.

I think the change might need to be reverted.




> > $ cat test.cc
> > #include <algorithm>
> > #include <vector>
> > void f(std::vector<void const *> &v, void const * p) {
> >     std::erase(v, p);
> > }
>
> > $ ~/gcc/inst/bin/g++ -std=c++20 -D_GLIBCXX_DEBUG -fsyntax-only test.cc
> > In file included from ~/gcc/inst/include/c++/14.0.1/algorithm:60,
> >                  from test.cc:1:
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h: In instantiation of
> ‘constexpr _From std::__niter_wrap(_From, _To) [with _From =
> __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<const void**,
> __cxx1998::vector<const void*, allocator<const void*> > >,
> __debug::vector<const void*>, random_access_iterator_tag>; _To =
> __gnu_cxx::__normal_iterator<const void**, __cxx1998::vector<const void*,
> allocator<const void*> > >]’:
> > ~/gcc/inst/include/c++/14.0.1/vector:144:29:   required from ‘constexpr
> typename std::__debug::vector<_Tp, _Allocator>::size_type
> std::erase(__debug::vector<_Tp, _Alloc>&, const _Up&) [with _Tp = const
> void*; _Alloc = allocator<const void*>; _Up = const void*; typename
> __debug::vector<_Tp, _Allocator>::size_type = long unsigned int]’
> >   144 |           __cont.erase(__niter_wrap(__cont.begin(), __removed),
> >       |                        ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
> > test.cc:4:15:   required from here
> >     4 |     std::erase(v, p);
> >       |     ~~~~~~~~~~^~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: error: no
> match for ‘operator-’ (operand types are
> ‘__gnu_cxx::__normal_iterator<const void**, std::__cxx1998::vector<const
> void*, std::allocator<const void*> > >’ and ‘const void**’)
> >   347 |     { return __from + (__res - std::__niter_base(__from)); }
> >       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > In file included from
> ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:67:
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1148:7: note:
> candidate: ‘constexpr __gnu_cxx::__normal_iterator<_Iterator, _Container>
> __gnu_cxx::__normal_iterator<_Iterator,
> _Container>::operator-(difference_type) const [with _Iterator = const
> void**; _Container = std::__cxx1998::vector<const void*,
> std::allocator<const void*> >; difference_type = long int]’ (near match)
> >  1148 |       operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
> >       |       ^~~~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1148:7: note:
>  conversion of argument 1 would be ill-formed:
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:49: error: invalid
> conversion from ‘const void**’ to ‘__gnu_cxx::__normal_iterator<const
> void**, std::__cxx1998::vector<const void*, std::allocator<const void*> >
> >::difference_type’ {aka ‘long int’} [-fpermissive]
> >   347 |     { return __from + (__res - std::__niter_base(__from)); }
> >       |                                ~~~~~~~~~~~~~~~~~^~~~~~~~
> >       |                                                 |
> >       |                                                 const void**
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:618:5: note:
> candidate: ‘template<class _IteratorL, class _IteratorR> constexpr decltype
> ((__y.base() - __x.base())) std::operator-(const
> reverse_iterator<_IteratorL>&, const reverse_iterator<_IteratorR>&)’
> >   618 |     operator-(const reverse_iterator<_IteratorL>& __x,
> >       |     ^~~~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:618:5: note:
>  template argument deduction/substitution failed:
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: note:
>  ‘__gnu_cxx::__normal_iterator<const void**, std::__cxx1998::vector<const
> void*, std::allocator<const void*> > >’ is not derived from ‘const
> std::reverse_iterator<_IteratorL>’
> >   347 |     { return __from + (__res - std::__niter_base(__from)); }
> >       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1789:5: note:
> candidate: ‘template<class _IteratorL, class _IteratorR> constexpr decltype
> ((__x.base() - __y.base())) std::operator-(const
> move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)’
> >  1789 |     operator-(const move_iterator<_IteratorL>& __x,
> >       |     ^~~~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1789:5: note:
>  template argument deduction/substitution failed:
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: note:
>  ‘__gnu_cxx::__normal_iterator<const void**, std::__cxx1998::vector<const
> void*, std::allocator<const void*> > >’ is not derived from ‘const
> std::move_iterator<_IteratorL>’
> >   347 |     { return __from + (__res - std::__niter_base(__from)); }
> >       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1312:5: note:
> candidate: ‘template<class _IteratorL, class _IteratorR, class _Container>
> constexpr decltype ((__lhs.base() - __rhs.base()))
> __gnu_cxx::operator-(const __normal_iterator<_IteratorL, _Container>&,
> const __normal_iterator<_IteratorR, _Container>&)’
> >  1312 |     operator-(const __normal_iterator<_IteratorL, _Container>&
> __lhs,
> >       |     ^~~~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1312:5: note:
>  template argument deduction/substitution failed:
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: note:
>  mismatched types ‘const __gnu_cxx::__normal_iterator<_IteratorR,
> _Container>’ and ‘const void**’
> >   347 |     { return __from + (__res - std::__niter_base(__from)); }
> >       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1325:5: note:
> candidate: ‘template<class _Iterator, class _Container> constexpr typename
> __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type
> __gnu_cxx::operator-(const __normal_iterator<_Iterator, _Container>&, const
> __normal_iterator<_Iterator, _Container>&)’
> >  1325 |     operator-(const __normal_iterator<_Iterator, _Container>&
> __lhs,
> >       |     ^~~~~~~~
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_iterator.h:1325:5: note:
>  template argument deduction/substitution failed:
> > ~/gcc/inst/include/c++/14.0.1/bits/stl_algobase.h:347:30: note:
>  mismatched types ‘const __gnu_cxx::__normal_iterator<_Iterator,
> _Container>’ and ‘const void**’
> >   347 |     { return __from + (__res - std::__niter_base(__from)); }
> >       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
Jonathan Wakely Feb. 19, 2024, 8:21 a.m. UTC | #9
On Mon, 19 Feb 2024, 08:12 Jonathan Wakely, <jwakely.gcc@gmail.com> wrote:

>
>
> On Mon, 19 Feb 2024, 07:08 Stephan Bergmann, <sberg.fun@gmail.com> wrote:
>
>> On 2/17/24 15:14, François Dumont wrote:
>> > Thanks for the link, tested and committed.
>>
>> I assume this is the cause for the below failure now,
>>
>
> Yes, the new >= C++11 overload of __niter_base recursively unwraps
> multiple layers of wrapping, so that a safe iterator wrapping a normal
> iterator wrapping a pointer is unwrapped to just a pointer. But then
> __niter_wrap doesn't restore both layers.
>


Actually that's not the problem. __niter_wrap would restore both layers,
except that it uses __niter_base itself:

>   347 |     { return __from + (__res - std::__niter_base(__from)); }
>       |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

And it seems to be getting called with the wrong types. Maybe that's just a
bug in std:: erase or maybe niter_wrap needs adjusting.

I'll check in a couple of hours if François doesn't get to it first.

I have to wonder how this wasn't caught by existing tests though.
François Dumont Feb. 19, 2024, 6:39 p.m. UTC | #10
Turns out that 23_containers/vector/erasure.cc was showing the problem 
in _GLIBCXX_DEBUG mode.

I had only run 25_algorithms tests in _GLIBCXX_DEBUG mode.

This is what I'm testing, I 'll let you know tomorrow morning if all 
successful.

Of course feel free to do or ask for a revert instead.

François


On 19/02/2024 09:21, Jonathan Wakely wrote:
>
>
> On Mon, 19 Feb 2024, 08:12 Jonathan Wakely, <jwakely.gcc@gmail.com> wrote:
>
>
>
>     On Mon, 19 Feb 2024, 07:08 Stephan Bergmann, <sberg.fun@gmail.com>
>     wrote:
>
>         On 2/17/24 15:14, François Dumont wrote:
>         > Thanks for the link, tested and committed.
>
>         I assume this is the cause for the below failure now,
>
>
>     Yes, the new >= C++11 overload of __niter_base recursively unwraps
>     multiple layers of wrapping, so that a safe iterator wrapping a
>     normal iterator wrapping a pointer is unwrapped to just a pointer.
>     But then __niter_wrap doesn't restore both layers.
>
>
>
> Actually that's not the problem. __niter_wrap would restore both 
> layers, except that it uses __niter_base itself:
>
> >   347 |     { return __from + (__res - std::__niter_base(__from)); }
> >       |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> And it seems to be getting called with the wrong types. Maybe that's 
> just a bug in std:: erase or maybe niter_wrap needs adjusting.
>
> I'll check in a couple of hours if François doesn't get to it first.
>
> I have to wonder how this wasn't caught by existing tests though.
>
>
>
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index 0f73da13172..d534e02871f 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -344,7 +344,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     _GLIBCXX20_CONSTEXPR
     inline _From
     __niter_wrap(_From __from, _To __res)
-    { return __from + (__res - std::__niter_base(__from)); }
+    { return __from + (std::__niter_base(__res) - std::__niter_base(__from)); }
 
   // No need to wrap, iterator already has the right type.
   template<typename _Iterator>
François Dumont Feb. 20, 2024, 6:42 p.m. UTC | #11
libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_wrap behavior

     In _GLIBCXX_DEBUG mode the std::__niter_base can remove 2 layers, the
     __gnu_debug::_Safe_iterator<> and the __gnu_cxx::__normal_iterator<>.
     When std::__niter_wrap is called to build a 
__gnu_debug::_Safe_iterator<>
     from a __gnu_cxx::__normal_iterator<> we then have a consistency issue
     as the difference between the 2 iterators will done on a 
__normal_iterator
     on one side and a C pointer on the other. To avoid this problem call
     std::__niter_base on both input iterators.

     libstdc++-v3/ChangeLog:

             * include/bits/stl_algobase.h (std::__niter_wrap): Add a 
call to
             std::__niter_base on res iterator.

Tested under Linux x86_64 normal and _GLIBCXX_DEBUG modes in c++98, 
c++11, c++17.

Ok to commit ?

François


On 19/02/2024 09:21, Jonathan Wakely wrote:
>
>
> On Mon, 19 Feb 2024, 08:12 Jonathan Wakely, <jwakely.gcc@gmail.com> wrote:
>
>
>
>     On Mon, 19 Feb 2024, 07:08 Stephan Bergmann, <sberg.fun@gmail.com>
>     wrote:
>
>         On 2/17/24 15:14, François Dumont wrote:
>         > Thanks for the link, tested and committed.
>
>         I assume this is the cause for the below failure now,
>
>
>     Yes, the new >= C++11 overload of __niter_base recursively unwraps
>     multiple layers of wrapping, so that a safe iterator wrapping a
>     normal iterator wrapping a pointer is unwrapped to just a pointer.
>     But then __niter_wrap doesn't restore both layers.
>
>
>
> Actually that's not the problem. __niter_wrap would restore both 
> layers, except that it uses __niter_base itself:
>
> >   347 |     { return __from + (__res - std::__niter_base(__from)); }
> >       |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> And it seems to be getting called with the wrong types. Maybe that's 
> just a bug in std:: erase or maybe niter_wrap needs adjusting.
>
> I'll check in a couple of hours if François doesn't get to it first.
>
> I have to wonder how this wasn't caught by existing tests though.
>
>
>
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index 0f73da13172..d534e02871f 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -344,7 +344,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     _GLIBCXX20_CONSTEXPR
     inline _From
     __niter_wrap(_From __from, _To __res)
-    { return __from + (__res - std::__niter_base(__from)); }
+    { return __from + (std::__niter_base(__res) - std::__niter_base(__from)); }
 
   // No need to wrap, iterator already has the right type.
   template<typename _Iterator>
Jonathan Wakely Feb. 20, 2024, 7:27 p.m. UTC | #12
On Tue, 20 Feb 2024 at 18:43, François Dumont wrote:
>
>    libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_wrap behavior
>
>     In _GLIBCXX_DEBUG mode the std::__niter_base can remove 2 layers, the
>     __gnu_debug::_Safe_iterator<> and the __gnu_cxx::__normal_iterator<>.
>     When std::__niter_wrap is called to build a __gnu_debug::_Safe_iterator<>
>     from a __gnu_cxx::__normal_iterator<> we then have a consistency issue
>     as the difference between the 2 iterators will done on a __normal_iterator
>     on one side and a C pointer on the other. To avoid this problem call
>     std::__niter_base on both input iterators.
>
>     libstdc++-v3/ChangeLog:
>
>             * include/bits/stl_algobase.h (std::__niter_wrap): Add a call to
>             std::__niter_base on res iterator.
>
> Tested under Linux x86_64 normal and _GLIBCXX_DEBUG modes in c++98, c++11, c++17.
>
> Ok to commit ?
>

OK, thanks.
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index e7207f67266..056fa0c4173 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -317,12 +317,27 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)
     { return __it; }
 
+#if __cplusplus < 201103L
   template<typename _Ite, typename _Seq>
-    _GLIBCXX20_CONSTEXPR
     _Ite
     __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
 		 std::random_access_iterator_tag>&);
 
+ template<typename _Ite, typename _Cont, typename _Seq>
+    _Ite
+    __niter_base(const ::__gnu_debug::_Safe_iterator<
+		 ::__gnu_cxx::__normal_iterator<_Ite, _Cont>, _Seq,
+		 std::random_access_iterator_tag>&);
+#else
+  template<typename _Ite, typename _Seq>
+    _GLIBCXX20_CONSTEXPR
+    decltype(std::__niter_base(std::declval<_Ite>()))
+    __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
+		 std::random_access_iterator_tag>&)
+    noexcept( noexcept(std::is_nothrow_copy_constructible<
+	decltype(std::__niter_base(std::declval<_Ite>()))>::value) );
+#endif
+
   // Reverse the __niter_base transformation to get a
   // __normal_iterator back again (this assumes that __normal_iterator
   // is only used to wrap random access iterators, like pointers).
diff --git a/libstdc++-v3/include/debug/safe_iterator.tcc b/libstdc++-v3/include/debug/safe_iterator.tcc
index 6eb70cbda04..d6cfe24cc83 100644
--- a/libstdc++-v3/include/debug/safe_iterator.tcc
+++ b/libstdc++-v3/include/debug/safe_iterator.tcc
@@ -235,13 +235,29 @@  namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
+#if __cplusplus < 201103L
   template<typename _Ite, typename _Seq>
-    _GLIBCXX20_CONSTEXPR
     _Ite
     __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
 		 std::random_access_iterator_tag>& __it)
     { return __it.base(); }
 
+  template<typename _Ite, typename _Cont, typename _DbgSeq>
+    _Ite
+    __niter_base(const ::__gnu_debug::_Safe_iterator<
+		 ::__gnu_cxx::__normal_iterator<_Ite, _Cont>, _DbgSeq,
+		 std::random_access_iterator_tag>& __it)
+    { return __it.base().base(); }
+#else
+  template<typename _Ite, typename _Seq>
+    _GLIBCXX20_CONSTEXPR
+    auto
+    __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
+		 std::random_access_iterator_tag>& __it)
+    -> decltype(std::__niter_base(declval<_Ite>()))
+    { return std::__niter_base(__it.base()); }
+#endif
+
   template<bool _IsMove,
 	   typename _Ite, typename _Seq, typename _Cat, typename _OI>
     _GLIBCXX20_CONSTEXPR