From 48dc2f7b4f0f1069a24c4c3e2438f0ceb2a09a7b Mon Sep 17 00:00:00 2001 From: Shashank Mittal Date: Sun, 26 May 2024 22:42:59 +0530 Subject: [PATCH] fixes from #1359 Signed-off-by: Shashank Mittal --- kclvm/sema/src/resolver/arg.rs | 44 ++++++++++++---------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/kclvm/sema/src/resolver/arg.rs b/kclvm/sema/src/resolver/arg.rs index 7c8ccecbf..deb29bf1f 100644 --- a/kclvm/sema/src/resolver/arg.rs +++ b/kclvm/sema/src/resolver/arg.rs @@ -5,6 +5,7 @@ use indexmap::IndexSet; use kclvm_ast::ast; use kclvm_ast::pos::GetPos; +use kclvm_error::Position; use crate::ty::TypeRef; @@ -38,17 +39,24 @@ impl<'ctx> Resolver<'ctx> { let mut kwarg_types: Vec<(String, TypeRef)> = vec![]; let mut check_table: IndexSet = IndexSet::default(); for kw in kwargs { - let (suggs, msg) = self.get_arg_kw_err_suggestion(kw, func_ty); if !kw.node.arg.node.names.is_empty() { let arg_name = &kw.node.arg.node.names[0].node; + let fix_range=kw.get_span_pos(); + let (start_pos,end_pos)=fix_range; + let start_column=start_pos.column.map(|col| col.saturating_sub(2)); + let modified_start_pos=Position{ + column:start_column, + ..start_pos.clone() + }; + let modified_fix_range=(modified_start_pos,end_pos); if check_table.contains(arg_name) { self.handler.add_compile_error_with_suggestions( &format!( - "{} has duplicated keyword argument {}{}", - func_name, arg_name, msg + "{} has duplicated keyword argument {}", + func_name, arg_name ), - kw.get_span_pos(), - Some(suggs), + modified_fix_range, + Some(vec![]), ); } check_table.insert(arg_name.to_string()); @@ -58,7 +66,7 @@ impl<'ctx> Resolver<'ctx> { kwarg_types.push((arg_name.to_string(), arg_value_type.clone())); } else { self.handler - .add_compile_error(&format!("missing argument{}", msg), kw.get_span_pos()); + .add_compile_error(&format!("missing argument"), kw.get_span_pos()); } } // Do few argument count check @@ -124,7 +132,7 @@ impl<'ctx> Resolver<'ctx> { "{} got an unexpected keyword argument '{}'{}", func_name, arg_name, msg ), - kwargs[i].get_span_pos(), + kwargs[i].node.arg.get_span_pos(), Some(suggs), ); } @@ -147,28 +155,6 @@ impl<'ctx> Resolver<'ctx> { } /// Generate suggestions for keyword argument errors. - pub(crate) fn get_arg_kw_err_suggestion( - &self, - kw: &ast::NodeRef, - func_ty: &FunctionType, - ) -> (Vec, String) { - let attr = &kw.node.arg.node.names[0].node; - let valid_params: Vec<&str> = func_ty - .params - .iter() - .map(|param| param.name.as_str()) - .collect(); - let suggs = suggestions::provide_suggestions(attr, valid_params.into_iter()); - - let suggestion = if !suggs.is_empty() { - format!(", did you mean '{}'?", suggs.join(" or ")) - } else { - String::new() - }; - - (suggs, suggestion) - } - pub(crate) fn get_arg_kw_err_suggestion_from_name( &self, arg_name: &str,