[llvm] [ADT] Require base equality in indexed_accessor_iterator::operator==() (PR #107856)

Andrei Golubev via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 9 05:46:29 PDT 2024


https://github.com/andrey-golubev created https://github.com/llvm/llvm-project/pull/107856

Similarly to operator<(), equality-comparing iterators from different ranges must really be forbidden. The preconditions for being able to do `it1 < it2` and `it1 != it2` (or `it1 == it2` for the matter) ought to be the same. Thus, there's little sense in keeping explicit base object comparison in operator==() whilst having this is a precondition in operator<() and operator-() (e.g. used for std::distance() and such).

>From 7eb026e65817eb0968bca2bcca1f0d26bea10471 Mon Sep 17 00:00:00 2001
From: "Golubev, Andrey" <andrey.golubev at intel.com>
Date: Mon, 9 Sep 2024 12:36:51 +0000
Subject: [PATCH] [ADT] Require base equality in
 indexed_accessor_iterator::operator==()

Similarly to operator<(), equality-comparing iterators from different
ranges must really be forbidden. The preconditions for being able to do
`it1 < it2` and `it1 != it2` (or `it1 == it2` for the matter) ought to
be the same. Thus, there's little sense in keeping explicit base object
comparison in operator==() whilst having this is a precondition in
operator<() and operator-() (e.g. used for std::distance() and such).
---
 llvm/include/llvm/ADT/STLExtras.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index e11d6cac7685e4..eb441bb31c9bc8 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1194,7 +1194,8 @@ class indexed_accessor_iterator
     return index - rhs.index;
   }
   bool operator==(const indexed_accessor_iterator &rhs) const {
-    return base == rhs.base && index == rhs.index;
+    assert(base == rhs.base && "incompatible iterators");
+    return index == rhs.index;
   }
   bool operator<(const indexed_accessor_iterator &rhs) const {
     assert(base == rhs.base && "incompatible iterators");



More information about the llvm-commits mailing list