Bug 1316206 - Make RefPtr(decltype(nullptr)) MOZ_IMPLICIT - r=froydnj
authorGerald Squelart <gsquelart@mozilla.com>
Tue, 08 Nov 2016 15:12:33 +1100
changeset 351893 30d3890fca9847a283859a53043277713b7eadab
parent 351892 9380a01deaf545a1ef339caaf15ffb951c158681
child 351894 53f988e1c1fd34ca2ff5f42a59fa05b4a768a1e5
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1316206
milestone52.0a1
Bug 1316206 - Make RefPtr(decltype(nullptr)) MOZ_IMPLICIT - r=froydnj Making this constructor non-explicit will permit automatic conversions from 'nullptr' into RefPtr types, which I think are not dangerous. The one spot that this affects is in 'UserDataType nsBaseHashtable::Get(KeyType)', which does a 'return 0;' into the UserDataType, which could be a bool, an int, a RefPtr or other. I'm changing that into a C++11 "value initialization", which falls back to "zero initialization" for PODs: 'return UserDataType{};'. Also fixed the comment to clarify not-found return values, as Get(KeyType) was not only used for pointers anyway. MozReview-Commit-ID: F41VlvTNOZU
mfbt/RefPtr.h
xpcom/glue/nsBaseHashtable.h
--- a/mfbt/RefPtr.h
+++ b/mfbt/RefPtr.h
@@ -107,17 +107,17 @@ public:
   MOZ_IMPLICIT RefPtr(T* aRawPtr)
     : mRawPtr(aRawPtr)
   {
     if (mRawPtr) {
       ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
     }
   }
 
-  explicit RefPtr(decltype(nullptr))
+  MOZ_IMPLICIT RefPtr(decltype(nullptr))
     : mRawPtr(nullptr)
   {
   }
 
   template <typename I>
   MOZ_IMPLICIT RefPtr(already_AddRefed<I>& aSmartPtr)
     : mRawPtr(aSmartPtr.take())
     // construct from |already_AddRefed|
--- a/xpcom/glue/nsBaseHashtable.h
+++ b/xpcom/glue/nsBaseHashtable.h
@@ -91,29 +91,30 @@ public:
     if (aData) {
       *aData = ent->mData;
     }
 
     return true;
   }
 
   /**
-   * For pointer types, get the value, returning nullptr if the entry is not
-   * present in the table.
+   * Get the value, returning a zero-initialized POD or a default-initialized
+   * object if the entry is not present in the table.
    *
    * @param aKey the key to retrieve
-   * @return The found value, or nullptr if no entry was found with the given key.
-   * @note If nullptr values are stored in the table, it is not possible to
-   *       distinguish between a nullptr value and a missing entry.
+   * @return The found value, or UserDataType{} if no entry was found with the
+   *         given key.
+   * @note If zero/default-initialized values are stored in the table, it is
+   *       not possible to distinguish between such a value and a missing entry.
    */
   UserDataType Get(KeyType aKey) const
   {
     EntryType* ent = this->GetEntry(aKey);
     if (!ent) {
-      return 0;
+      return UserDataType{};
     }
 
     return ent->mData;
   }
 
   /**
    * put a new value for the associated key
    * @param aKey the key to put