aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/ArrayRefTest.cpp
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2025-06-25 19:33:34 +0200
committerGitHub <noreply@github.com>2025-06-25 19:33:34 +0200
commit0556a2aa187b86c28a9441aec3e98b9780a2c9ee (patch)
treefe7b934c8aa8080252c732495e8fbe7141f56a27 /llvm/unittests/ADT/ArrayRefTest.cpp
parentdf79c40c988a707d8458952df04afe82f77ffb0f (diff)
downloadllvm-0556a2aa187b86c28a9441aec3e98b9780a2c9ee.zip
llvm-0556a2aa187b86c28a9441aec3e98b9780a2c9ee.tar.gz
llvm-0556a2aa187b86c28a9441aec3e98b9780a2c9ee.tar.bz2
[ArrayRef] Provide constructors from any type with a data() member (#145735)
The assumption here is that if data() returns a pointer and size() exists it's something representing contiguous storage. Modeled after the behavior of C++20 std::span and enables two-way conversion between std::span (or really any span-like type) and ArrayRef. Add a unit test that verifies this if std::span is around. This also means we can get rid of specific conversions for std::vector and SmallVector.
Diffstat (limited to 'llvm/unittests/ADT/ArrayRefTest.cpp')
-rw-r--r--llvm/unittests/ADT/ArrayRefTest.cpp19
1 files changed, 18 insertions, 1 deletions
diff --git a/llvm/unittests/ADT/ArrayRefTest.cpp b/llvm/unittests/ADT/ArrayRefTest.cpp
index 39a4a9b..3858d90 100644
--- a/llvm/unittests/ADT/ArrayRefTest.cpp
+++ b/llvm/unittests/ADT/ArrayRefTest.cpp
@@ -8,10 +8,16 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Allocator.h"
-#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include <limits>
#include <vector>
+#if __has_include(<version>)
+#include <version>
+#endif
+#ifdef __cpp_lib_span
+#include <span>
+#endif
+
using namespace llvm;
// Check that the ArrayRef-of-pointer converting constructor only allows adding
@@ -406,4 +412,15 @@ TEST(ArrayRefTest, MutableArrayRefDeductionGuides) {
}
}
+#ifdef __cpp_lib_span
+static_assert(std::is_constructible_v<ArrayRef<int>, std::span<const int>>,
+ "should be able to construct ArrayRef from const std::span");
+static_assert(std::is_constructible_v<std::span<const int>, ArrayRef<int>>,
+ "should be able to construct const std::span from ArrayRef");
+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");
+#endif
+
} // end anonymous namespace