[PATCH] D145373: Fix SafeIntIterator reference type

Guillaume Chatelet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 6 07:28:53 PST 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGbff3682e9ede: Fix SafeIntIterator reference type (authored by gchatelet).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D145373/new/

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,13 @@
               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
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.502647.patch
Type: text/x-patch
Size: 1657 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230306/851c1acb/attachment.bin>


More information about the llvm-commits mailing list