[ADT] Extend EnableIfCallable for callables with incomplete returns
authorFehr Mathieu <mathieu.fehr@gmail.com>
Mon, 13 Sep 2021 18:15:13 +0000 (18:15 +0000)
committerDaniil Suchkov <dsuchkov@azul.com>
Mon, 13 Sep 2021 19:16:16 +0000 (19:16 +0000)
std::is_convertible has no defined behavior when its arguments
are incomplete, even if they are equal. In practice, it returns false.
Adding std::is_same allows us to use the constructor using a callable,
even if the return value is incomplete. We also check the case where
we convert a T into a const T.

Reviewed By: DaniilSuchkov

Differential Revision: https://reviews.llvm.org/D104703

Committer: Daniil Suchkov <dsuchkov@azul.com>

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

index e67ef73..43e98de 100644 (file)
@@ -64,11 +64,16 @@ template <typename CallableT, typename ThisT>
 using EnableUnlessSameType =
     std::enable_if_t<!std::is_same<remove_cvref_t<CallableT>, ThisT>::value>;
 template <typename CallableT, typename Ret, typename... Params>
-using EnableIfCallable =
-    std::enable_if_t<std::is_void<Ret>::value ||
-                     std::is_convertible<decltype(std::declval<CallableT>()(
-                                             std::declval<Params>()...)),
-                                         Ret>::value>;
+using EnableIfCallable = std::enable_if_t<llvm::disjunction<
+    std::is_void<Ret>,
+    std::is_same<decltype(std::declval<CallableT>()(std::declval<Params>()...)),
+                 Ret>,
+    std::is_same<const decltype(std::declval<CallableT>()(
+                     std::declval<Params>()...)),
+                 Ret>,
+    std::is_convertible<decltype(std::declval<CallableT>()(
+                            std::declval<Params>()...)),
+                        Ret>>::value>;
 
 template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
 protected:
index 3c6f179..fc856a9 100644 (file)
@@ -291,4 +291,23 @@ TEST(UniqueFunctionTest, IncompleteTypes) {
   unique_function<Templated<Incomplete> *()> IncompleteResultPointer;
 }
 
+// Incomplete function returning an incomplete type
+Incomplete incompleteFunction();
+const Incomplete incompleteFunctionConst();
+
+// Check that we can assign a callable to a unique_function when the
+// callable return value is incomplete.
+TEST(UniqueFunctionTest, IncompleteCallableType) {
+  unique_function<Incomplete()> IncompleteReturnInCallable{incompleteFunction};
+  unique_function<const Incomplete()> IncompleteReturnInCallableConst{
+      incompleteFunctionConst};
+  unique_function<const Incomplete()> IncompleteReturnInCallableConstConversion{
+      incompleteFunction};
+}
+
+// Define the incomplete function
+class Incomplete {};
+Incomplete incompleteFunction() { return {}; }
+const Incomplete incompleteFunctionConst() { return {}; }
+
 } // anonymous namespace