[llvm] e8f85cf - [ArrayRef] Bring MutableArrayRef's constructor in line with ArrayRef

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 26 02:16:03 PDT 2025


Author: Benjamin Kramer
Date: 2025-06-26T11:12:05+02:00
New Revision: e8f85cf51fb9583767cd8ec23cc7eaeacbe2be35

URL: https://github.com/llvm/llvm-project/commit/e8f85cf51fb9583767cd8ec23cc7eaeacbe2be35
DIFF: https://github.com/llvm/llvm-project/commit/e8f85cf51fb9583767cd8ec23cc7eaeacbe2be35.diff

LOG: [ArrayRef] Bring MutableArrayRef's constructor in line with ArrayRef

This time when the argument has a data member returning a mutable
pointer.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/ArrayRef.h
    llvm/unittests/ADT/ArrayRefTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h
index ddd2c7ce68c83..892482d64e4a1 100644
--- a/llvm/include/llvm/ADT/ArrayRef.h
+++ b/llvm/include/llvm/ADT/ArrayRef.h
@@ -320,13 +320,16 @@ namespace llvm {
     /// Construct a MutableArrayRef from a range.
     MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
 
-    /// Construct a MutableArrayRef from a SmallVector.
-    /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
-    : ArrayRef<T>(Vec) {}
-
-    /// Construct a MutableArrayRef from a std::vector.
-    /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
-    : ArrayRef<T>(Vec) {}
+    /// Construct a MutableArrayRef from a type that has a data() method that
+    /// returns a pointer convertible to T *.
+    template <typename C,
+              typename = std::enable_if_t<
+                  std::conjunction_v<
+                      std::is_convertible<
+                          decltype(std::declval<C &>().data()) *, T *const *>,
+                      std::is_integral<decltype(std::declval<C &>().size())>>,
+                  void>>
+    /*implicit*/ constexpr MutableArrayRef(const C &V) : ArrayRef<T>(V) {}
 
     /// Construct a MutableArrayRef from a std::array
     template <size_t N>

diff  --git a/llvm/unittests/ADT/ArrayRefTest.cpp b/llvm/unittests/ADT/ArrayRefTest.cpp
index 3858d9064f9ca..985db1625454c 100644
--- a/llvm/unittests/ADT/ArrayRefTest.cpp
+++ b/llvm/unittests/ADT/ArrayRefTest.cpp
@@ -421,6 +421,16 @@ static_assert(std::is_constructible_v<ArrayRef<int>, std::span<int>>,
               "should be able to construct ArrayRef from mutable std::span");
 static_assert(!std::is_constructible_v<std::span<int>, ArrayRef<int>>,
               "cannot construct mutable std::span from ArrayRef");
+
+static_assert(
+    !std::is_constructible_v<MutableArrayRef<int>, std::span<const int>>,
+    "cannot construct MutableArrayRef from const std::span");
+static_assert(
+    std::is_constructible_v<std::span<const int>, MutableArrayRef<int>>,
+    "should be able to construct const std::span from MutableArrayRef");
+static_assert(
+    std::is_constructible_v<MutableArrayRef<int>, std::span<int>>,
+    "should be able to construct MutableArrayRef from mutable std::span");
 #endif
 
 } // end anonymous namespace


        


More information about the llvm-commits mailing list