[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.
This commit is contained in:
Nikolas Klauser 2025-12-12 09:06:50 +01:00 committed by GitHub
parent 4fc5b6d8c4
commit a34a92d9e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 70 additions and 10 deletions

View File

@ -128,5 +128,9 @@ ABI Affecting Changes
``_LIBCPP_DEPRECATED_ABI_NON_TRIVIAL_ALLOCATOR``. Please inform the libc++ team if you need this flag, since it will
be removed in LLVM 24 if there is no evidence that it's required.
- ``bitset::operator[]`` now returns ``bool``, making libc++ conforming. The behaviour can be reverted by defining
``_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF``. Please inform the libc++ team if you need this flag,
since it will be removed in LLVM 24 if there is no evidence that it's required.
Build System Changes
--------------------

View File

@ -612,7 +612,7 @@ public:
_LIBCPP_HIDE_FROM_ABI bitset& flip(size_t __pos);
// element access:
#ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
#ifndef _LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
_LIBCPP_HIDE_FROM_ABI bool operator[](size_t __p) const { return __base::__make_ref(__p); }
#else
_LIBCPP_HIDE_FROM_ABI __const_reference operator[](size_t __p) const { return __base::__make_ref(__p); }

View File

@ -680,7 +680,8 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip(size_t __pos);
// element access:
# ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
// TODO(LLVM 24): Remove the opt-out
# ifndef _LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
return __base::__make_ref(__p);

View File

@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// constexpr bool operator[](size_t pos) const; // constexpr since C++23
// Make sure that `_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF` reverts to the old behaviour.
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
#include <bitset>
#include <cassert>
#include <cstddef>
#include <vector>
#include "../../../std/utilities/template.bitset/bitset_test_cases.h"
#include "test_macros.h"
template <std::size_t N>
TEST_CONSTEXPR_CXX23 void test_index_const() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> const v = cases[c];
if (v.size() > 0) {
assert(v[N / 2] == v.test(N / 2));
}
}
ASSERT_SAME_TYPE(decltype(cases[0][0]), typename std::bitset<N>::__const_reference);
}
TEST_CONSTEXPR_CXX23 bool test() {
test_index_const<0>();
test_index_const<1>();
test_index_const<31>();
test_index_const<32>();
test_index_const<33>();
test_index_const<63>();
test_index_const<64>();
test_index_const<65>();
std::bitset<1> set_;
set_[0] = false;
const auto& set = set_;
auto b = set[0];
set_[0] = true;
assert(b);
return true;
}
int main(int, char**) {
test();
test_index_const<1000>(); // not in constexpr because of constexpr evaluation step limits
#if TEST_STD_VER > 20
static_assert(test());
#endif
return 0;
}

View File

@ -25,11 +25,7 @@ TEST_CONSTEXPR_CXX23 void test_index_const() {
assert(v[N / 2] == v.test(N / 2));
}
}
#if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
ASSERT_SAME_TYPE(decltype(cases[0][0]), bool);
#else
ASSERT_SAME_TYPE(decltype(cases[0][0]), typename std::bitset<N>::__const_reference);
#endif
}
TEST_CONSTEXPR_CXX23 bool test() {
@ -47,11 +43,7 @@ TEST_CONSTEXPR_CXX23 bool test() {
const auto& set = set_;
auto b = set[0];
set_[0] = true;
#if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
assert(!b);
#else
assert(b);
#endif
return true;
}