tint/resolver: Consistently use utils::Result in ConstEval

Instead of using builder.Diagnostics().contains_errors()

Produces cleaner code and reduces scope of error handling.

Bug: tint:1661
Change-Id: I35af5ad1c6553f2cf74d1ce92dc14984f93b9db4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/102161
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton
2022-09-20 09:26:21 +00:00
committed by Dawn LUCI CQ
parent fc6167b9a8
commit e68d4506c0
4 changed files with 337 additions and 367 deletions

View File

@@ -17,6 +17,7 @@
#include <ostream>
#include <variant>
#include "src/tint/debug.h"
namespace tint::utils {
@@ -50,6 +51,20 @@ struct [[nodiscard]] Result {
Result(const FAILURE_TYPE& failure) // NOLINT(runtime/explicit):
: value{failure} {}
/// Copy constructor with success / failure casting
/// @param other the Result to copy
template <typename S,
typename F,
typename = std::void_t<decltype(SUCCESS_TYPE{std::declval<S>()}),
decltype(FAILURE_TYPE{std::declval<F>()})>>
Result(const Result<S, F>& other) { // NOLINT(runtime/explicit):
if (other) {
value = SUCCESS_TYPE{other.Get()};
} else {
value = FAILURE_TYPE{other.Failure()};
}
}
/// @returns true if the result was a success
operator bool() const {
Validate();

View File

@@ -51,5 +51,17 @@ TEST(ResultTest, CustomFailure) {
EXPECT_EQ(r.Failure(), "oh noes!");
}
TEST(ResultTest, ValueCast) {
struct X {};
struct Y : X {};
Y* y = nullptr;
auto r_y = Result<Y*>{y};
auto r_x = Result<X*>{r_y};
(void)r_x;
(void)r_y;
}
} // namespace
} // namespace tint::utils