[libcxx-commits] [libcxx] [libc++] Fix ambiguity in ranges::advance and ranges::next affecting flat_set (PR #177773)
Amgad Emara via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Mar 15 16:44:32 PDT 2026
https://github.com/TheAmgadX updated https://github.com/llvm/llvm-project/pull/177773
>From d6447685c25ac490f2269e24446c02657484a551 Mon Sep 17 00:00:00 2001
From: TheAmgadX <theamgadx at gmail.com>
Date: Sat, 24 Jan 2026 17:07:55 +0200
Subject: [PATCH 1/3] [libc++] Fix ambiguity in ranges::advance and
ranges::next
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Fix an overload ambiguity in std::ranges::advance when the sentinel type is the
same as the iterator’s difference type.
After fixing ranges::advance, the same ambiguity was exposed in
std::ranges::next, which forwards to ranges::advance. The ambiguity is
triggered in std::flat_set initializer-list construction via the following
call chain:
std::__inplace_merge
-> std::__upper_bound
-> _IterOps::next
Both ranges::advance and ranges::next are now constrained to prevent the
sentinel overload from participating when the sentinel matches
iter_difference_t<I>.
Fixes #175091.
Tests:
- flat_set initializer_list.compile.pass.cpp
---
libcxx/include/__iterator/advance.h | 1 +
libcxx/include/__iterator/next.h | 1 +
.../initializer_list.compile.pass.cpp | 26 +++++++++++++++++++
3 files changed, 28 insertions(+)
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h
index c7d3c1f0e8f05..e83cde609f5b2 100644
--- a/libcxx/include/__iterator/advance.h
+++ b/libcxx/include/__iterator/advance.h
@@ -120,6 +120,7 @@ struct __advance {
// Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound_sentinel)
// denotes a range.
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
+ requires (!same_as<_Sp, iter_difference_t<_Ip>>)
_LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
// If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound_sentinel)`.
if constexpr (assignable_from<_Ip&, _Sp>) {
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index 1143ab31ff7c2..3348f5b6fb8b4 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -49,6 +49,7 @@ struct __next {
}
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
+ requires (!same_as<_Sp, iter_difference_t<_Ip>>)
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp __bound_sentinel) const {
ranges::advance(__x, __bound_sentinel);
return __x;
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
new file mode 100644
index 0000000000000..194bd534d267c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <flat_set>
+
+struct T {
+ T(const auto&);
+ friend bool operator==(T, T);
+};
+
+struct Comp {
+ bool operator()(T, T) const;
+};
+
+int main(int, char**) {
+ std::flat_set<T, Comp> x = {0};
+ (void)x;
+ return 0;
+}
>From b79b7956d4f5afdc5d98115f735f6c96f43efbe7 Mon Sep 17 00:00:00 2001
From: TheAmgadX <theamgadx at gmail.com>
Date: Sun, 25 Jan 2026 22:07:54 +0200
Subject: [PATCH 2/3] [libc++] Implement LWG4510: Fix ambiguity in
ranges::advance and ranges::next
---
libcxx/docs/Status/Cxx2cIssues.csv | 1 +
libcxx/include/__iterator/advance.h | 2 +-
libcxx/include/__iterator/next.h | 2 +-
3 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/libcxx/docs/Status/Cxx2cIssues.csv b/libcxx/docs/Status/Cxx2cIssues.csv
index 60b1bd6ff70da..bc8fd3ca5e7df 100644
--- a/libcxx/docs/Status/Cxx2cIssues.csv
+++ b/libcxx/docs/Status/Cxx2cIssues.csv
@@ -263,4 +263,5 @@
"`LWG4139 <https://wg21.link/LWG4139>`__","§[time.zone.leap] recursive constraint in ``<=>``","Not Adopted Yet","|Complete|","20","`#118369 <https://github.com/llvm/llvm-project/issues/118369>`__",""
"`LWG3456 <https://wg21.link/LWG3456>`__","Pattern used by ``std::from_chars`` is underspecified (option B)","Not Adopted Yet","|Complete|","20","`#118370 <https://github.com/llvm/llvm-project/issues/118370>`__",""
"`LWG3882 <https://wg21.link/LWG3882>`__","``tuple`` relational operators have confused friendships","Not Adopted Yet","|Complete|","22","","The comparison operators are constrained harder than the proposed resolution. libstdc++ and MSVC STL do the same."
+"`LWG4510 <https://wg21.link/LWG4510>`__","Ambiguity of ``ranges::advance`` and ``ranges::next``","Not Adopted Yet","|Complete|","","`#175091 <https://github.com/llvm/llvm-project/issues/175091>`__",""
"","","","","","",""
diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h
index e83cde609f5b2..204e6012d1147 100644
--- a/libcxx/include/__iterator/advance.h
+++ b/libcxx/include/__iterator/advance.h
@@ -120,7 +120,7 @@ struct __advance {
// Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound_sentinel)
// denotes a range.
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
- requires (!same_as<_Sp, iter_difference_t<_Ip>>)
+ requires (!__integer_like<_Sp>) && sentinel_for<_Sp, _Ip>
_LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
// If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound_sentinel)`.
if constexpr (assignable_from<_Ip&, _Sp>) {
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index 3348f5b6fb8b4..ecd561318ad7b 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -49,7 +49,7 @@ struct __next {
}
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
- requires (!same_as<_Sp, iter_difference_t<_Ip>>)
+ requires (!__integer_like<_Sp>) && sentinel_for<_Sp, _Ip>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp __bound_sentinel) const {
ranges::advance(__x, __bound_sentinel);
return __x;
>From ab3f9336b3ffdad79a76b0b882170d6d59cd4cc7 Mon Sep 17 00:00:00 2001
From: TheAmgadX <theamgadx at gmail.com>
Date: Sun, 1 Mar 2026 16:40:37 +0200
Subject: [PATCH 3/3] [libc++] Add regression tests for LWG4510 and GH175091
This patch adds regression tests for std::ranges::next and
std::ranges::advance to ensure they are not susceptible to ADL
ambiguity when used with types providing a custom operator==.
This follows the reproducer identified in LWG4510 and
github.com/llvm/llvm-project/issues/175091.
- Added license headers and minimum language requirements to tests.
- Included <ranges> header comments as per libc++ style.
- Added a custom struct T to the test cases to verify the ADL fix.
---
libcxx/include/__iterator/advance.h | 1 -
libcxx/include/__iterator/concepts.h | 4 +-
libcxx/include/__iterator/next.h | 1 -
.../initializer_list.compile.pass.cpp | 26 -----------
.../flat.set.cons/initializer_list.pass.cpp | 16 +++++++
.../sentinel_for.integer_like.pass.cpp | 21 +++++++++
.../advance.integer_like.pass.cpp | 45 +++++++++++++++++++
.../next.integer_like.pass.cpp | 43 ++++++++++++++++++
8 files changed, 128 insertions(+), 29 deletions(-)
delete mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
create mode 100644 libcxx/test/std/iterators/iterator.requirements/iterator.concepts/iterator.concept.sentinel/sentinel_for.integer_like.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.iter.ops/range.iter.op.advance/advance.integer_like.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.iter.ops/range.iter.op.next/next.integer_like.pass.cpp
diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h
index 204e6012d1147..c7d3c1f0e8f05 100644
--- a/libcxx/include/__iterator/advance.h
+++ b/libcxx/include/__iterator/advance.h
@@ -120,7 +120,6 @@ struct __advance {
// Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound_sentinel)
// denotes a range.
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
- requires (!__integer_like<_Sp>) && sentinel_for<_Sp, _Ip>
_LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
// If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound_sentinel)`.
if constexpr (assignable_from<_Ip&, _Sp>) {
diff --git a/libcxx/include/__iterator/concepts.h b/libcxx/include/__iterator/concepts.h
index 3b43920443636..b1f3b74a9808b 100644
--- a/libcxx/include/__iterator/concepts.h
+++ b/libcxx/include/__iterator/concepts.h
@@ -138,7 +138,9 @@ concept input_or_output_iterator = requires(_Ip __i) {
// [iterator.concept.sentinel]
template <class _Sp, class _Ip>
-concept sentinel_for = semiregular<_Sp> && input_or_output_iterator<_Ip> && __weakly_equality_comparable_with<_Sp, _Ip>;
+concept sentinel_for =
+ semiregular<_Sp> && !__integer_like<_Sp> && input_or_output_iterator<_Ip> &&
+ __weakly_equality_comparable_with<_Sp, _Ip>;
template <class, class>
inline constexpr bool disable_sized_sentinel_for = false;
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index ecd561318ad7b..1143ab31ff7c2 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -49,7 +49,6 @@ struct __next {
}
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
- requires (!__integer_like<_Sp>) && sentinel_for<_Sp, _Ip>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp __bound_sentinel) const {
ranges::advance(__x, __bound_sentinel);
return __x;
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
deleted file mode 100644
index 194bd534d267c..0000000000000
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
+++ /dev/null
@@ -1,26 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17
-
-#include <flat_set>
-
-struct T {
- T(const auto&);
- friend bool operator==(T, T);
-};
-
-struct Comp {
- bool operator()(T, T) const;
-};
-
-int main(int, char**) {
- std::flat_set<T, Comp> x = {0};
- (void)x;
- return 0;
-}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.pass.cpp
index 2a8e4c40ebe2b..b24c65920b178 100644
--- a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.pass.cpp
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.pass.cpp
@@ -35,6 +35,15 @@ struct DefaultCtableComp {
bool default_constructed_ = false;
};
+struct T {
+ constexpr T(const auto&) {}
+ friend constexpr bool operator==(T, T) { return true; }
+};
+
+struct Comp {
+ constexpr bool operator()(T, T) const { return false; }
+};
+
template <template <class...> class KeyContainer>
constexpr void test() {
int expected[] = {1, 2, 3, 5};
@@ -108,6 +117,13 @@ constexpr void test() {
}
constexpr bool test() {
+ {
+ // github issue: https://github.com/llvm/llvm-project/issues/175091
+ // LWG4510: Ambiguity of std::ranges::advance and std::ranges::next when the difference type is also a sentinel type
+ std::flat_set<T, Comp> x = {0};
+ (void)x;
+ }
+
{
// The constructors in this subclause shall not participate in overload
// resolution unless uses_allocator_v<container_type, Alloc> is true.
diff --git a/libcxx/test/std/iterators/iterator.requirements/iterator.concepts/iterator.concept.sentinel/sentinel_for.integer_like.pass.cpp b/libcxx/test/std/iterators/iterator.requirements/iterator.concepts/iterator.concept.sentinel/sentinel_for.integer_like.pass.cpp
new file mode 100644
index 0000000000000..bdc9edc1e78c1
--- /dev/null
+++ b/libcxx/test/std/iterators/iterator.requirements/iterator.concepts/iterator.concept.sentinel/sentinel_for.integer_like.pass.cpp
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+//
+//===----------------------------------------------------------------------===//
+
+#include <iterator>
+#include <concepts>
+#include <cstddef>
+
+static_assert(!std::sentinel_for<int, int*>);
+static_assert(!std::sentinel_for<long, int*>);
+static_assert(!std::sentinel_for<std::ptrdiff_t, int*>);
+static_assert(!std::sentinel_for<unsigned, int*>);
+
+// sanity checks
+
+static_assert(std::sentinel_for<int*, int*>);
+static_assert(std::sentinel_for<const int*, int*>);
+
+int main() {}
diff --git a/libcxx/test/std/ranges/range.iter.ops/range.iter.op.advance/advance.integer_like.pass.cpp b/libcxx/test/std/ranges/range.iter.ops/range.iter.op.advance/advance.integer_like.pass.cpp
new file mode 100644
index 0000000000000..d0dd5369ea4d0
--- /dev/null
+++ b/libcxx/test/std/ranges/range.iter.ops/range.iter.op.advance/advance.integer_like.pass.cpp
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <ranges>
+
+// std::ranges::advance
+
+// Regression test for https://github.com/llvm/llvm-project/issues/175091
+// LWG4510: https://cplusplus.github.io/LWG/issue4510
+
+#include <ranges>
+#include <cassert>
+
+struct T {
+ constexpr T(int) {}
+ constexpr T(const auto&) {}
+ friend constexpr bool operator==(T, T) { return true; }
+};
+
+int main() {
+ {
+ int buffer[10];
+ int* it = buffer;
+
+ std::ranges::advance(it, 3);
+
+ assert(it == buffer + 3);
+ }
+
+ // LWG4510 Reproducer: Test with type T that has a conversion constructor
+ // and an operator==, which previously caused ambiguity in ranges::advance.
+ {
+ T t{0};
+ T* it = &t;
+ std::ranges::advance(it, 0);
+ assert(it == &t);
+ }
+}
diff --git a/libcxx/test/std/ranges/range.iter.ops/range.iter.op.next/next.integer_like.pass.cpp b/libcxx/test/std/ranges/range.iter.ops/range.iter.op.next/next.integer_like.pass.cpp
new file mode 100644
index 0000000000000..19b48f9890527
--- /dev/null
+++ b/libcxx/test/std/ranges/range.iter.ops/range.iter.op.next/next.integer_like.pass.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <ranges>
+
+// std::ranges::next
+
+// Regression test for https://github.com/llvm/llvm-project/issues/175091
+// LWG4510: https://cplusplus.github.io/LWG/issue4510
+
+#include <ranges>
+#include <cassert>
+
+struct T {
+ constexpr T(int) {}
+ constexpr T(const auto&) {}
+ friend constexpr bool operator==(T, T) { return true; }
+};
+
+int main() {
+ {
+ int buffer[10];
+ int* it = buffer;
+ auto result = std::ranges::next(it, 5);
+ assert(result == buffer + 5);
+ }
+
+ // LWG4510 Reproducer: Test with type T that has a conversion constructor
+ // and an operator==, which previously caused ambiguity in ranges::advance.
+ {
+ T t{0};
+ T* it = &t;
+ auto result = std::ranges::next(it, 0);
+ assert(result == &t);
+ }
+}
More information about the libcxx-commits
mailing list