Skip to content

Commit

Permalink
Improve array literal related type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Apr 7, 2021
1 parent 2aaea9e commit 192bd50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
27 changes: 13 additions & 14 deletions compiler/src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ impl<'a> Typechecker<'a> {
}
Expr::ArrayLit(checked, Some(type_), *pos)
}
None => {
if let Some(TypeId::Array(inner)) = expected {
Expr::ArrayLit(vec![], Some(*inner.clone()), *pos)
} else {
return Err(Error::type_annotation_required(*pos));
}
}
None => match expected {
Some(TypeId::Array(inner)) => Expr::ArrayLit(vec![], Some(*inner.clone()), *pos),
Some(type_) => return Err(Error::type_error("array", type_.pretty(self.pool)?, *pos)),
None => return Err(Error::type_annotation_required(*pos)),
},
},
Expr::Declare(name, type_, init, pos) => {
let (initializer, type_) = match (type_, init) {
Expand Down Expand Up @@ -278,13 +276,14 @@ impl<'a> Typechecker<'a> {
}
Expr::ForIn(name, array, body, pos) => {
let array = self.check(array, None, scope)?;
if let TypeId::Array(inner) = type_of(&array, scope, self.pool)? {
let mut local_scope = scope.clone();
let local = self.add_local(name.clone(), &inner, &mut local_scope)?;
let body = self.check_seq(body, &mut local_scope)?;
Expr::ForIn(local, Box::new(array), body, *pos)
} else {
return Err(Error::array_expected_in_for(*pos));
match type_of(&array, scope, self.pool)? {
TypeId::Array(inner) => {
let mut local_scope = scope.clone();
let local = self.add_local(name.clone(), &inner, &mut local_scope)?;
let body = self.check_seq(body, &mut local_scope)?;
Expr::ForIn(local, Box::new(array), body, *pos)
}
other => return Err(Error::type_error(other.pretty(self.pool)?, "array", *pos)),
}
}
Expr::This(pos) => Expr::This(*pos),
Expand Down
6 changes: 1 addition & 5 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Error {
Error::CompileError("Type annotation required".to_owned(), pos)
}

pub fn type_error<N: Display>(from: N, to: N, pos: Pos) -> Error {
pub fn type_error<F: Display, T: Display>(from: F, to: T, pos: Pos) -> Error {
let error = format!("Can't coerce {} to {}", from, to);
Error::CompileError(error, pos)
}
Expand Down Expand Up @@ -70,10 +70,6 @@ impl Error {
Error::CompileError(error, pos)
}

pub fn array_expected_in_for(pos: Pos) -> Error {
Error::CompileError("Array expected in for expression".to_owned(), pos)
}

pub fn no_matching_overload<N: Display>(name: N, errors: &[FunctionResolutionError], pos: Pos) -> Error {
let error = format!(
"Arguments passed to {} do not match any of the overloads:{}",
Expand Down

0 comments on commit 192bd50

Please sign in to comment.