Skip to content

Commit a34a92d

Browse files
authored
[libc++] Always return bool from bitset::operator[](size_t) const (#169894)
This takes an ABI break unconditionally, since it's small enough that nobody should be affected. This both simplifies `bitset` a bit and makes us more conforming.
1 parent 4fc5b6d commit a34a92d

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

libcxx/docs/ReleaseNotes/22.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,9 @@ ABI Affecting Changes
128128
``_LIBCPP_DEPRECATED_ABI_NON_TRIVIAL_ALLOCATOR``. Please inform the libc++ team if you need this flag, since it will
129129
be removed in LLVM 24 if there is no evidence that it's required.
130130

131+
- ``bitset::operator[]`` now returns ``bool``, making libc++ conforming. The behaviour can be reverted by defining
132+
``_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF``. Please inform the libc++ team if you need this flag,
133+
since it will be removed in LLVM 24 if there is no evidence that it's required.
134+
131135
Build System Changes
132136
--------------------

libcxx/include/__cxx03/bitset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ public:
612612
_LIBCPP_HIDE_FROM_ABI bitset& flip(size_t __pos);
613613

614614
// element access:
615-
#ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
615+
#ifndef _LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
616616
_LIBCPP_HIDE_FROM_ABI bool operator[](size_t __p) const { return __base::__make_ref(__p); }
617617
#else
618618
_LIBCPP_HIDE_FROM_ABI __const_reference operator[](size_t __p) const { return __base::__make_ref(__p); }

libcxx/include/bitset

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,8 @@ public:
680680
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip(size_t __pos);
681681

682682
// element access:
683-
# ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
683+
// TODO(LLVM 24): Remove the opt-out
684+
# ifndef _LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
684685
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
685686
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
686687
return __base::__make_ref(__p);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// constexpr bool operator[](size_t pos) const; // constexpr since C++23
10+
11+
// Make sure that `_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF` reverts to the old behaviour.
12+
13+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
14+
15+
#include <bitset>
16+
#include <cassert>
17+
#include <cstddef>
18+
#include <vector>
19+
20+
#include "../../../std/utilities/template.bitset/bitset_test_cases.h"
21+
#include "test_macros.h"
22+
23+
template <std::size_t N>
24+
TEST_CONSTEXPR_CXX23 void test_index_const() {
25+
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
26+
for (std::size_t c = 0; c != cases.size(); ++c) {
27+
std::bitset<N> const v = cases[c];
28+
if (v.size() > 0) {
29+
assert(v[N / 2] == v.test(N / 2));
30+
}
31+
}
32+
ASSERT_SAME_TYPE(decltype(cases[0][0]), typename std::bitset<N>::__const_reference);
33+
}
34+
35+
TEST_CONSTEXPR_CXX23 bool test() {
36+
test_index_const<0>();
37+
test_index_const<1>();
38+
test_index_const<31>();
39+
test_index_const<32>();
40+
test_index_const<33>();
41+
test_index_const<63>();
42+
test_index_const<64>();
43+
test_index_const<65>();
44+
45+
std::bitset<1> set_;
46+
set_[0] = false;
47+
const auto& set = set_;
48+
auto b = set[0];
49+
set_[0] = true;
50+
assert(b);
51+
52+
return true;
53+
}
54+
55+
int main(int, char**) {
56+
test();
57+
test_index_const<1000>(); // not in constexpr because of constexpr evaluation step limits
58+
#if TEST_STD_VER > 20
59+
static_assert(test());
60+
#endif
61+
62+
return 0;
63+
}

libcxx/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ TEST_CONSTEXPR_CXX23 void test_index_const() {
2525
assert(v[N / 2] == v.test(N / 2));
2626
}
2727
}
28-
#if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
2928
ASSERT_SAME_TYPE(decltype(cases[0][0]), bool);
30-
#else
31-
ASSERT_SAME_TYPE(decltype(cases[0][0]), typename std::bitset<N>::__const_reference);
32-
#endif
3329
}
3430

3531
TEST_CONSTEXPR_CXX23 bool test() {
@@ -47,11 +43,7 @@ TEST_CONSTEXPR_CXX23 bool test() {
4743
const auto& set = set_;
4844
auto b = set[0];
4945
set_[0] = true;
50-
#if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
5146
assert(!b);
52-
#else
53-
assert(b);
54-
#endif
5547

5648
return true;
5749
}

0 commit comments

Comments
 (0)