]> granicus.if.org Git - llvm/commitdiff
Re-apply "Disallow ArrayRef assignment from temporaries."
authorJordan Rose <jordan_rose@apple.com>
Tue, 11 Oct 2016 20:39:16 +0000 (20:39 +0000)
committerJordan Rose <jordan_rose@apple.com>
Tue, 11 Oct 2016 20:39:16 +0000 (20:39 +0000)
This re-applies r283798, disabled in r283803, with the static_assert
tests disabled under MSVC. The deleted functions still seem to catch
mistakes in MSVC, so it's not a significant loss.

Part of rdar://problem/16375365

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283935 91177308-0d34-0410-b5e6-96231b3b80d8

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

index c1d66c699035144e740c15095c4dbfb6375ca3b0..3efc09ddd36f500a59428e53f3d5e2cab07b5a9e 100644 (file)
@@ -219,6 +219,22 @@ namespace llvm {
       return Data[Index];
     }
 
+    /// Disallow accidental assignment from a temporary.
+    ///
+    /// The declaration here is extra complicated so that "arrayRef = {}"
+    /// continues to select the move assignment operator.
+    template <typename U>
+    typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type &
+    operator=(U &&Temporary) = delete;
+
+    /// Disallow accidental assignment from a temporary.
+    ///
+    /// The declaration here is extra complicated so that "arrayRef = {}"
+    /// continues to select the move assignment operator.
+    template <typename U>
+    typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type &
+    operator=(std::initializer_list<U>) = delete;
+
     /// @}
     /// @name Expensive Operations
     /// @{
index 43e5005e62d8e1dbfb050fde38795e202e52485f..ca75800f092578a08d4d16f74aa15cc49a615f59 100644 (file)
@@ -31,6 +31,26 @@ static_assert(
     !std::is_convertible<ArrayRef<volatile int *>, ArrayRef<int *>>::value,
     "Removing volatile");
 
+// Check that we can't accidentally assign a temporary location to an ArrayRef.
+// (Unfortunately we can't make use of the same thing with constructors.)
+//
+// Disable this check under MSVC; even MSVC 2015 isn't inconsistent between
+// std::is_assignable and actually writing such an assignment.
+#if !defined(_MSC_VER)
+static_assert(
+    !std::is_assignable<ArrayRef<int *>, int *>::value,
+    "Assigning from single prvalue element");
+static_assert(
+    !std::is_assignable<ArrayRef<int *>, int * &&>::value,
+    "Assigning from single xvalue element");
+static_assert(
+    std::is_assignable<ArrayRef<int *>, int * &>::value,
+    "Assigning from single lvalue element");
+static_assert(
+    !std::is_assignable<ArrayRef<int *>, std::initializer_list<int *>>::value,
+    "Assigning from an initializer list");
+#endif
+
 namespace {
 
 TEST(ArrayRefTest, AllocatorCopy) {
@@ -161,6 +181,14 @@ TEST(ArrayRefTest, InitializerList) {
   ArgTest12({1, 2});
 }
 
+TEST(ArrayRefTest, EmptyInitializerList) {
+  ArrayRef<int> A = {};
+  EXPECT_TRUE(A.empty());
+
+  A = {};
+  EXPECT_TRUE(A.empty());
+}
+
 // Test that makeArrayRef works on ArrayRef (no-op)
 TEST(ArrayRefTest, makeArrayRef) {
   static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};