[PATCH] D88901: [ADT] function_ref's constructor is unavailable if the argument is not callable.
Sam McCall via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 7 07:30:13 PDT 2020
sammccall updated this revision to Diff 296670.
sammccall added a comment.
Herald added a subscriber: hiraditya.
Combine SFINAE conditions into one top-level condition.
Fix a couple of broken usages that no longer compile (in default-args that were
never used, so template instantiation didn't catch them).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D88901/new/
https://reviews.llvm.org/D88901
Files:
llvm/include/llvm/ADT/STLExtras.h
llvm/lib/AsmParser/LLParser.h
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/unittests/ADT/FunctionRefTest.cpp
Index: llvm/unittests/ADT/FunctionRefTest.cpp
===================================================================
--- llvm/unittests/ADT/FunctionRefTest.cpp
+++ llvm/unittests/ADT/FunctionRefTest.cpp
@@ -48,4 +48,15 @@
ASSERT_EQ(1, X());
}
+// Test that overloads on function_refs are resolved as expected.
+const char *returns(StringRef) { return "not a function"; }
+const char *returns(function_ref<double()> F) { return "number"; }
+const char *returns(function_ref<StringRef()> F) { return "string"; }
+
+TEST(FunctionRefTest, SFINAE) {
+ EXPECT_EQ("not a function", returns("boo!"));
+ EXPECT_EQ("number", returns([] { return 42; }));
+ EXPECT_EQ("string", returns([] { return "hello"; }));
+}
+
} // namespace
Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp
===================================================================
--- llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -579,7 +579,7 @@
/// \returns true if an error occurred.
Error parseBitcodeInto(
Module *M, bool ShouldLazyLoadMetadata = false, bool IsImporting = false,
- DataLayoutCallbackTy DataLayoutCallback = [](std::string) {
+ DataLayoutCallbackTy DataLayoutCallback = [](StringRef) {
return None;
});
Index: llvm/lib/AsmParser/LLParser.h
===================================================================
--- llvm/lib/AsmParser/LLParser.h
+++ llvm/lib/AsmParser/LLParser.h
@@ -166,8 +166,8 @@
: Context(Context), Lex(F, SM, Err, Context), M(M), Index(Index),
Slots(Slots), BlockAddressPFS(nullptr) {}
bool Run(
- bool UpgradeDebugInfo,
- DataLayoutCallbackTy DataLayoutCallback = [](Module *) {});
+ bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback =
+ [](StringRef) { return None; });
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -186,16 +186,27 @@
std::forward<Params>(params)...);
}
+ template <typename Callable,
+ typename Result =
+ typename std::result_of<Callable(Params...)>::type>
+ static constexpr bool IsCompatible =
+ std::is_void<Ret>::value || std::is_convertible<Result, Ret>::value;
+
public:
function_ref() = default;
function_ref(std::nullptr_t) {}
template <typename Callable>
+ // Only allow this constructor if the object is actually callable
+ // and returns the correct type.
function_ref(
Callable &&callable,
std::enable_if_t<
+ // This is not the copy-constructor.
!std::is_same<std::remove_cv_t<std::remove_reference_t<Callable>>,
- function_ref>::value> * = nullptr)
+ function_ref>::value &&
+ // Must be callable and return a suitable type.
+ IsCompatible<Callable>> * = nullptr)
: callback(callback_fn<typename std::remove_reference<Callable>::type>),
callable(reinterpret_cast<intptr_t>(&callable)) {}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88901.296670.patch
Type: text/x-patch
Size: 3211 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201007/06b8cb22/attachment.bin>
More information about the llvm-commits
mailing list