exit if any errors were emitted

This commit is contained in:
gluax 2022-04-22 15:57:18 -07:00
parent 22b20bdd6c
commit c36023a3d6
3 changed files with 40 additions and 11 deletions

View File

@ -110,10 +110,7 @@ impl<'a> Compiler<'a> {
// Write the AST snapshot post parsing
// ast.to_json_file_without_keys(self.output_directory, "canonicalization_ast.json", &["span"])?;
let _symbol_table = match CreateSymbolTable::do_pass((&ast, self.handler)) {
Ok(table) => table,
Err(_) => todo!("exit with latest error code here"),
};
let _symbol_table = CreateSymbolTable::do_pass((&ast, self.handler));
Ok(ast)
}

View File

@ -30,15 +30,13 @@ use leo_errors::emitter::Handler;
impl<'a> Pass<'a> for CreateSymbolTable<'a> {
type Input = (&'a Ast, &'a Handler);
type Output = Result<SymbolTable<'a>, ()>;
type Output = SymbolTable<'a>;
fn do_pass((ast, handler): Self::Input) -> Self::Output {
let mut visitor = VisitorDirector::new(CreateSymbolTable::new(handler));
visitor.visit_program(ast.as_repr());
if handler.had_errors() {
return Err(());
}
handler.exit_with_last_err_code();
Ok(visitor.visitor().symbol_table())
visitor.visitor().symbol_table()
}
}

View File

@ -27,18 +27,29 @@ pub trait Emitter {
/// Emit the error `err`.
fn emit_err(&mut self, err: LeoError);
/// Tracks last emmited error.
fn last_emited_err_code(&self) -> Option<i32>;
/// Emit the warning.
fn emit_warning(&mut self, warning: LeoWarning);
}
/// A trivial `Emitter` using the standard error.
pub struct StderrEmitter;
pub struct StderrEmitter {
/// Exit code of the last emitted error.
last_error_code: Option<i32>,
}
impl Emitter for StderrEmitter {
fn emit_err(&mut self, err: LeoError) {
self.last_error_code = Some(err.exit_code());
eprintln!("{}", err);
}
fn last_emited_err_code(&self) -> Option<i32> {
self.last_error_code
}
fn emit_warning(&mut self, warning: LeoWarning) {
eprintln!("{warning}");
}
@ -64,6 +75,11 @@ impl<T> Buffer<T> {
pub fn into_inner(self) -> Vec<T> {
self.0
}
/// Last entry to the buffer.
pub fn last_entry(&self) -> Option<&T> {
self.0.last()
}
}
impl<T: fmt::Display> fmt::Display for Buffer<T> {
@ -110,6 +126,11 @@ impl Emitter for BufferEmitter {
self.0.borrow_mut().push(err);
}
fn last_emited_err_code(&self) -> Option<i32> {
let temp = &*self.0.borrow();
temp.last_entry().map(|entry| entry.exit_code())
}
fn emit_warning(&mut self, warning: LeoWarning) {
self.1.borrow_mut().push(warning);
}
@ -133,6 +154,11 @@ impl HandlerInner {
self.emitter.emit_err(err);
}
/// Gets the last emitted error's exit code.
fn last_emited_err_code(&self) -> Option<i32> {
self.emitter.last_emited_err_code()
}
/// Emit the error `err`.
fn emit_warning(&mut self, warning: LeoWarning) {
self.warn_count = self.warn_count.saturating_add(1);
@ -149,7 +175,7 @@ pub struct Handler {
impl Default for Handler {
fn default() -> Self {
Self::new(Box::new(StderrEmitter))
Self::new(Box::new(StderrEmitter { last_error_code: None }))
}
}
@ -211,6 +237,14 @@ impl Handler {
self.err_count() > 0
}
/// Gets the last emitted error's exit code if it exists.
/// Then exits the program with it if it did exist.
pub fn exit_with_last_err_code(&self) {
if let Some(code) = self.inner.borrow().last_emited_err_code() {
std::process::exit(code);
}
}
/// Extend handler with `error` given `res = Err(error)`.
#[allow(clippy::result_unit_err)]
pub fn extend_if_error<T>(&self, res: Result<T, LeoError>) -> Result<T, ()> {