From 2dbbc7b1284ffb9f9f8a25cf4436f4880b647b3f Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 12 Jul 2024 16:16:28 -0400 Subject: [PATCH] Make errors nice --- dust-lang/src/abstract_tree/value_node.rs | 30 ++++------------------- dust-lang/src/error.rs | 1 + dust-lang/src/lib.rs | 14 ++++++++++- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/dust-lang/src/abstract_tree/value_node.rs b/dust-lang/src/abstract_tree/value_node.rs index 526161b..f818067 100644 --- a/dust-lang/src/abstract_tree/value_node.rs +++ b/dust-lang/src/abstract_tree/value_node.rs @@ -122,34 +122,14 @@ impl AbstractNode for ValueNode { return Err(ValidationError::WrongTypeArgumentsCount { expected: parameters.len(), actual: arguments.len(), + position: arguments.last().unwrap().position(), }); } - let mut arguments = arguments.iter(); - let mut found = false; - - for (identifier, content) in variants { - 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(), - } - })?; - } - } - } - } + let found = variants + .into_iter() + .find(|(identifier, _)| identifier == &variant.node) + .is_some(); if !found { return Err(ValidationError::EnumVariantNotFound { diff --git a/dust-lang/src/error.rs b/dust-lang/src/error.rs index b47b0b1..29002ca 100644 --- a/dust-lang/src/error.rs +++ b/dust-lang/src/error.rs @@ -184,6 +184,7 @@ pub enum ValidationError { WrongTypeArgumentsCount { expected: usize, actual: usize, + position: SourcePosition, }, } diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index 12f8de2..19f7a43 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -445,7 +445,19 @@ impl InterpreterError { .add_label(Label::new((self.source_id.clone(), 0..0)).with_message(reason)), ValidationError::CannotUsePath(_) => 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) + ), + ), + ), } }