diff --git a/orc-rt/include/orc-rt/Error.h b/orc-rt/include/orc-rt/Error.h index 6b43b3388aff4..48d9064440b6d 100644 --- a/orc-rt/include/orc-rt/Error.h +++ b/orc-rt/include/orc-rt/Error.h @@ -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 class ORC_RT_NODISCARD Expected { template friend class Expected; @@ -310,6 +314,13 @@ template class ORC_RT_NODISCARD Expected { new (getErrorStorage()) error_type(Err.takePayload()); } + template + Expected(OtherT &&Val, ForceExpectedSuccessValue _, + std::enable_if_t> * = nullptr) + : HasError(false), Unchecked(true) { + new (getStorage()) storage_type(std::forward(Val)); + } + /// Create an Expected from a T value. template Expected(OtherT &&Val, diff --git a/orc-rt/unittests/ErrorTest.cpp b/orc-rt/unittests/ErrorTest.cpp index 3fd8279b7dd77..0da55c33961c8 100644 --- a/orc-rt/unittests/ErrorTest.cpp +++ b/orc-rt/unittests/ErrorTest.cpp @@ -386,6 +386,29 @@ TEST(ErrorTest, ExpectedCovariance) { (void)!!A2; } +// Test that Expected works as expected with . +TEST(ErrorTest, ExpectedError) { + { + // Test success-success case. + Expected E(Error::success(), ForceExpectedSuccessValue()); + EXPECT_TRUE(!!E); + cantFail(E.takeError()); + auto Err = std::move(*E); + EXPECT_FALSE(!!Err); + } + + { + // Test "failure" success case. + Expected E(make_error("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());