[PATCH] D102631: Add SFINAE in unique_function to reference incomplete types in arguments

Fehr Mathieu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 17 07:42:28 PDT 2021


math-fehr created this revision.
Herald added a subscriber: dexonsmith.
math-fehr requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We can't declare unique_function that has a reference to an incomplete
type in its arguments. This comes from AdjustedParamT, which call
IsSizeLessThanThresholdT, which fails at compile-time when its
template argument is a reference to an incomplete type, due to
sizeof(T&) requiring T to be complete.

This patch uses SFINAE in AdjustedParamT to remove the compile-time
error. It also add a compile-time test to check that this now works.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102631

Files:
  llvm/include/llvm/ADT/FunctionExtras.h
  llvm/unittests/ADT/FunctionExtrasTest.cpp


Index: llvm/unittests/ADT/FunctionExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/FunctionExtrasTest.cpp
+++ llvm/unittests/ADT/FunctionExtrasTest.cpp
@@ -273,4 +273,11 @@
   EXPECT_EQ("string", returns([] { return "hello"; }));
 }
 
+TEST(UniqueFunctionTest, IncompleteTypes) {
+  class A;
+  unique_function<void(A &, A *)> IncompleteArguments;
+  unique_function<A &()> IncompleteResultReference;
+  unique_function<A *()> IncompleteResultPointer;
+}
+
 } // anonymous namespace
Index: llvm/include/llvm/ADT/FunctionExtras.h
===================================================================
--- llvm/include/llvm/ADT/FunctionExtras.h
+++ llvm/include/llvm/ADT/FunctionExtras.h
@@ -89,18 +89,22 @@
   // The heuristic used is related to common ABI register passing conventions.
   // It doesn't have to be exact though, and in one way it is more strict
   // because we want to still be able to observe either moves *or* copies.
-  template <typename T>
-  using AdjustedParamT = typename std::conditional<
-      !std::is_reference<T>::value &&
-          llvm::is_trivially_copy_constructible<T>::value &&
-          llvm::is_trivially_move_constructible<T>::value &&
-          IsSizeLessThanThresholdT<T>::value,
-      T, T &>::type;
+  template <typename T> struct AdjustedParamT {
+    using type = typename std::conditional<
+        llvm::is_trivially_copy_constructible<T>::value &&
+            llvm::is_trivially_move_constructible<T>::value &&
+            IsSizeLessThanThresholdT<T>::value,
+        T, T &>::type;
+  };
+
+  // This specialization ensures that 'AdjustedParam<T&>' does not trigger a
+  // compile-time error when 'T' is an incomplete type.
+  template <typename T> struct AdjustedParamT<T &> { using type = T &; };
 
   // The type of the erased function pointer we use as a callback to dispatch to
   // the stored callable when it is trivial to move and destroy.
-  using CallPtrT = ReturnT (*)(void *CallableAddr,
-                               AdjustedParamT<ParamTs>... Params);
+  using CallPtrT = ReturnT (*)(
+      void *CallableAddr, typename AdjustedParamT<ParamTs>::type... Params);
   using MovePtrT = void (*)(void *LHSCallableAddr, void *RHSCallableAddr);
   using DestroyPtrT = void (*)(void *CallableAddr);
 
@@ -159,7 +163,9 @@
   }
 
   CallPtrT getTrivialCallback() const {
-    return CallbackAndInlineFlag.getPointer().template get<TrivialCallback *>()->CallPtr;
+    return CallbackAndInlineFlag.getPointer()
+        .template get<TrivialCallback *>()
+        ->CallPtr;
   }
 
   NonTrivialCallbacks *getNonTrivialCallbacks() const {
@@ -199,7 +205,7 @@
 
   template <typename CalledAsT>
   static ReturnT CallImpl(void *CallableAddr,
-                          AdjustedParamT<ParamTs>... Params) {
+                          typename AdjustedParamT<ParamTs>::type... Params) {
     auto &Func = *reinterpret_cast<CalledAsT *>(CallableAddr);
     return Func(std::forward<ParamTs>(Params)...);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102631.345879.patch
Type: text/x-patch
Size: 3022 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210517/5396d63b/attachment.bin>


More information about the llvm-commits mailing list