Skip to content

Commit c9dbf0f

Browse files
committed
[libc++] Fix __wrap_iter copy-assignment in constexpr contexts
Fixes llvm/llvm-project#52902 In debug mode during constant evaluation the iterator was never assigend. There seem to be no other instances of this bug. Reviewed By: Quuxplusone, Mordante, #libc, ldionne Spies: ldionne, libcxx-commits Differential Revision: https://reviews.llvm.org/D116346
1 parent fb7bea0 commit c9dbf0f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

libcxx/include/__iterator/wrap_iter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ class __wrap_iter
6969
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
7070
__wrap_iter& operator=(const __wrap_iter& __x)
7171
{
72-
if (this != _VSTD::addressof(__x) && !__libcpp_is_constant_evaluated())
72+
if (this != _VSTD::addressof(__x))
7373
{
74-
__get_db()->__iterator_copy(this, _VSTD::addressof(__x));
74+
if (!__libcpp_is_constant_evaluated())
75+
__get_db()->__iterator_copy(this, _VSTD::addressof(__x));
7576
__i = __x.__i;
7677
}
7778
return *this;

libcxx/test/std/strings/basic.string/string.iterators/iterators.pass.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
#include "test_macros.h"
2424

2525
template<class C>
26-
void test()
26+
TEST_CONSTEXPR_CXX20 void test()
2727
{
2828
{ // N3644 testing
2929
typename C::iterator ii1{}, ii2{};
3030
typename C::iterator ii4 = ii1;
3131
typename C::const_iterator cii{};
32+
3233
assert ( ii1 == ii2 );
3334
assert ( ii1 == ii4 );
3435

@@ -49,10 +50,17 @@ void test()
4950
assert (cii - ii1 == 0);
5051
assert (ii1 - cii == 0);
5152
}
53+
{
54+
C a;
55+
typename C::iterator i1 = a.begin();
56+
typename C::iterator i2;
57+
assert ( i1 != i2 );
58+
i2 = i1;
59+
assert ( i1 == i2 );
60+
}
5261
}
5362

54-
int main(int, char**)
55-
{
63+
TEST_CONSTEXPR_CXX20 bool test() {
5664
test<std::string>();
5765
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
5866
test<std::wstring>();
@@ -65,5 +73,14 @@ int main(int, char**)
6573
test<std::u16string>();
6674
test<std::u32string>();
6775

76+
return true;
77+
}
78+
79+
int main(int, char**)
80+
{
81+
test();
82+
#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L
83+
static_assert(test());
84+
#endif
6885
return 0;
6986
}

0 commit comments

Comments
 (0)