1
0

Make errors nice

This commit is contained in:
Jeff 2024-07-12 16:16:28 -04:00
parent c47d09fd1d
commit 2dbbc7b128
3 changed files with 19 additions and 26 deletions

View File

@ -122,34 +122,14 @@ impl AbstractNode for ValueNode {
return Err(ValidationError::WrongTypeArgumentsCount { return Err(ValidationError::WrongTypeArgumentsCount {
expected: parameters.len(), expected: parameters.len(),
actual: arguments.len(), actual: arguments.len(),
position: arguments.last().unwrap().position(),
}); });
} }
let mut arguments = arguments.iter(); let found = variants
let mut found = false; .into_iter()
.find(|(identifier, _)| identifier == &variant.node)
for (identifier, content) in variants { .is_some();
if identifier == variant.node {
found = true;
}
if let Some(content) = content {
for expected_type in &content {
if let Type::Generic {
concrete_type: None,
..
} = expected_type
{
arguments.next().ok_or_else(|| {
ValidationError::WrongTypeArgumentsCount {
expected: content.len(),
actual: arguments.len(),
}
})?;
}
}
}
}
if !found { if !found {
return Err(ValidationError::EnumVariantNotFound { return Err(ValidationError::EnumVariantNotFound {

View File

@ -184,6 +184,7 @@ pub enum ValidationError {
WrongTypeArgumentsCount { WrongTypeArgumentsCount {
expected: usize, expected: usize,
actual: usize, actual: usize,
position: SourcePosition,
}, },
} }

View File

@ -445,7 +445,19 @@ impl InterpreterError {
.add_label(Label::new((self.source_id.clone(), 0..0)).with_message(reason)), .add_label(Label::new((self.source_id.clone(), 0..0)).with_message(reason)),
ValidationError::CannotUsePath(_) => todo!(), ValidationError::CannotUsePath(_) => todo!(),
ValidationError::Uninitialized => todo!(), ValidationError::Uninitialized => todo!(),
ValidationError::WrongTypeArgumentsCount { expected, actual } => todo!(), ValidationError::WrongTypeArgumentsCount {
expected,
actual,
position,
} => builder.add_label(
Label::new((self.source_id.clone(), position.0..position.1)).with_message(
format!(
"Expected {} type arguments but got {}.",
expected.fg(type_color),
actual.fg(type_color)
),
),
),
} }
} }