[PATCH] D145373: Fix #61122

Guillaume Chatelet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 6 06:16:00 PST 2023


gchatelet created this revision.
gchatelet added a reviewer: kuhar.
Herald added a project: All.
gchatelet requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145373

Files:
  llvm/include/llvm/ADT/Sequence.h
  llvm/unittests/ADT/SequenceTest.cpp


Index: llvm/unittests/ADT/SequenceTest.cpp
===================================================================
--- llvm/unittests/ADT/SequenceTest.cpp
+++ llvm/unittests/ADT/SequenceTest.cpp
@@ -296,4 +296,12 @@
               ElementsAre(UntypedEnum::A));
 }
 
+TEST(SequenceTest, Repro) {
+  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
Index: llvm/include/llvm/ADT/Sequence.h
===================================================================
--- llvm/include/llvm/ADT/Sequence.h
+++ 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<T>(Value)) {}
@@ -198,9 +198,9 @@
   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; }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145373.502611.patch
Type: text/x-patch
Size: 1573 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230306/c30f2ee2/attachment.bin>


More information about the llvm-commits mailing list