8000 Improve error format when emitting by CohenArthur · Pull Request #556 · jinko-core/jinko · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 15 additions & 14 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl ErrorHandler {
first_err.emit();
}
self.errors.iter().skip(1).for_each(|e| {
eprintln!("----------------------------------------------------------------");
eprintln!();
e.emit()
});
}
Expand Down Expand Up @@ -72,13 +72,13 @@ pub enum ErrKind {
impl ErrKind {
pub fn as_str(&self) -> &'static str {
match self {
ErrKind::Hint => "Hint",
ErrKind::Parsing => "Parsing",
ErrKind::Context => "Interpreter",
ErrKind::TypeChecker => "Typechecker",
ErrKind::Generics => "Generics",
ErrKind::IO => "I/O",
ErrKind::ExternFunc => "External Function",
ErrKind::Hint => "hint",
ErrKind::Parsing => "parsing",
ErrKind::Context => "runtime",
ErrKind::TypeChecker => "typechecker",
ErrKind::Generics => "generics",
ErrKind::IO => "i/o",
ErrKind::ExternFunc => "external function",
}
}
}
Expand All @@ -97,11 +97,9 @@ impl Error {

if let Some(msg) = &self.msg {
if let Some(path) = loc.path() {
let kind_str = self.kind.as_str();

eprintln!("{}: {}", "error type".black().on_yellow(), kind_str);
eprintln!(
"{}:{}:{}: {}",
"{}: {}:{}:{}: {}",
"error".black().on_yellow(),
path.display().to_string().yellow(),
loc.start().line(),
loc.start().column(),
Expand All @@ -119,6 +117,7 @@ impl Error {
}

fn emit_hint(&self) {
eprintln!();
eprint!("{}: ", "hint".black().on_green());
if let Some(loc) = &self.loc {
if let Some(path) = loc.path() {
Expand Down Expand Up @@ -147,9 +146,11 @@ impl Error {
eprintln!("{}", msg)
}

eprintln!("----------------------------------------------------------------");
if let Some(first_hint) = self.hints.first() {
first_hint.emit_hint();
}

self.hints.iter().for_each(|hint| hint.emit_hint());
self.hints.iter().skip(1).for_each(|hint| hint.emit_hint());
}

pub fn new(kind: ErrKind) -> Error {
Expand Down
7 changes: 6 additions & 1 deletion src/instruction/type_instantiation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ impl TypeCheck for TypeInstantiation {
value_ty,
field_dec.get_type()
))
.with_loc(var_assign.location().cloned()),
.with_loc(var_assign.location().cloned())
.with_hint(
Error::hint()
.with_msg(format!("field `{}` declared here", field_dec.name()))
.with_loc(field_dec.location().cloned()),
),
);
// FIXME: Add hint for typedec argument later
}
Expand Down
7 changes: 7 additions & 0 deletions src/instruction/var_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ impl VarAssign {
}
}

pub fn with_loc(self, location: SpanTuple) -> VarAssign {
VarAssign {
location: Some(location),
..self
}
}

/// Get a reference to the symbol of the variable assignment
pub fn symbol(&self) -> &str {
&self.symbol
Expand Down
23 changes: 21 additions & 2 deletions src/parser/constructs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,25 @@ fn func_or_type_inst_args(
generics: Vec<TypeId>,
start_loc: Location,
) -> ParseResult<ParseInput, Box<dyn Instruction>> {
let (input, first_attr_start_loc) = position(input)?;
if let Ok((input, first_attr)) =
terminated(terminated(Token::identifier, nom_next), Token::colon)(input)
{
let input = next(input);
let (input, first_attr_val) = expr(input)?;
let (input, first_attr_end_loc) = position(input)?;
let (input, attrs) = many0(preceded(Token::comma, type_inst_arg))(input)?;
let (input, _) = Token::right_parenthesis(input)?;
let (input, end_loc) = position(input)?;

let mut type_inst = TypeInstantiation::new(TypeId::new(Symbol::from(id)));
type_inst.add_field(VarAssign::new(false, first_attr, first_attr_val));
type_inst.add_field(VarAssign::new(false, first_attr, first_attr_val).with_loc(
SpanTuple::new(
input.extra,
first_attr_start_loc.into(),
first_attr_end_loc.into(),
),
));
attrs.into_iter().for_each(|attr| type_inst.add_field(attr));

type_inst.set_generics(generics);
Expand Down Expand Up @@ -741,13 +750,23 @@ fn typed_arg(input: ParseInput) -> ParseResult<ParseInput, DecArg> {

/// type_inst_arg = spaced_identifier ':' expr
fn type_inst_arg(input: ParseInput) -> ParseResult<ParseInput, VarAssign> {
let input = next(input);
let (input, start_loc) = position(input)?;
let (input, (id, _)) = spaced_identifier(input)?;
let (input, _) = Token::colon(input)?;
let input = next(input);
let (input, value) = expr(input)?;
let (input, end_loc) = position(input)?;
let input = next(input);

Ok((input, VarAssign::new(false, id, value)))
Ok((
input,
VarAssign::new(false, id, value).with_loc(SpanTuple::new(
input.extra,
start_loc.into(),
end_loc.into(),
)),
))
}

fn spaced_identifier(input: ParseInput) -> ParseResult<ParseInput, (String, Location)> {
Expand Down
7 changes: 4 additions & 3 deletions src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use type_id::{TypeId, PRIMITIVE_TYPES};

use crate::context::ScopeMap;
use crate::error::{ErrKind, Error, ErrorHandler};
use crate::instruction::{FunctionDec, TypeDec};
use crate::instruction::{FunctionDec, Instruction, TypeDec};
use crate::log;

use colored::Colorize;
Expand Down Expand Up @@ -149,12 +149,13 @@ impl TypeCtx {

/// Declare a newly-created function's type
pub fn declare_function(&mut self, name: String, function: FunctionDec) -> Result<(), Error> {
// FIXME: Remove clone here
// FIXME: Remove clones here
let loc = function.location().cloned();
match self.types.add_function(name.clone(), function) {
Ok(_) => Ok(()),
Err(err) => {
let previous_dec = self.types.get_function(&name).unwrap();
Err(err.with_hint(
Err(err.with_loc(loc).with_hint(
Error::new(ErrKind::Hint)
.with_msg(String::from("previous declaration here"))
.with_loc(previous_dec.loc()),
Expand Down
6 changes: 6 additions & 0 deletions tests/ft/errors/errors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests:
- name: "Error stress test"
binary: "target/debug/jinko"
args:
- "tests/ft/errors/stress.jk"
exit_code: 1
28 changes: 28 additions & 0 deletions tests/ft/errors/stress.jk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file shall not contain any parsing errors

undeclared_call(15);
UndeclaredType(f0: "eh", f1: "personality");

// Comments afterwards to see what context looks like
// More comments

a = 15;
mut a = 14;

b = 14;
b = 13;

mut c = 15;
c = 14.4;

func redeclare() -> int { 15 }
func redeclare() -> int { 14 }

type Wrapper(inner: char);
w = Wrapper(inner: "not a char");

type Redec(inner: int);
type Redec(inner: string);

type MultiWrapper(inner: char, inner2: char);
multi_w = MultiWrapper(inner: 'c', inner2: "not a char");
0