diff --git a/llvm/include/llvm/ADT/Sequence.h b/llvm/include/llvm/ADT/Sequence.h --- a/llvm/include/llvm/ADT/Sequence.h +++ b/llvm/include/llvm/ADT/Sequence.h @@ -190,7 +190,7 @@ using value_type = T; using difference_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(Value)) {} @@ -198,9 +198,9 @@ SafeIntIterator(const SafeIntIterator &O) : SI(O.SI) {} // Dereference - value_type operator*() const { return SI.to(); } + reference operator*() const { return SI.to(); } // 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 --- a/llvm/unittests/ADT/SequenceTest.cpp +++ b/llvm/unittests/ADT/SequenceTest.cpp @@ -296,4 +296,13 @@ ElementsAre(UntypedEnum::A)); } +// Reproducer for https://github.com/llvm/llvm-project/issues/61122 +TEST(SequenceTest, CorrectReferenceType) { + std::vector vals = {1, 2, 3}; + detail::SafeIntIterator begin(4); + detail::SafeIntIterator end(6); + vals.insert(vals.end(), begin, end); + EXPECT_THAT(vals, ElementsAre(1, 2, 3, 4, 5)); +} + } // namespace