Bug 1466909 - Use AddLvalueReference for UniquePtr's operator*(). r=froydnj
authorAndré Bargull <andre.bargull@gmail.com>
Thu, 07 Jun 2018 07:27:22 -0700
changeset 805580 17b140524c6ee9e1263175212dec5231bfe3059f
parent 805579 7b4cd53cbfb8dbc6207d34e549516b84ffe3601c
child 805581 8e386f33143372071bfbfeca7596a2144cf0ca85
push id112702
push userbmo:mh+mozilla@glandium.org
push dateFri, 08 Jun 2018 01:32:47 +0000
reviewersfroydnj
bugs1466909
milestone62.0a1
Bug 1466909 - Use AddLvalueReference for UniquePtr's operator*(). r=froydnj
mfbt/UniquePtr.h
mfbt/tests/TestUniquePtr.cpp
--- a/mfbt/UniquePtr.h
+++ b/mfbt/UniquePtr.h
@@ -309,17 +309,17 @@ public:
   }
 
   UniquePtr& operator=(decltype(nullptr))
   {
     reset(nullptr);
     return *this;
   }
 
-  T& operator*() const { return *get(); }
+  typename AddLvalueReference<T>::Type operator*() const { return *get(); }
   Pointer operator->() const
   {
     MOZ_ASSERT(get(), "dereferencing a UniquePtr containing nullptr");
     return get();
   }
 
   explicit operator bool() const { return get() != nullptr; }
 
--- a/mfbt/tests/TestUniquePtr.cpp
+++ b/mfbt/tests/TestUniquePtr.cpp
@@ -4,25 +4,27 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Compiler.h"
 #include "mozilla/Move.h"
 #include "mozilla/TypeTraits.h"
 #include "mozilla/UniquePtr.h"
+#include "mozilla/UniquePtrExtensions.h"
 #include "mozilla/Vector.h"
 
 #include <stddef.h>
 
 using mozilla::DefaultDelete;
 using mozilla::IsSame;
 using mozilla::MakeUnique;
 using mozilla::Swap;
 using mozilla::UniquePtr;
+using mozilla::UniqueFreePtr;
 using mozilla::Vector;
 
 #define CHECK(c) \
   do { \
     bool cond = !!(c); \
     MOZ_ASSERT(cond, "Failed assertion: " #c); \
     if (!cond) { \
       return false; \
@@ -557,16 +559,46 @@ TestMakeUnique()
   // various type mismatching to test "fuzzy" forwarding
   UniquePtr<Q> q4(MakeUnique<Q>('s', 66LL, 3.141592654, &q3));
 
   UniquePtr<char[]> c1(MakeUnique<char[]>(5));
 
   return true;
 }
 
+static bool
+TestVoid()
+{
+  // UniquePtr<void> supports all operations except operator*() and
+  // operator->().
+  UniqueFreePtr<void> p1(malloc(1));
+  UniqueFreePtr<void> p2;
+
+  auto x = p1.get();
+  CHECK(x != nullptr);
+  CHECK((IsSame<decltype(x), void*>::value));
+
+  p2.reset(p1.release());
+  CHECK(p1.get() == nullptr);
+  CHECK(p2.get() != nullptr);
+
+  p1 = std::move(p2);
+  CHECK(p1);
+  CHECK(!p2);
+
+  p1.swap(p2);
+  CHECK(!p1);
+  CHECK(p2);
+
+  p2 = nullptr;
+  CHECK(!p2);
+
+  return true;
+}
+
 int
 main()
 {
   TestDeleterType();
 
   if (!TestDefaultFree()) {
     return 1;
   }
@@ -583,10 +615,13 @@ main()
     return 1;
   }
   if (!TestArray()) {
     return 1;
   }
   if (!TestMakeUnique()) {
     return 1;
   }
+  if (!TestVoid()) {
+    return 1;
+  }
   return 0;
 }