mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-27 18:30:11 +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
@@ -7,6 +7,7 @@
|
||||
#include "executable_semantics/interpreter/action.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
#include "llvm/Support/Error.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -49,7 +50,7 @@ void ActionStack::Initialize(ValueNodeView value_node,
|
||||
|
||||
auto ActionStack::ValueOfNode(ValueNodeView value_node,
|
||||
SourceLocation source_loc) const
|
||||
-> Nonnull<const Value*> {
|
||||
-> ErrorOr<Nonnull<const Value*>> {
|
||||
if (std::optional<Nonnull<const Value*>> constant_value =
|
||||
value_node.constant_value();
|
||||
constant_value.has_value()) {
|
||||
@@ -74,9 +75,9 @@ auto ActionStack::ValueOfNode(ValueNodeView value_node,
|
||||
return *result;
|
||||
}
|
||||
}
|
||||
// TODO: Move these errors to name resolution and explain them more clearly.
|
||||
FATAL_RUNTIME_ERROR(source_loc)
|
||||
<< "could not find `" << value_node.base() << "`";
|
||||
// TODO: Move these errors to compile time and explain them more clearly.
|
||||
return FATAL_RUNTIME_ERROR(source_loc)
|
||||
<< "could not find `" << value_node.base() << "`";
|
||||
}
|
||||
|
||||
void ActionStack::MergeScope(RuntimeScope scope) {
|
||||
@@ -110,7 +111,7 @@ void ActionStack::InitializeFragment(ContinuationValue::StackFragment& fragment,
|
||||
fragment.StoreReversed(std::move(reversed_todo));
|
||||
}
|
||||
|
||||
void ActionStack::FinishAction() {
|
||||
auto ActionStack::FinishAction() -> ErrorOr<Success> {
|
||||
std::unique_ptr<Action> act = todo_.Pop();
|
||||
switch (act->kind()) {
|
||||
case Action::Kind::ExpressionAction:
|
||||
@@ -123,9 +124,11 @@ void ActionStack::FinishAction() {
|
||||
case Action::Kind::DeclarationAction:
|
||||
PopScopes();
|
||||
}
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::FinishAction(Nonnull<const Value*> result) {
|
||||
auto ActionStack::FinishAction(Nonnull<const Value*> result)
|
||||
-> ErrorOr<Success> {
|
||||
std::unique_ptr<Action> act = todo_.Pop();
|
||||
switch (act->kind()) {
|
||||
case Action::Kind::StatementAction:
|
||||
@@ -139,27 +142,33 @@ void ActionStack::FinishAction(Nonnull<const Value*> result) {
|
||||
PopScopes();
|
||||
SetResult(result);
|
||||
}
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::Spawn(std::unique_ptr<Action> child) {
|
||||
auto ActionStack::Spawn(std::unique_ptr<Action> child) -> ErrorOr<Success> {
|
||||
Action& action = *todo_.Top();
|
||||
action.set_pos(action.pos() + 1);
|
||||
todo_.Push(std::move(child));
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::Spawn(std::unique_ptr<Action> child, RuntimeScope scope) {
|
||||
auto ActionStack::Spawn(std::unique_ptr<Action> child, RuntimeScope scope)
|
||||
-> ErrorOr<Success> {
|
||||
Action& action = *todo_.Top();
|
||||
action.set_pos(action.pos() + 1);
|
||||
todo_.Push(std::make_unique<ScopeAction>(std::move(scope)));
|
||||
todo_.Push(std::move(child));
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::RunAgain() {
|
||||
auto ActionStack::RunAgain() -> ErrorOr<Success> {
|
||||
Action& action = *todo_.Top();
|
||||
action.set_pos(action.pos() + 1);
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::UnwindTo(Nonnull<const Statement*> ast_node) {
|
||||
auto ActionStack::UnwindTo(Nonnull<const Statement*> ast_node)
|
||||
-> ErrorOr<Success> {
|
||||
while (true) {
|
||||
if (const auto* statement_action =
|
||||
llvm::dyn_cast<StatementAction>(todo_.Top().get());
|
||||
@@ -169,24 +178,30 @@ void ActionStack::UnwindTo(Nonnull<const Statement*> ast_node) {
|
||||
}
|
||||
todo_.Pop();
|
||||
}
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::UnwindPast(Nonnull<const Statement*> ast_node) {
|
||||
UnwindTo(ast_node);
|
||||
auto ActionStack::UnwindPast(Nonnull<const Statement*> ast_node)
|
||||
-> ErrorOr<Success> {
|
||||
RETURN_IF_ERROR(UnwindTo(ast_node));
|
||||
todo_.Pop();
|
||||
PopScopes();
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::UnwindPast(Nonnull<const Statement*> ast_node,
|
||||
Nonnull<const Value*> result) {
|
||||
UnwindPast(ast_node);
|
||||
auto ActionStack::UnwindPast(Nonnull<const Statement*> ast_node,
|
||||
Nonnull<const Value*> result) -> ErrorOr<Success> {
|
||||
RETURN_IF_ERROR(UnwindPast(ast_node));
|
||||
SetResult(result);
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::Resume(Nonnull<const ContinuationValue*> continuation) {
|
||||
auto ActionStack::Resume(Nonnull<const ContinuationValue*> continuation)
|
||||
-> ErrorOr<Success> {
|
||||
Action& action = *todo_.Top();
|
||||
action.set_pos(action.pos() + 1);
|
||||
continuation->stack().RestoreTo(todo_);
|
||||
return Success();
|
||||
}
|
||||
|
||||
static auto IsRunAction(const Action& action) -> bool {
|
||||
@@ -194,7 +209,7 @@ static auto IsRunAction(const Action& action) -> bool {
|
||||
return statement != nullptr && llvm::isa<Run>(statement->statement());
|
||||
}
|
||||
|
||||
void ActionStack::Suspend() {
|
||||
auto ActionStack::Suspend() -> ErrorOr<Success> {
|
||||
// Pause the current continuation
|
||||
todo_.Pop();
|
||||
std::vector<std::unique_ptr<Action>> paused;
|
||||
@@ -205,6 +220,7 @@ void ActionStack::Suspend() {
|
||||
llvm::cast<const ContinuationValue>(*todo_.Top()->results()[0]);
|
||||
// Update the continuation with the paused stack.
|
||||
continuation.stack().StoreReversed(std::move(paused));
|
||||
return Success();
|
||||
}
|
||||
|
||||
void ActionStack::PopScopes() {
|
||||
|
||||
Reference in New Issue
Block a user