Skip to content

[libc++] Fix uses of non-empty transparent comparator in <map> #152624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libcxx/include/map
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,12 @@ public:
# if _LIBCPP_STD_VER >= 14
template <typename _K2>
_LIBCPP_HIDE_FROM_ABI bool operator()(const _K2& __x, const _CP& __y) const {
return __comp_(__x, __y.__get_value().first);
return __comp_(__x, __y.first);
}

template <typename _K2>
_LIBCPP_HIDE_FROM_ABI bool operator()(const _CP& __x, const _K2& __y) const {
return __comp_(__x.__get_value().first, __y);
return __comp_(__x.first, __y);
}
# endif
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ int main(int, char**) {
typedef std::map<int, double, transparent_less_not_referenceable> M;
assert(M().count(C2Int{5}) == 0);
}
{
using M = std::map<int, double, transparent_less_nonempty>;
assert(M().count(C2Int{5}) == 0);
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ int main(int, char**) {
P result = example.equal_range(C2Int{5});
assert(result.first == result.second);
}
{
using M = std::map<int, double, transparent_less_nonempty>;
using P = std::pair<typename M::iterator, typename M::iterator>;
M example;
P result = example.equal_range(C2Int{5});
assert(result.first == result.second);
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ int main(int, char**) {
M example;
assert(example.find(C2Int{5}) == example.end());
}
{
using M = std::map<int, double, transparent_less_nonempty>;
M example;
assert(example.find(C2Int{5}) == example.end());
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ int main(int, char**) {
M example;
assert(example.lower_bound(C2Int{5}) == example.end());
}
{
using M = std::map<int, double, transparent_less_nonempty>;
M example;
assert(example.lower_bound(C2Int{5}) == example.end());
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ int main(int, char**) {
M example;
assert(example.upper_bound(C2Int{5}) == example.end());
}
{
using M = std::map<int, double, transparent_less_nonempty>;
M example;
assert(example.upper_bound(C2Int{5}) == example.end());
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ int main(int, char**) {
typedef std::multimap<int, double, transparent_less_not_referenceable> M;
assert(M().count(C2Int{5}) == 0);
}
{
using M = std::multimap<int, double, transparent_less_nonempty>;
assert(M().count(C2Int{5}) == 0);
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ int main(int, char**) {
P result = example.equal_range(C2Int{5});
assert(result.first == result.second);
}
{
using M = std::multimap<int, double, transparent_less_nonempty>;
using P = std::pair<typename M::iterator, typename M::iterator>;
M example;
P result = example.equal_range(C2Int{5});
assert(result.first == result.second);
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ int main(int, char**) {
M example;
assert(example.find(C2Int{5}) == example.end());
}
{
using M = std::multimap<int, double, transparent_less_nonempty>;
M example;
assert(example.find(C2Int{5}) == example.end());
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ int main(int, char**) {
M example;
assert(example.lower_bound(C2Int{5}) == example.end());
}
{
using M = std::multimap<int, double, transparent_less_nonempty>;
M example;
assert(example.lower_bound(C2Int{5}) == example.end());
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ int main(int, char**) {
M example;
assert(example.upper_bound(C2Int{5}) == example.end());
}
{
using M = std::multimap<int, double, transparent_less_nonempty>;
M example;
assert(example.upper_bound(C2Int{5}) == example.end());
}

return 0;
}
11 changes: 11 additions & 0 deletions libcxx/test/support/is_transparent.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ struct transparent_less_not_referenceable
using is_transparent = void () const &; // it's a type; a weird one, but a type
};

// Prevent regression when empty base class optimization is not suitable.
// See https://github.com/llvm/llvm-project/issues/152543.
struct transparent_less_nonempty {
template <class T, class U>
constexpr bool operator()(T&& t, U&& u) const {
return std::forward<T>(t) < std::forward<U>(u);
}
struct is_transparent {
} pad_; // making this comparator non-empty
};

struct transparent_less_no_type
{
template <class T, class U>
Expand Down
Loading