[llvm] bff3682 - Fix SafeIntIterator reference type
Guillaume Chatelet via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 6 07:28:47 PST 2023
Author: Guillaume Chatelet
Date: 2023-03-06T15:28:25Z
New Revision: bff3682e9ede6c317bbfb666e64a7d4927ba9055
URL: https://github.com/llvm/llvm-project/commit/bff3682e9ede6c317bbfb666e64a7d4927ba9055
DIFF: https://github.com/llvm/llvm-project/commit/bff3682e9ede6c317bbfb666e64a7d4927ba9055.diff
LOG: Fix SafeIntIterator reference type
We explicitly state that the `reference` type for Sequence iterator is a `value_type`.
Since the iterator is a lazy generator, it cannot point to any memory and so it cannot have a reference type.
Fixes https://github.com/llvm/llvm-project/issues/61122
Differential Revision: https://reviews.llvm.org/D145373
Added:
Modified:
llvm/include/llvm/ADT/Sequence.h
llvm/unittests/ADT/SequenceTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/Sequence.h b/llvm/include/llvm/ADT/Sequence.h
index 1153352d8b24f..ddda9a95a7bc6 100644
--- a/llvm/include/llvm/ADT/Sequence.h
+++ b/llvm/include/llvm/ADT/Sequence.h
@@ -190,7 +190,7 @@ template <typename T, bool IsReverse> struct SafeIntIterator {
using value_type = T;
using
diff erence_type = intmax_t;
using pointer = T *;
- using reference = T &;
+ using reference = value_type; // The iterator does not reference memory.
// Construct from T.
explicit SafeIntIterator(T Value) : SI(CheckedInt::from<T>(Value)) {}
@@ -198,9 +198,9 @@ template <typename T, bool IsReverse> struct SafeIntIterator {
SafeIntIterator(const SafeIntIterator<T, !IsReverse> &O) : SI(O.SI) {}
// Dereference
- value_type operator*() const { return SI.to<T>(); }
+ reference operator*() const { return SI.to<T>(); }
// Indexing
- value_type operator[](intmax_t Offset) const { return *(*this + Offset); }
+ reference operator[](intmax_t Offset) const { return *(*this + Offset); }
// Can be compared for equivalence using the equality/inequality operators.
bool operator==(const SafeIntIterator &O) const { return SI == O.SI; }
diff --git a/llvm/unittests/ADT/SequenceTest.cpp b/llvm/unittests/ADT/SequenceTest.cpp
index aec1ea457aeb3..acc15bf4daae6 100644
--- a/llvm/unittests/ADT/SequenceTest.cpp
+++ b/llvm/unittests/ADT/SequenceTest.cpp
@@ -296,4 +296,13 @@ TEST(SequenceTest, NonIterableEnums) {
ElementsAre(UntypedEnum::A));
}
+// Reproducer for https://github.com/llvm/llvm-project/issues/61122
+TEST(SequenceTest, CorrectReferenceType) {
+ std::vector<int> vals = {1, 2, 3};
+ detail::SafeIntIterator<int, false> begin(4);
+ detail::SafeIntIterator<int, false> end(6);
+ vals.insert(vals.end(), begin, end);
+ EXPECT_THAT(vals, ElementsAre(1, 2, 3, 4, 5));
+}
+
} // namespace
More information about the llvm-commits
mailing list