mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
### Context & Motivation The error handling utilities in `//base/error.h` are very useful for writing code with strong safety guarantees. While hardening the `Dump` debug utilities (from review in #4866), I encountered a rough edge with references and pointers. After a [brief Discord discussion in #contributing-help](https://discord.com/channels/655572317891461132/1052653651895779359/1334675462877610038), it was suggested that adding support for references to `ErrorOr` would be a good candidate to move forward. Using a reference type with the `ErrorOr` class (e.g. `ErrorOr<Node&>`) produces two errors: <ol> <li><strong><code>variant can not have a reference type as an alternative</code></strong> <ul><li>From private field: <code>std::variant<Error, T> val_;</code></li></ul> </li> <li><strong><code>'operator->' declared as a pointer to a reference</code></strong> <ul><li>From member function: <code>auto operator->() -> T*</code></li></ul> </li> </ol> ### Changes To support reference types, both errors are resolved: 1. `std::reference_wrapper` is conditionally used for storage when `T` is a reference type 2. type trait aliases like `using ValueT = std::remove_reference_t<T>` are used to produce compatible types for methods like `auto operator->() -> ValueT*`
124 lines
3.1 KiB
C++
124 lines
3.1 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "common/error.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "common/error_test_helpers.h"
|
|
#include "common/raw_string_ostream.h"
|
|
|
|
namespace Carbon {
|
|
namespace {
|
|
|
|
using ::Carbon::Testing::IsError;
|
|
using ::Carbon::Testing::IsSuccess;
|
|
using ::testing::Eq;
|
|
|
|
TEST(ErrorTest, Error) {
|
|
Error err("test");
|
|
EXPECT_EQ(err.message(), "test");
|
|
}
|
|
|
|
TEST(ErrorTest, ErrorEmptyString) {
|
|
ASSERT_DEATH({ Error err(""); }, "CHECK failure at");
|
|
}
|
|
|
|
auto IndirectError() -> Error { return Error("test"); }
|
|
|
|
TEST(ErrorTest, IndirectError) { EXPECT_EQ(IndirectError().message(), "test"); }
|
|
|
|
TEST(ErrorTest, ErrorOr) {
|
|
ErrorOr<int> err(Error("test"));
|
|
|
|
EXPECT_THAT(err, IsError("test"));
|
|
}
|
|
|
|
TEST(ErrorTest, ErrorOrValue) { EXPECT_TRUE(ErrorOr<int>(0).ok()); }
|
|
|
|
auto IndirectErrorOrTest() -> ErrorOr<int> { return Error("test"); }
|
|
|
|
TEST(ErrorTest, IndirectErrorOr) { EXPECT_FALSE(IndirectErrorOrTest().ok()); }
|
|
|
|
struct Val {
|
|
int val;
|
|
};
|
|
|
|
TEST(ErrorTest, ErrorOrArrowOp) {
|
|
ErrorOr<Val> err({1});
|
|
EXPECT_EQ(err->val, 1);
|
|
}
|
|
|
|
TEST(ErrorTest, ErrorOrReference) {
|
|
Val val = {1};
|
|
ErrorOr<Val&> maybe_val(val);
|
|
EXPECT_EQ(maybe_val->val, 1);
|
|
}
|
|
|
|
auto IndirectErrorOrSuccessTest() -> ErrorOr<Success> { return Success(); }
|
|
|
|
TEST(ErrorTest, IndirectErrorOrSuccess) {
|
|
EXPECT_TRUE(IndirectErrorOrSuccessTest().ok());
|
|
}
|
|
|
|
TEST(ErrorTest, ReturnIfErrorNoError) {
|
|
auto result = []() -> ErrorOr<Success> {
|
|
CARBON_RETURN_IF_ERROR(ErrorOr<Success>(Success()));
|
|
CARBON_RETURN_IF_ERROR(ErrorOr<Success>(Success()));
|
|
return Success();
|
|
}();
|
|
EXPECT_TRUE(result.ok());
|
|
}
|
|
|
|
TEST(ErrorTest, ReturnIfErrorHasError) {
|
|
auto result = []() -> ErrorOr<Success> {
|
|
CARBON_RETURN_IF_ERROR(ErrorOr<Success>(Success()));
|
|
CARBON_RETURN_IF_ERROR(ErrorOr<Success>(Error("error")));
|
|
return Success();
|
|
}();
|
|
EXPECT_THAT(result, IsError("error"));
|
|
}
|
|
|
|
TEST(ErrorTest, AssignOrReturnNoError) {
|
|
auto result = []() -> ErrorOr<int> {
|
|
CARBON_ASSIGN_OR_RETURN(int a, ErrorOr<int>(1));
|
|
CARBON_ASSIGN_OR_RETURN(const int b, ErrorOr<int>(2));
|
|
int c = 0;
|
|
CARBON_ASSIGN_OR_RETURN(c, ErrorOr<int>(3));
|
|
return a + b + c;
|
|
}();
|
|
EXPECT_THAT(result, IsSuccess(Eq(6)));
|
|
}
|
|
|
|
TEST(ErrorTest, AssignOrReturnHasDirectError) {
|
|
auto result = []() -> ErrorOr<int> {
|
|
CARBON_RETURN_IF_ERROR(ErrorOr<int>(Error("error")));
|
|
return 0;
|
|
}();
|
|
EXPECT_THAT(result, IsError("error"));
|
|
}
|
|
|
|
TEST(ErrorTest, AssignOrReturnHasErrorInExpected) {
|
|
auto result = []() -> ErrorOr<int> {
|
|
CARBON_ASSIGN_OR_RETURN(int a, ErrorOr<int>(Error("error")));
|
|
return a;
|
|
}();
|
|
EXPECT_THAT(result, IsError("error"));
|
|
}
|
|
|
|
TEST(ErrorTest, ErrorBuilderOperatorImplicitCast) {
|
|
ErrorOr<int> result = ErrorBuilder() << "msg";
|
|
EXPECT_THAT(result, IsError("msg"));
|
|
}
|
|
|
|
TEST(ErrorTest, StreamError) {
|
|
Error result = ErrorBuilder("TestFunc") << "msg";
|
|
RawStringOstream result_stream;
|
|
result_stream << result;
|
|
EXPECT_EQ(result_stream.TakeStr(), "TestFunc: msg");
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon
|