From 8d879edb0b630614d6bc00fe14ab2388cdbc09fd Mon Sep 17 00:00:00 2001 From: Hewill Kang Date: Sat, 3 Jan 2026 22:52:26 +0800 Subject: [PATCH] [libc++] Make sure `flat_{multi}map::key_compare` handle `boolean-testable` correctly (#132621) This is sibling of [#69378](https://github.com/llvm/llvm-project/pull/69378). --------- Co-authored-by: Hui Xie Co-authored-by: A. Jiang --- libcxx/include/__flat_map/flat_map.h | 4 +- libcxx/include/__flat_map/flat_multimap.h | 2 +- .../robust_against_nonbool.compile.pass.cpp | 51 +++++++++++++++++++ .../robust_against_nonbool.compile.pass.cpp | 47 +++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/robust_against_nonbool.compile.pass.cpp create mode 100644 libcxx/test/std/containers/container.adaptors/flat.multimap/robust_against_nonbool.compile.pass.cpp diff --git a/libcxx/include/__flat_map/flat_map.h b/libcxx/include/__flat_map/flat_map.h index 97180ab99e44..50487cada24c 100644 --- a/libcxx/include/__flat_map/flat_map.h +++ b/libcxx/include/__flat_map/flat_map.h @@ -934,7 +934,7 @@ private: __compare_(std::forward<_CompArg>(__comp)...) {} _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __is_sorted_and_unique(auto&& __key_container) const { - auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) { return !__compare_(__x, __y); }; + auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) -> bool { return !__compare_(__x, __y); }; return ranges::adjacent_find(__key_container, __greater_or_equal_to) == ranges::end(__key_container); } @@ -967,7 +967,7 @@ private: auto __zv = ranges::views::zip(__containers_.keys, __containers_.values); auto __append_start_offset = __containers_.keys.size() - __num_of_appended; auto __end = __zv.end(); - auto __compare_key = [this](const auto& __p1, const auto& __p2) { + auto __compare_key = [this](const auto& __p1, const auto& __p2) -> bool { return __compare_(std::get<0>(__p1), std::get<0>(__p2)); }; if constexpr (!_WasSorted) { diff --git a/libcxx/include/__flat_map/flat_multimap.h b/libcxx/include/__flat_map/flat_multimap.h index f0540a7d5e8a..72e3b5f21670 100644 --- a/libcxx/include/__flat_map/flat_multimap.h +++ b/libcxx/include/__flat_map/flat_multimap.h @@ -854,7 +854,7 @@ private: auto __zv = ranges::views::zip(__containers_.keys, __containers_.values); auto __append_start_offset = __containers_.keys.size() - __num_appended; auto __end = __zv.end(); - auto __compare_key = [this](const auto& __p1, const auto& __p2) { + auto __compare_key = [this](const auto& __p1, const auto& __p2) -> bool { return __compare_(std::get<0>(__p1), std::get<0>(__p2)); }; if constexpr (!_WasSorted) { diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/robust_against_nonbool.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/robust_against_nonbool.compile.pass.cpp new file mode 100644 index 000000000000..1c4bedabf8b1 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/robust_against_nonbool.compile.pass.cpp @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// REQUIRES: std-at-least-c++23 + +// +// +// flat_map should support comparator that return a non-boolean +// value as long as the returned type is implicitly convertible to bool. + +#include +#include +#include + +#include "boolean_testable.h" + +void test() { + using Key = StrictComparable; + using Value = StrictComparable; + std::flat_map m1; + std::flat_map m2(std::from_range, m1, StrictBinaryPredicate); + std::flat_map m3(std::sorted_unique, m1.keys(), m1.values(), StrictBinaryPredicate); + std::flat_map m4(m1.begin(), m1.end(), StrictBinaryPredicate); + m2.insert(m1.begin(), m1.end()); + m2.insert(std::sorted_unique, m1.begin(), m1.end()); + m2.insert_range(m1); + (void)m2.at(2); + m3[1] = 2; + m3.insert_or_assign(1, 2); + m4.try_emplace(1, 2); + m2.emplace(1, 2); + m2.emplace_hint(m2.begin(), 1, 2); + for (const auto& [k, v] : m2) { + (void)k; + (void)v; + } + (void)m2.find(Key{1}); + (void)m2.equal_range(Key{1}); + (void)(m2 == m2); + m2.erase(m2.begin()); + m2.erase(m2.begin(), m2.end()); + std::erase_if( + m2, [](std::pair&, const StrictComparable&>) -> BooleanTestable const& { + return yes; + }); +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.multimap/robust_against_nonbool.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.multimap/robust_against_nonbool.compile.pass.cpp new file mode 100644 index 000000000000..d5e2832390f5 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.multimap/robust_against_nonbool.compile.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// REQUIRES: std-at-least-c++23 + +// +// +// flat_multimap should support comparator that return a non-boolean +// value as long as the returned type is implicitly convertible to bool. + +#include +#include +#include + +#include "boolean_testable.h" + +void test() { + using Key = StrictComparable; + using Value = StrictComparable; + std::flat_multimap m1; + std::flat_multimap m2(std::from_range, m1, StrictBinaryPredicate); + std::flat_multimap m3(std::sorted_equivalent, m1.keys(), m1.values(), StrictBinaryPredicate); + std::flat_multimap m4(m1.begin(), m1.end(), StrictBinaryPredicate); + m2.insert(m1.begin(), m1.end()); + m2.insert(std::sorted_equivalent, m1.begin(), m1.end()); + m2.insert_range(m1); + m2.emplace(1, 2); + m2.emplace_hint(m2.begin(), 1, 2); + for (const auto& [k, v] : m2) { + (void)k; + (void)v; + } + (void)m2.find(Key{1}); + (void)m2.equal_range(Key{1}); + (void)(m2 == m2); + m2.erase(m2.begin()); + m2.erase(m2.begin(), m2.end()); + std::erase_if( + m2, [](std::pair&, const StrictComparable&>) -> BooleanTestable const& { + return yes; + }); +}