Support ranges with overloaded const/non-const begin and end. (#7627)

If we find an overload set containing mulitple methods, discard any
non-const methods and try again. This allows libc++'s `std::vector` to
be iterated with range-based for.
This commit is contained in:
Richard Smith
2026-08-11 19:14:37 +00:00
committed by GitHub
parent e10827ed84
commit 0f93cbd370
3 changed files with 521 additions and 0 deletions
@@ -90,6 +90,33 @@ fn Test() {
Core.AssertIsRange(Cpp.ConstRangeWithSentinel);
}
// --- methods_overloaded.carbon
library "[[@TEST_NAME]]";
import Core library "cpp_range_for_iterate";
import Cpp inline '''c++
struct OverloadedRange {
struct ConstIterator {};
struct ConstSentinel {};
struct Iterator {};
struct Sentinel {};
auto begin() -> Iterator;
auto end() -> Sentinel;
auto begin() const -> ConstIterator;
auto end() const -> ConstSentinel;
};
''';
fn Test() {
Core.AssertIsRange(Cpp.OverloadedRange);
// We should pick the const overloads.
let _: Core.CppRangeForIterate where
.Iterator = Cpp.OverloadedRange.ConstIterator and
.Sentinel = Cpp.OverloadedRange.ConstSentinel = Cpp.OverloadedRange;
}
// --- adl_return_same_types.carbon
library "[[@TEST_NAME]]";