Skip to content

Commit e8f85cf

Browse files
committed
[ArrayRef] Bring MutableArrayRef's constructor in line with ArrayRef
This time when the argument has a data member returning a mutable pointer.
1 parent d144eb1 commit e8f85cf

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

llvm/include/llvm/ADT/ArrayRef.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,16 @@ namespace llvm {
320320
/// Construct a MutableArrayRef from a range.
321321
MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
322322

323-
/// Construct a MutableArrayRef from a SmallVector.
324-
/*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
325-
: ArrayRef<T>(Vec) {}
326-
327-
/// Construct a MutableArrayRef from a std::vector.
328-
/*implicit*/ MutableArrayRef(std::vector<T> &Vec)
329-
: ArrayRef<T>(Vec) {}
323+
/// Construct a MutableArrayRef from a type that has a data() method that
324+
/// returns a pointer convertible to T *.
325+
template <typename C,
326+
typename = std::enable_if_t<
327+
std::conjunction_v<
328+
std::is_convertible<
329+
decltype(std::declval<C &>().data()) *, T *const *>,
330+
std::is_integral<decltype(std::declval<C &>().size())>>,
331+
void>>
332+
/*implicit*/ constexpr MutableArrayRef(const C &V) : ArrayRef<T>(V) {}
330333

331334
/// Construct a MutableArrayRef from a std::array
332335
template <size_t N>

llvm/unittests/ADT/ArrayRefTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ static_assert(std::is_constructible_v<ArrayRef<int>, std::span<int>>,
421421
"should be able to construct ArrayRef from mutable std::span");
422422
static_assert(!std::is_constructible_v<std::span<int>, ArrayRef<int>>,
423423
"cannot construct mutable std::span from ArrayRef");
424+
425+
static_assert(
426+
!std::is_constructible_v<MutableArrayRef<int>, std::span<const int>>,
427+
"cannot construct MutableArrayRef from const std::span");
428+
static_assert(
429+
std::is_constructible_v<std::span<const int>, MutableArrayRef<int>>,
430+
"should be able to construct const std::span from MutableArrayRef");
431+
static_assert(
432+
std::is_constructible_v<MutableArrayRef<int>, std::span<int>>,
433+
"should be able to construct MutableArrayRef from mutable std::span");
424434
#endif
425435

426436
} // end anonymous namespace

0 commit comments

Comments
 (0)