This is an archive of the discontinued LLVM Phabricator instance.

ADT: Fix const-correctness of iterator facade
ClosedPublic

Authored by dexonsmith on Nov 12 2021, 12:51 PM.

Details

Reviewers
dblaikie
Summary

Fix the const-ness of iterator_facade_base::operator-> and
iterator_facade_base::operator[]. This is a follow-up to
1b651be0465de70cfa22ce4f715d3501a4dcffc1, which fixed const-ness of
various iterator adaptors.

Iterators, like the pointers that they generalize, have two types of
const.

  • The const qualifier on members indicates whether the iterator itself can be changed. This is analagous to int *const.
  • The const qualifier on return values of operator*(), operator[](), and operator->() controls whether the the pointed-to value can be changed. This is analogous to const int*.

If an iterator facade returns a handle to its own state, then T (and
PointerT and ReferenceT) should usually be const-qualified. Otherwise,
if clients are expected to modify the state itself, the field can be
declared mutable or a const_cast can be used.

Depends on a couple of in-flight patches.

Diff Detail

Event Timeline

dexonsmith requested review of this revision.Nov 12 2021, 12:51 PM
dexonsmith created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptNov 12 2021, 12:51 PM
dblaikie accepted this revision.Nov 12 2021, 2:01 PM

Looks good!

This revision is now accepted and ready to land.Nov 12 2021, 2:01 PM

Thanks for the reviews! Pushed in 6b9b86db9dd974585a5c71cf2e5231d1e3385f7c.

llvm/unittests/ADT/IteratorTest.cpp
85

I ignored this clang-format suggestion since I find &IntIterator::operator->== ... harder to read than &IntIterator::operator-> == ....

dexonsmith closed this revision.Nov 12 2021, 8:45 PM
dblaikie added inline comments.Nov 13 2021, 8:54 AM
llvm/unittests/ADT/IteratorTest.cpp
85

Yep, that seems entirely correct - these sort of expressions are uncommon so it's not too surprising that clang-format is misclassifying/misformatting them. (this behavior doesn't seem intentional - it's quite different from how other similar expressions would be formatted that didn't involve naming operator overloads, like x == &y would be formatted with spaces around the == and no space between & and y usually)

But if you wrap the LHS and RHS in () clang-format seems to do OK with it, if you like. No big deal either way, though.