[PATCH] D95498: ADT: Add SFINAE to the generic IntrusiveRefCntPtr constructors

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 28 15:09:40 PST 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG17c584551d57: ADT: Add SFINAE to the generic IntrusiveRefCntPtr constructors (authored by dexonsmith).

Changed prior to commit:
  https://reviews.llvm.org/D95498?vs=319452&id=319972#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95498/new/

https://reviews.llvm.org/D95498

Files:
  llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
  llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp


Index: llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
===================================================================
--- llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
+++ llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
@@ -96,4 +96,50 @@
   EXPECT_TRUE(Retained);
 }
 
+// Test that the generic constructors use SFINAE to disable invalid
+// conversions.
+struct X : RefCountedBase<X> {};
+struct Y : X {};
+struct Z : RefCountedBase<Z> {};
+static_assert(!std::is_convertible<IntrusiveRefCntPtr<X> &&,
+                                   IntrusiveRefCntPtr<Y>>::value,
+              "X&& -> Y should be rejected with SFINAE");
+static_assert(!std::is_convertible<const IntrusiveRefCntPtr<X> &,
+                                   IntrusiveRefCntPtr<Y>>::value,
+              "const X& -> Y should be rejected with SFINAE");
+static_assert(
+    !std::is_convertible<std::unique_ptr<X>, IntrusiveRefCntPtr<Y>>::value,
+    "X -> Y should be rejected with SFINAE");
+static_assert(!std::is_convertible<IntrusiveRefCntPtr<X> &&,
+                                   IntrusiveRefCntPtr<Z>>::value,
+              "X&& -> Z should be rejected with SFINAE");
+static_assert(!std::is_convertible<const IntrusiveRefCntPtr<X> &,
+                                   IntrusiveRefCntPtr<Z>>::value,
+              "cosnt X& -> Z should be rejected with SFINAE");
+static_assert(
+    !std::is_convertible<std::unique_ptr<X>, IntrusiveRefCntPtr<Z>>::value,
+    "X -> Z should be rejected with SFINAE");
+
+TEST(IntrusiveRefCntPtr, InteropsWithConvertible) {
+  // Check converting constructors and operator=.
+  auto Y1 = makeIntrusiveRefCnt<Y>();
+  auto Y2 = makeIntrusiveRefCnt<Y>();
+  auto Y3 = makeIntrusiveRefCnt<Y>();
+  auto Y4 = makeIntrusiveRefCnt<Y>();
+  const void *P1 = Y1.get();
+  const void *P2 = Y2.get();
+  const void *P3 = Y3.get();
+  const void *P4 = Y4.get();
+  IntrusiveRefCntPtr<X> X1 = std::move(Y1);
+  IntrusiveRefCntPtr<X> X2 = Y2;
+  IntrusiveRefCntPtr<X> X3;
+  IntrusiveRefCntPtr<X> X4;
+  X3 = std::move(Y3);
+  X4 = Y4;
+  EXPECT_EQ(P1, X1.get());
+  EXPECT_EQ(P2, X2.get());
+  EXPECT_EQ(P3, X3.get());
+  EXPECT_EQ(P4, X4.get());
+}
+
 } // end namespace llvm
Index: llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
===================================================================
--- llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -171,21 +171,18 @@
   IntrusiveRefCntPtr(const IntrusiveRefCntPtr &S) : Obj(S.Obj) { retain(); }
   IntrusiveRefCntPtr(IntrusiveRefCntPtr &&S) : Obj(S.Obj) { S.Obj = nullptr; }
 
-  template <class X>
-  IntrusiveRefCntPtr(IntrusiveRefCntPtr<X> &&S) : Obj(S.get()) {
+  template <class X,
+            std::enable_if_t<std::is_convertible<X *, T *>::value, bool> = true>
+  IntrusiveRefCntPtr(IntrusiveRefCntPtr<X> S) : Obj(S.get()) {
     S.Obj = nullptr;
   }
 
-  template <class X>
+  template <class X,
+            std::enable_if_t<std::is_convertible<X *, T *>::value, bool> = true>
   IntrusiveRefCntPtr(std::unique_ptr<X> S) : Obj(S.release()) {
     retain();
   }
 
-  template <class X>
-  IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X> &S) : Obj(S.get()) {
-    retain();
-  }
-
   ~IntrusiveRefCntPtr() { release(); }
 
   IntrusiveRefCntPtr &operator=(IntrusiveRefCntPtr S) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95498.319972.patch
Type: text/x-patch
Size: 3297 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210128/93c561c0/attachment.bin>


More information about the llvm-commits mailing list