mirror of
https://github.com/intel/llvm.git
synced 2026-01-13 02:38:07 +08:00
[orc-rt] Add support for constructing Expected<Error> values. (#161656)
These will be used in upcoming RPC support patches where the outer Expected value captures any RPC-infrastructure errors, and the inner Error is returned from the romet call (i.e. the remote handler's return type is Error).
This commit is contained in:
@@ -288,6 +288,10 @@ private:
|
||||
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;
|
||||
@@ -310,6 +314,13 @@ public:
|
||||
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,
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user