[PATCH] D102631: Allow incomplete template types in unique_function arguments

Fehr Mathieu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 18 11:32:33 PDT 2021


math-fehr updated this revision to Diff 346228.
math-fehr added a comment.

Update tests and comments


Repository:
  rG LLVM Github Monorepo

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

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,18 @@
   EXPECT_EQ("string", returns([] { return "hello"; }));
 }
 
+// A forward declared type, and a templated type.
+class ForwardDeclared;
+template <typename T> class Vec { T a; };
+
+// Check that we can define unique_function that have references to
+// incomplete types, even if those types are templated over an
+// incomplete type.
+TEST(UniqueFunctionTest, IncompleteTypes) {
+  unique_function<void(Vec<ForwardDeclared> &, Vec<ForwardDeclared> *)>
+      IncompleteArguments;
+  unique_function<Vec<ForwardDeclared> &()> IncompleteResultReference;
+  unique_function<Vec<ForwardDeclared> *()> 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<V<T>&>' does not trigger a
+  // compile-time error when 'T' is an incomplete type and V a templated 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.346228.patch
Type: text/x-patch
Size: 3393 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210518/a2873225/attachment.bin>


More information about the llvm-commits mailing list