Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions orc-rt/include/orc-rt/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ class ErrorAsOutParameter {
Error *Err;
};

/// Tag to force construction of an Expected value in the success state. See
/// Expected constructor for details.
struct ForceExpectedSuccessValue {};

template <typename T> class ORC_RT_NODISCARD Expected {

template <class OtherT> friend class Expected;
Expand All @@ -310,6 +314,13 @@ template <typename T> class ORC_RT_NODISCARD Expected {
new (getErrorStorage()) error_type(Err.takePayload());
}

template <typename OtherT>
Expected(OtherT &&Val, ForceExpectedSuccessValue _,
std::enable_if_t<std::is_convertible_v<OtherT, T>> * = nullptr)
: HasError(false), Unchecked(true) {
new (getStorage()) storage_type(std::forward<OtherT>(Val));
}

/// Create an Expected from a T value.
template <typename OtherT>
Expected(OtherT &&Val,
Expand Down
23 changes: 23 additions & 0 deletions orc-rt/unittests/ErrorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,29 @@ TEST(ErrorTest, ExpectedCovariance) {
(void)!!A2;
}

// Test that Expected<Error> works as expected with .
TEST(ErrorTest, ExpectedError) {
{
// Test success-success case.
Expected<Error> E(Error::success(), ForceExpectedSuccessValue());
EXPECT_TRUE(!!E);
cantFail(E.takeError());
auto Err = std::move(*E);
EXPECT_FALSE(!!Err);
}

{
// Test "failure" success case.
Expected<Error> E(make_error<StringError>("foo"),
ForceExpectedSuccessValue());
EXPECT_TRUE(!!E);
cantFail(E.takeError());
auto Err = std::move(*E);
EXPECT_TRUE(!!Err);
EXPECT_EQ(toString(std::move(Err)), "foo");
}
}

// Test that the ExitOnError utility works as expected.
TEST(ErrorTest, CantFailSuccess) {
cantFail(Error::success());
Expand Down
Loading