mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:11:12 +01:00
Replaced std::exit() with return Carbon::ErrorOr for expected errors like invalid syntax (#1120)
* Replaced std::exit() with return llvm::Expected/llvm::Error<T> for expected errors like invalid syntax. * Use llvm::formatv() for formatting lexer error messages. x * Addresed merge errors. * Fixed impl scope. * Made ErrorBuilder::operator<< nodiscard, to catch code forgetting 'return' in 'return FATAL_COMPILATION_ERROR()'. * FatalComplationError() -> ParseAndLexContext::RecordLexerError(). Other usages of ERROR_TOKEN in lexer.lpp were actually supposed to be END_OF_FILE. * Update executable_semantics/syntax/parse_and_lex_context.h Co-authored-by: Jon Meow <jperkins@google.com> * Code review fixes. * Update executable_semantics/syntax/parser.ypp Co-authored-by: Jon Meow <jperkins@google.com> * More code review fixes. * Update executable_semantics/interpreter/type_checker.h Co-authored-by: Jon Meow <jperkins@google.com> * Yet more code review fixes... * Update executable_semantics/syntax/lexer.lpp Co-authored-by: Jon Meow <jperkins@google.com> * code review comments * Update executable_semantics/interpreter/interpreter.cpp Co-authored-by: Geoff Romer <gromer@google.com> * Apply suggestions from code review Co-authored-by: Jon Meow <jperkins@google.com> * Update executable_semantics/syntax/lexer.lpp Co-authored-by: Jon Meow <jperkins@google.com> * code review * code review * Apply suggestions from code review Co-authored-by: Jon Meow <jperkins@google.com> * formatted code * review comments * Switched to the new ErrorOr<V> error implementation * code review comments * fixed comment * restored ostream.h as #976 makes the change unnecesary * review comments Co-authored-by: Jon Meow <jperkins@google.com> Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Jon Meow
Geoff Romer
parent
e5a87af6fe
commit
aa8a5f174d
@@ -17,6 +17,9 @@ cc_library(
|
||||
hdrs = ["error.h"],
|
||||
deps = [
|
||||
"//common:check",
|
||||
"//common:error",
|
||||
"//executable_semantics/ast:source_location",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -26,6 +29,7 @@ cc_test(
|
||||
deps = [
|
||||
":error",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -5,38 +5,102 @@
|
||||
#ifndef EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
|
||||
#define EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "common/error.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "llvm/Support/Signals.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// Prints an error and exits. This should be used for non-recoverable errors
|
||||
// with user input.
|
||||
// A helper class for accumulating error message and converting to
|
||||
// `Carbon::Error`/`Carbon::ErrorOr<T>`.
|
||||
class ErrorBuilder {
|
||||
public:
|
||||
explicit ErrorBuilder(std::optional<SourceLocation> loc = std::nullopt)
|
||||
: out_(message_) {
|
||||
if (loc.has_value()) {
|
||||
out_ << *loc << ": ";
|
||||
}
|
||||
}
|
||||
|
||||
// Accumulates string message.
|
||||
template <typename T>
|
||||
[[nodiscard]] auto operator<<(const T& message) -> ErrorBuilder& {
|
||||
out_ << message;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator Error() { return Error(message_); }
|
||||
|
||||
template <typename V>
|
||||
operator ErrorOr<V>() {
|
||||
return Error(message_);
|
||||
}
|
||||
|
||||
std::string message_;
|
||||
llvm::raw_string_ostream out_;
|
||||
};
|
||||
|
||||
// Builds a Carbon::Error instance with the specified message. This should be
|
||||
// used for non-recoverable errors with user input.
|
||||
//
|
||||
// For example:
|
||||
// FATAL_PROGRAM_ERROR(line_num) << "Line is bad!";
|
||||
// FATAL_PROGRAM_ERROR_NO_LINE() << "Application is bad!";
|
||||
// return FATAL_PROGRAM_ERROR(line_num) << "Line is bad!";
|
||||
// return FATAL_PROGRAM_ERROR_NO_LINE() << "Application is bad!";
|
||||
//
|
||||
// Where possible, try to identify the error as a compilation or
|
||||
// runtime error. Use CHECK/FATAL for internal errors. The generic program error
|
||||
// option is provided as a fallback for cases that don't fit those
|
||||
// runtime error. Use CHECK/FATAL for internal errors. The generic program
|
||||
// error option is provided as a fallback for cases that don't fit those
|
||||
// classifications.
|
||||
//
|
||||
// TODO: replace below macro invocations with direct `return ErrorBuilder() <<
|
||||
// xx` calls.
|
||||
|
||||
#define FATAL_PROGRAM_ERROR_NO_LINE() RAW_EXITING_STREAM() << "PROGRAM ERROR: "
|
||||
#define FATAL_PROGRAM_ERROR_NO_LINE() \
|
||||
Carbon::ErrorBuilder() << "PROGRAM ERROR: "
|
||||
|
||||
#define FATAL_PROGRAM_ERROR(line) \
|
||||
FATAL_PROGRAM_ERROR_NO_LINE() << (line) << ": "
|
||||
|
||||
#define FATAL_COMPILATION_ERROR_NO_LINE() \
|
||||
RAW_EXITING_STREAM() << "COMPILATION ERROR: "
|
||||
Carbon::ErrorBuilder() << "COMPILATION ERROR: "
|
||||
|
||||
#define FATAL_COMPILATION_ERROR(line) \
|
||||
FATAL_COMPILATION_ERROR_NO_LINE() << (line) << ": "
|
||||
|
||||
#define FATAL_RUNTIME_ERROR_NO_LINE() RAW_EXITING_STREAM() << "RUNTIME ERROR: "
|
||||
#define FATAL_RUNTIME_ERROR_NO_LINE() \
|
||||
Carbon::ErrorBuilder() << "RUNTIME ERROR: "
|
||||
|
||||
#define FATAL_RUNTIME_ERROR(line) \
|
||||
FATAL_RUNTIME_ERROR_NO_LINE() << (line) << ": "
|
||||
|
||||
// Macro hackery to get a unique variable name.
|
||||
#define MAKE_UNIQUE_NAME_IMPL(a, b, c) a##b##c
|
||||
#define MAKE_UNIQUE_NAME(a, b, c) MAKE_UNIQUE_NAME_IMPL(a, b, c)
|
||||
|
||||
#define RETURN_IF_ERROR_IMPL(unique_name, expr) \
|
||||
if (auto unique_name = (expr); !unique_name.ok()) { \
|
||||
return std::move(unique_name).error(); \
|
||||
}
|
||||
|
||||
#define RETURN_IF_ERROR(expr) \
|
||||
RETURN_IF_ERROR_IMPL( \
|
||||
MAKE_UNIQUE_NAME(_llvm_error_line, __LINE__, __COUNTER__), expr)
|
||||
|
||||
#define ASSIGN_OR_RETURN_IMPL(unique_name, var, expr) \
|
||||
auto unique_name = (expr); \
|
||||
if (!unique_name.ok()) { \
|
||||
return std::move(unique_name).error(); \
|
||||
} \
|
||||
var = std::move(*unique_name);
|
||||
|
||||
#define ASSIGN_OR_RETURN(var, expr) \
|
||||
ASSIGN_OR_RETURN_IMPL( \
|
||||
MAKE_UNIQUE_NAME(_llvm_expected_line, __LINE__, __COUNTER__), var, expr)
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
|
||||
|
||||
@@ -9,24 +9,94 @@
|
||||
namespace Carbon::Testing {
|
||||
namespace {
|
||||
|
||||
using ::testing::Eq;
|
||||
|
||||
auto MakeSuccess() -> ErrorOr<Success> { return Success(); }
|
||||
|
||||
auto MakeError(std::string_view message) -> ErrorOr<Success> {
|
||||
return Error(message);
|
||||
}
|
||||
|
||||
auto MakeInt(int value) -> ErrorOr<int> { return value; }
|
||||
|
||||
auto MakeFailedInt(std::string_view message) -> ErrorOr<int> {
|
||||
return Error(message);
|
||||
}
|
||||
|
||||
auto ErrorToString(const Error& e) -> std::string { return e.message(); }
|
||||
|
||||
template <typename V>
|
||||
auto ErrorToString(const ErrorOr<V>& e) -> std::string {
|
||||
return e.error().message();
|
||||
}
|
||||
|
||||
TEST(ErrorTest, FatalProgramError) {
|
||||
ASSERT_DEATH({ FATAL_PROGRAM_ERROR_NO_LINE() << "test"; },
|
||||
"^PROGRAM ERROR: test\n");
|
||||
EXPECT_EQ(ErrorToString(FATAL_PROGRAM_ERROR_NO_LINE() << "test"),
|
||||
"PROGRAM ERROR: test");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, FatalRuntimeError) {
|
||||
ASSERT_DEATH({ FATAL_RUNTIME_ERROR_NO_LINE() << "test"; },
|
||||
"^RUNTIME ERROR: test\n");
|
||||
EXPECT_EQ(ErrorToString(FATAL_RUNTIME_ERROR_NO_LINE() << "test"),
|
||||
"RUNTIME ERROR: test");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, FatalCompilationError) {
|
||||
ASSERT_DEATH({ FATAL_COMPILATION_ERROR_NO_LINE() << "test"; },
|
||||
"^COMPILATION ERROR: test\n");
|
||||
EXPECT_EQ(ErrorToString(FATAL_COMPILATION_ERROR_NO_LINE() << "test"),
|
||||
"COMPILATION ERROR: test");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, FatalProgramErrorLine) {
|
||||
ASSERT_DEATH({ FATAL_PROGRAM_ERROR(1) << "test"; },
|
||||
"^PROGRAM ERROR: 1: test\n");
|
||||
EXPECT_EQ(ErrorToString(FATAL_PROGRAM_ERROR(1) << "test"),
|
||||
"PROGRAM ERROR: 1: test");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, ReturnIfErrorNoError) {
|
||||
auto result = []() -> ErrorOr<Success> {
|
||||
RETURN_IF_ERROR(MakeSuccess());
|
||||
RETURN_IF_ERROR(MakeSuccess());
|
||||
return Success();
|
||||
}();
|
||||
EXPECT_TRUE(result.ok());
|
||||
}
|
||||
|
||||
TEST(ErrorTest, ReturnIfErrorHasError) {
|
||||
auto result = []() -> ErrorOr<Success> {
|
||||
RETURN_IF_ERROR(MakeSuccess());
|
||||
RETURN_IF_ERROR(MakeError("error"));
|
||||
return Success();
|
||||
}();
|
||||
ASSERT_FALSE(result.ok());
|
||||
EXPECT_EQ(ErrorToString(result), "error");
|
||||
}
|
||||
|
||||
TEST(ErrorTest, AssignOrReturnNoError) {
|
||||
auto result = []() -> ErrorOr<int> {
|
||||
RETURN_IF_ERROR(MakeSuccess());
|
||||
ASSIGN_OR_RETURN(int a, MakeInt(1));
|
||||
ASSIGN_OR_RETURN(const int b, MakeInt(2));
|
||||
int c = 0;
|
||||
ASSIGN_OR_RETURN(c, MakeInt(3));
|
||||
return a + b + c;
|
||||
}();
|
||||
ASSERT_TRUE(result.ok());
|
||||
EXPECT_EQ(6, *result);
|
||||
}
|
||||
|
||||
TEST(ErrorTest, AssignOrReturnHasDirectError) {
|
||||
auto result = []() -> ErrorOr<int> {
|
||||
RETURN_IF_ERROR(MakeError("error"));
|
||||
return 0;
|
||||
}();
|
||||
ASSERT_FALSE(result.ok());
|
||||
}
|
||||
|
||||
TEST(ErrorTest, AssignOrReturnHasErrorInExpected) {
|
||||
auto result = []() -> ErrorOr<int> {
|
||||
ASSIGN_OR_RETURN(int a, MakeFailedInt("error"));
|
||||
return a;
|
||||
}();
|
||||
ASSERT_FALSE(result.ok());
|
||||
EXPECT_EQ(ErrorToString(result), "error");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user