fix last error double printing for error collection

This commit is contained in:
gluax 2022-05-23 18:14:36 -07:00
parent ba4f1666d1
commit 65d237e25f
5 changed files with 24 additions and 20 deletions

View File

@ -151,11 +151,11 @@ impl Emitter for BufferEmitter {
self.0.borrow_mut().push(LeoOrString::Leo(err));
}
fn last_emited_err(&self) -> Option<LeoError> {
fn last_emitted_err_code(&self) -> Option<i32> {
let temp = &*self.0.borrow();
temp.last_entry().map(|entry| match entry {
LeoOrString::Leo(err) => err.clone(),
_ => unimplemented!(),
LeoOrString::Leo(err) => err.exit_code(),
_ => 0,
})
}

View File

@ -265,7 +265,7 @@ impl<'a> TypeChecker<'a> {
)
| Type::Field
| Type::Group,
) => {},
) => {}
Some(t) => self
.handler
.emit_err(TypeCheckerError::type_is_not_negatable(t, unary.inner.span()).into()),

View File

@ -38,8 +38,6 @@ impl<'a> Pass<'a> for TypeChecker<'a> {
fn do_pass((ast, symbol_table, handler): Self::Input) -> Self::Output {
let mut visitor = VisitorDirector::new(TypeChecker::new(symbol_table, handler));
visitor.visit_program(ast.as_repr());
// todo @gluax: awkward cause last error double prints...
// but can't os exit or it causes tests to stop.
handler.last_err()?;
Ok(())

View File

@ -27,8 +27,8 @@ pub trait Emitter {
/// Emit the error `err`.
fn emit_err(&mut self, err: LeoError);
/// Tracks last emmited error.
fn last_emited_err(&self) -> Option<LeoError>;
/// Tracks last emitted error.
fn last_emitted_err_code(&self) -> Option<i32>;
/// Emit the warning.
fn emit_warning(&mut self, warning: LeoWarning);
@ -36,18 +36,18 @@ pub trait Emitter {
/// A trivial `Emitter` using the standard error.
pub struct StderrEmitter {
/// The last emitted error.
last_error: Option<LeoError>,
/// 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 = Some(err.clone());
self.last_error_code = Some(err.exit_code());
eprintln!("{}", err);
}
fn last_emited_err(&self) -> Option<LeoError> {
self.last_error.clone()
fn last_emitted_err_code(&self) -> Option<i32> {
self.last_error_code
}
fn emit_warning(&mut self, warning: LeoWarning) {
@ -126,9 +126,9 @@ impl Emitter for BufferEmitter {
self.0.borrow_mut().push(err);
}
fn last_emited_err(&self) -> Option<LeoError> {
fn last_emitted_err_code(&self) -> Option<i32> {
let temp = &*self.0.borrow();
temp.last_entry().cloned()
temp.last_entry().map(|entry| entry.exit_code())
}
fn emit_warning(&mut self, warning: LeoWarning) {
@ -155,8 +155,8 @@ impl HandlerInner {
}
/// Gets the last emitted error's exit code.
fn last_emited_err(&self) -> Option<LeoError> {
self.emitter.last_emited_err()
fn last_emited_err_code(&self) -> Option<i32> {
self.emitter.last_emitted_err_code()
}
/// Emit the error `err`.
@ -175,7 +175,7 @@ pub struct Handler {
impl Default for Handler {
fn default() -> Self {
Self::new(Box::new(StderrEmitter { last_error: None }))
Self::new(Box::new(StderrEmitter { last_error_code: None }))
}
}
@ -240,8 +240,8 @@ impl Handler {
/// Gets the last emitted error's exit code if it exists.
/// Then exits the program with it if it did exist.
pub fn last_err(&self) -> Result<(), LeoError> {
if let Some(code) = self.inner.borrow().last_emited_err() {
Err(code)
if let Some(code) = self.inner.borrow().last_emited_err_code() {
Err(LeoError::LastErrorCode(code))
} else {
Ok(())
}

View File

@ -70,6 +70,10 @@ pub enum LeoError {
/// Represents an Type Checker Error in a Leo Error.
#[error(transparent)]
TypeCheckerError(#[from] TypeCheckerError),
/// Purely for just exiting with the correct status code and
/// not re-displaying an error.
#[error("")]
LastErrorCode(i32),
}
impl LeoError {
@ -85,6 +89,7 @@ impl LeoError {
ParserError(error) => error.error_code(),
PackageError(error) => error.error_code(),
TypeCheckerError(error) => error.error_code(),
LastErrorCode(_) => unreachable!(),
}
}
@ -100,6 +105,7 @@ impl LeoError {
ParserError(error) => error.exit_code(),
PackageError(error) => error.exit_code(),
TypeCheckerError(error) => error.exit_code(),
LastErrorCode(code) => *code,
}
}
}