[PATCH] D25446: Disallow ArrayRef assignment from temporaries

Jordan Rose via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 10 12:35:34 PDT 2016


jordan_rose created this revision.
jordan_rose added reviewers: chandlerc, dexonsmith.
jordan_rose added a subscriber: llvm-commits.
jordan_rose set the repository for this revision to rL LLVM.

Without this, the following statements will create ArrayRefs that refer to temporary storage that goes out of scope by the end of the line:

  c++
  someArrayRef = getSingleElement();
  someArrayRef = {elem1, elem2};

Note that the constructor still has this problem:

  c++
  ArrayRef<Element> someArrayRef = getSingleElement();
  ArrayRef<Element> someArrayRef = {elem1, elem2};

but that's a little harder to get rid of because we want to be able to use this in calls:

  c++
  takesArrayRef(getSingleElement());
  takesArrayRef({elem1, elem2});

Part of rdar://problem/16375365.


Repository:
  rL LLVM

https://reviews.llvm.org/D25446

Files:
  include/llvm/ADT/ArrayRef.h


Index: include/llvm/ADT/ArrayRef.h
===================================================================
--- include/llvm/ADT/ArrayRef.h
+++ include/llvm/ADT/ArrayRef.h
@@ -219,6 +219,22 @@
       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
     /// @{


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25446.74161.patch
Type: text/x-patch
Size: 989 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161010/38663af2/attachment.bin>


More information about the llvm-commits mailing list