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:
pk19604014
2022-03-22 16:17:04 -04:00
committed by GitHub
co-authored by Jon Meow Geoff Romer
parent e5a87af6fe
commit aa8a5f174d
43 changed files with 1324 additions and 873 deletions
@@ -9,6 +9,7 @@
#include "executable_semantics/ast/statement.h"
#include "executable_semantics/common/error.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
using llvm::cast;
@@ -31,114 +32,121 @@ struct FunctionData {
// belongs to, and that information may be updated by this call. `function`
// can be nullopt if `statement` does not belong to a function body, for
// example if it is part of a continuation body instead.
static void ResolveControlFlow(Nonnull<Statement*> statement,
static auto ResolveControlFlow(Nonnull<Statement*> statement,
std::optional<Nonnull<const Statement*>> loop,
std::optional<Nonnull<FunctionData*>> function) {
std::optional<Nonnull<FunctionData*>> function)
-> ErrorOr<Success> {
switch (statement->kind()) {
case StatementKind::Return: {
if (!function.has_value()) {
FATAL_COMPILATION_ERROR(statement->source_loc())
<< "return is not within a function body";
return FATAL_COMPILATION_ERROR(statement->source_loc())
<< "return is not within a function body";
}
const ReturnTerm& function_return =
(*function)->declaration->return_term();
if (function_return.is_auto()) {
if ((*function)->saw_return_in_auto) {
FATAL_COMPILATION_ERROR(statement->source_loc())
<< "Only one return is allowed in a function with an `auto` "
"return type.";
return FATAL_COMPILATION_ERROR(statement->source_loc())
<< "Only one return is allowed in a function with an `auto` "
"return type.";
}
(*function)->saw_return_in_auto = true;
}
auto& ret = cast<Return>(*statement);
ret.set_function((*function)->declaration);
if (ret.is_omitted_expression() != function_return.is_omitted()) {
FATAL_COMPILATION_ERROR(ret.source_loc())
<< ret << " should" << (function_return.is_omitted() ? " not" : "")
<< " provide a return value, to match the function's signature.";
return FATAL_COMPILATION_ERROR(ret.source_loc())
<< ret << " should"
<< (function_return.is_omitted() ? " not" : "")
<< " provide a return value, to match the function's signature.";
}
return;
return Success();
}
case StatementKind::Break:
if (!loop.has_value()) {
FATAL_COMPILATION_ERROR(statement->source_loc())
<< "break is not within a loop body";
return FATAL_COMPILATION_ERROR(statement->source_loc())
<< "break is not within a loop body";
}
cast<Break>(*statement).set_loop(*loop);
return;
return Success();
case StatementKind::Continue:
if (!loop.has_value()) {
FATAL_COMPILATION_ERROR(statement->source_loc())
<< "continue is not within a loop body";
return FATAL_COMPILATION_ERROR(statement->source_loc())
<< "continue is not within a loop body";
}
cast<Continue>(*statement).set_loop(*loop);
return;
return Success();
case StatementKind::If: {
auto& if_stmt = cast<If>(*statement);
ResolveControlFlow(&if_stmt.then_block(), loop, function);
RETURN_IF_ERROR(
ResolveControlFlow(&if_stmt.then_block(), loop, function));
if (if_stmt.else_block().has_value()) {
ResolveControlFlow(*if_stmt.else_block(), loop, function);
RETURN_IF_ERROR(
ResolveControlFlow(*if_stmt.else_block(), loop, function));
}
return;
return Success();
}
case StatementKind::Block: {
auto& block = cast<Block>(*statement);
for (auto* block_statement : block.statements()) {
ResolveControlFlow(block_statement, loop, function);
RETURN_IF_ERROR(ResolveControlFlow(block_statement, loop, function));
}
return;
return Success();
}
case StatementKind::While:
ResolveControlFlow(&cast<While>(*statement).body(), statement, function);
return;
RETURN_IF_ERROR(ResolveControlFlow(&cast<While>(*statement).body(),
statement, function));
return Success();
case StatementKind::Match: {
auto& match = cast<Match>(*statement);
for (Match::Clause& clause : match.clauses()) {
ResolveControlFlow(&clause.statement(), loop, function);
RETURN_IF_ERROR(
ResolveControlFlow(&clause.statement(), loop, function));
}
return;
return Success();
}
case StatementKind::Continuation:
ResolveControlFlow(&cast<Continuation>(*statement).body(), std::nullopt,
std::nullopt);
return;
RETURN_IF_ERROR(ResolveControlFlow(&cast<Continuation>(*statement).body(),
std::nullopt, std::nullopt));
return Success();
case StatementKind::ExpressionStatement:
case StatementKind::Assign:
case StatementKind::VariableDefinition:
case StatementKind::Run:
case StatementKind::Await:
return;
return Success();
}
}
void ResolveControlFlow(Nonnull<Declaration*> declaration) {
auto ResolveControlFlow(Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
switch (declaration->kind()) {
case DeclarationKind::FunctionDeclaration: {
auto& function = cast<FunctionDeclaration>(*declaration);
if (function.body().has_value()) {
FunctionData data = {.declaration = &function};
ResolveControlFlow(*function.body(), std::nullopt, &data);
RETURN_IF_ERROR(
ResolveControlFlow(*function.body(), std::nullopt, &data));
}
break;
}
case DeclarationKind::ClassDeclaration: {
auto& class_decl = cast<ClassDeclaration>(*declaration);
for (Nonnull<Declaration*> member : class_decl.members()) {
ResolveControlFlow(member);
RETURN_IF_ERROR(ResolveControlFlow(member));
}
break;
}
case DeclarationKind::InterfaceDeclaration: {
auto& iface_decl = cast<InterfaceDeclaration>(*declaration);
for (Nonnull<Declaration*> member : iface_decl.members()) {
ResolveControlFlow(member);
RETURN_IF_ERROR(ResolveControlFlow(member));
}
break;
}
case DeclarationKind::ImplDeclaration: {
auto& impl_decl = cast<ImplDeclaration>(*declaration);
for (Nonnull<Declaration*> member : impl_decl.members()) {
ResolveControlFlow(member);
RETURN_IF_ERROR(ResolveControlFlow(member));
}
break;
}
@@ -147,12 +155,14 @@ void ResolveControlFlow(Nonnull<Declaration*> declaration) {
// do nothing
break;
}
return Success();
}
void ResolveControlFlow(AST& ast) {
auto ResolveControlFlow(AST& ast) -> ErrorOr<Success> {
for (auto declaration : ast.declarations) {
ResolveControlFlow(declaration);
RETURN_IF_ERROR(ResolveControlFlow(declaration));
}
return Success();
}
} // namespace Carbon