-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ADT] Require base equality in indexed_accessor_iterator::operator==() #107856
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
Conversation
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).
@River707 I think you're effectively the maintainer of this iterator class (and the surrounding machinery), please take a look! |
@llvm/pr-subscribers-llvm-adt Author: Andrei Golubev (andrey-golubev) ChangesSimilarly to operator<(), equality-comparing iterators from different ranges must really be forbidden. The preconditions for being able to do Full diff: https://github.com/llvm/llvm-project/pull/107856.diff 1 Files Affected:
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");
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth a go - you've run check-all with this & verified to a reasonable degree that there aren't existing violations of this invariant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I don't remember this actually being a requirement in practice, it should be fine!
I did run
with stack trace pointing to:
but i don't think it's related? also, could someone click merge for this one (I do not have merge rights)? |
Similarly to operator<(), equality-comparing iterators from different ranges must really be forbidden. The preconditions for being able to do
it1 < it2
andit1 != it2
(orit1 == 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).