///
/// cantFail(foo(false));
/// @endcode
-inline void cantFail(Error Err) {
- if (Err)
- llvm_unreachable("Failure value returned from cantFail wrapped call");
+inline void cantFail(Error Err, const char *Msg = nullptr) {
+ if (Err) {
+ if (!Msg)
+ Msg = "Failure value returned from cantFail wrapped call";
+ llvm_unreachable(Msg);
+ }
}
/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
/// int X = cantFail(foo(false));
/// @endcode
template <typename T>
-T cantFail(Expected<T> ValOrErr) {
+T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) {
if (ValOrErr)
return std::move(*ValOrErr);
- else
- llvm_unreachable("Failure value returned from cantFail wrapped call");
+ else {
+ if (!Msg)
+ Msg = "Failure value returned from cantFail wrapped call";
+ llvm_unreachable(Msg);
+ }
}
/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
/// Bar &X = cantFail(foo(false));
/// @endcode
template <typename T>
-T& cantFail(Expected<T&> ValOrErr) {
+T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) {
if (ValOrErr)
return *ValOrErr;
- else
- llvm_unreachable("Failure value returned from cantFail wrapped call");
+ else {
+ if (!Msg)
+ Msg = "Failure value returned from cantFail wrapped call";
+ llvm_unreachable(Msg);
+ }
}
/// Helper for testing applicability of, and applying, handlers for
}
};
-/// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'.
+/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'.
template <typename ErrT>
class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> {
public:
: public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
/// Specialization for member functions of the form
-/// 'RetT (std::unique_ptr<ErrT>) const'.
+/// 'RetT (std::unique_ptr<ErrT>)'.
template <typename C, typename RetT, typename ErrT>
class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)>
: public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
#if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG)
TEST(Error, CantFailDeath) {
EXPECT_DEATH(
- cantFail(make_error<StringError>("foo", inconvertibleErrorCode())),
- "Failure value returned from cantFail wrapped call")
+ cantFail(make_error<StringError>("foo", inconvertibleErrorCode()),
+ "Cantfail call failed"),
+ "Cantfail call failed")
<< "cantFail(Error) did not cause an abort for failure value";
EXPECT_DEATH(