Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leo error reporting after build #28327

Open
wants to merge 3 commits into
base: mainnet
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions compiler/passes/src/type_checking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,62 @@ impl<'a, N: Network> Pass for TypeChecker<'a, N> {
type Output = Result<(SymbolTable, StructGraph, CallGraph)>;

fn do_pass((ast, handler, st, tt, max_depth, await_checking): Self::Input) -> Self::Output {
// get program name without consuming AST
ungaro marked this conversation as resolved.
Show resolved Hide resolved
let program_name = &ast
ungaro marked this conversation as resolved.
Show resolved Hide resolved
.ast
.program_scopes
.keys()
.next()
.map(|s| s.to_string())
.unwrap_or_else(|| "Unknown program".to_string());

let mut visitor = TypeChecker::<N>::new(st, tt, handler, max_depth, await_checking);
visitor.visit_program(ast.as_repr());

// color codes for terminal
const RED: &str = "\x1b[31m";
const YELLOW: &str = "\x1b[33m";
const RESET: &str = "\x1b[0m";

// get error and warning counts from handler
let inner = handler.inner.borrow();
let err_count = inner.err_count;
let warn_count = inner.warn_count;

// if there is at least one error or warning, add two empty lines before report
if err_count + warn_count > 0 {
println!("\n");
}

// show warning counts
if warn_count > 0 {
println!(
"{}warning{}: {}.leo generated {} warning{}",
YELLOW,
RESET,
program_name,
warn_count,
if warn_count > 1 { "s" } else { "" }
);
}

//show error counts, if warnings emitted include them as well.
if err_count > 0 {
let error_message = format!(
"{}error{}: could not compile {}.leo due to {} previous error{}",
RED,
RESET,
program_name,
err_count,
if err_count > 1 { "s" } else { "" }
);
if warn_count > 0 {
println!("{}; {} warning{} emitted", error_message, warn_count, if warn_count > 1 { "s" } else { "" });
} else {
println!("{}", error_message);
}
}

handler.last_err().map_err(|e| *e)?;

// Remove unused structs from the struct graph.
Expand Down
8 changes: 4 additions & 4 deletions errors/src/emitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ impl Emitter for BufferEmitter {

/// Contains the actual data for `Handler`.
/// Modelled this way to afford an API using interior mutability.
struct HandlerInner {
pub struct HandlerInner {
/// Number of errors emitted thus far.
err_count: usize,
pub err_count: usize,
/// Number of warnings emitted thus far.
warn_count: usize,
pub warn_count: usize,
/// The sink through which errors will be emitted.
emitter: Box<dyn Emitter>,
}
Expand Down Expand Up @@ -168,7 +168,7 @@ impl HandlerInner {
pub struct Handler {
/// The inner handler.
/// `RefCell` is used here to avoid `&mut` all over the compiler.
inner: RefCell<HandlerInner>,
pub inner: RefCell<HandlerInner>,
}

impl Default for Handler {
Expand Down