diff --git a/kclvm/ast/src/ast.rs b/kclvm/ast/src/ast.rs index 72548f381..8206d599a 100644 --- a/kclvm/ast/src/ast.rs +++ b/kclvm/ast/src/ast.rs @@ -42,7 +42,7 @@ use compiler_base_span::{Loc, Span}; use super::token; use crate::{node_ref, pos::ContainsPos}; -use kclvm_error::Position; +use kclvm_error::{diagnostic::Range, Position}; /// PosTuple denotes the tuple `(filename, line, column, end_line, end_column)`. pub type PosTuple = (String, u64, u64, u64, u64); @@ -62,8 +62,8 @@ impl Into for Pos { } } -impl Into<(Position, Position)> for Pos { - fn into(self) -> (Position, Position) { +impl Into for Pos { + fn into(self) -> Range { ( Position { filename: self.0.clone(), diff --git a/kclvm/ast/src/pos.rs b/kclvm/ast/src/pos.rs index c437faab7..1ed481c71 100644 --- a/kclvm/ast/src/pos.rs +++ b/kclvm/ast/src/pos.rs @@ -1,4 +1,4 @@ -use kclvm_error::Position; +use kclvm_error::{diagnostic::Range, Position}; use crate::ast; @@ -9,7 +9,7 @@ pub trait ContainsPos { pub trait GetPos { /// Get start and end position from node. - fn get_span_pos(&self) -> (Position, Position) { + fn get_span_pos(&self) -> Range { (self.get_pos(), self.get_end_pos()) } /// Get start pos from node. diff --git a/kclvm/error/src/diagnostic.rs b/kclvm/error/src/diagnostic.rs index 4bd58237d..6e7f96df5 100644 --- a/kclvm/error/src/diagnostic.rs +++ b/kclvm/error/src/diagnostic.rs @@ -95,7 +95,7 @@ impl From for Position { } impl Diagnostic { - pub fn new(level: Level, message: &str, range: (Position, Position)) -> Self { + pub fn new(level: Level, message: &str, range: Range) -> Self { Diagnostic::new_with_code(level, message, None, range, None) } @@ -104,7 +104,7 @@ impl Diagnostic { level: Level, message: &str, note: Option<&str>, - range: (Position, Position), + range: Range, code: Option, ) -> Self { Diagnostic { @@ -125,9 +125,11 @@ impl Diagnostic { } } +pub type Range = (Position, Position); + #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Message { - pub range: (Position, Position), + pub range: Range, pub style: Style, pub message: String, pub note: Option, diff --git a/kclvm/error/src/error.rs b/kclvm/error/src/error.rs index 10fdcdce6..577d343c3 100644 --- a/kclvm/error/src/error.rs +++ b/kclvm/error/src/error.rs @@ -142,7 +142,7 @@ pub enum WarningKind { /// let mut handler = Handler::default(); /// handler.add_warning(WarningKind::UnusedImportWarning, &[ /// Message { -/// pos: Position::dummy_pos(), +/// range: (Position::dummy_pos(), Position::dummy_pos()), /// style: Style::LineAndColumn, /// message: "Module 'a' imported but unused.".to_string(), /// note: None, @@ -168,7 +168,7 @@ impl std::fmt::Display for WarningKind { /// let mut handler = Handler::default(); /// handler.add_warning(WarningKind::UnusedImportWarning, &[ /// Message { -/// pos: Position::dummy_pos(), +/// range: (Position::dummy_pos(), Position::dummy_pos()), /// style: Style::LineAndColumn, /// message: "Module 'a' imported but unused.".to_string(), /// note: None, diff --git a/kclvm/error/src/lib.rs b/kclvm/error/src/lib.rs index bde251100..7a736cb20 100644 --- a/kclvm/error/src/lib.rs +++ b/kclvm/error/src/lib.rs @@ -18,6 +18,7 @@ use compiler_base_error::{ }; use compiler_base_session::{Session, SessionDiagnostic}; use compiler_base_span::{span::new_byte_pos, Span}; +use diagnostic::Range; use indexmap::IndexSet; use kclvm_runtime::PanicInfo; use std::{any::Any, sync::Arc}; @@ -91,7 +92,7 @@ impl Handler { } /// Construct a parse error and put it into the handler diagnostic buffer - pub fn add_syntex_error(&mut self, msg: &str, range: (Position, Position)) -> &mut Self { + pub fn add_syntex_error(&mut self, msg: &str, range: Range) -> &mut Self { let message = format!("Invalid syntax: {msg}"); let diag = Diagnostic::new_with_code( Level::Error, @@ -106,7 +107,7 @@ impl Handler { } /// Construct a type error and put it into the handler diagnostic buffer - pub fn add_type_error(&mut self, msg: &str, range: (Position, Position)) -> &mut Self { + pub fn add_type_error(&mut self, msg: &str, range: Range) -> &mut Self { let diag = Diagnostic::new_with_code( Level::Error, msg, @@ -120,7 +121,7 @@ impl Handler { } /// Construct a type error and put it into the handler diagnostic buffer - pub fn add_compile_error(&mut self, msg: &str, range: (Position, Position)) -> &mut Self { + pub fn add_compile_error(&mut self, msg: &str, range: Range) -> &mut Self { let diag = Diagnostic::new_with_code( Level::Error, msg, @@ -146,7 +147,7 @@ impl Handler { /// let mut handler = Handler::default(); /// handler.add_error(ErrorKind::InvalidSyntax, &[ /// Message { - /// pos: Position::dummy_pos(), + /// range: (Position::dummy_pos(), Position::dummy_pos()), /// style: Style::LineAndColumn, /// message: "Invalid syntax: expected '+', got '-'".to_string(), /// note: None, @@ -170,7 +171,7 @@ impl Handler { /// let mut handler = Handler::default(); /// handler.add_warning(WarningKind::UnusedImportWarning, &[ /// Message { - /// pos: Position::dummy_pos(), + /// range: (Position::dummy_pos(), Position::dummy_pos()), /// style: Style::LineAndColumn, /// message: "Module 'a' imported but unused.".to_string(), /// note: None, @@ -210,7 +211,7 @@ impl Handler { /// ``` /// use kclvm_error::*; /// let mut handler = Handler::default(); - /// handler.add_diagnostic(Diagnostic::new_with_code(Level::Error, "error message", None, Position::dummy_pos(), Some(DiagnosticId::Error(E1001.kind)))); + /// handler.add_diagnostic(Diagnostic::new_with_code(Level::Error, "error message", None, (Position::dummy_pos(), Position::dummy_pos()), Some(DiagnosticId::Error(E1001.kind)))); /// ``` #[inline] pub fn add_diagnostic(&mut self, diagnostic: Diagnostic) -> &mut Self { diff --git a/kclvm/parser/src/lib.rs b/kclvm/parser/src/lib.rs index 38663e372..9bfb5704a 100644 --- a/kclvm/parser/src/lib.rs +++ b/kclvm/parser/src/lib.rs @@ -19,7 +19,8 @@ use kclvm_config::modfile::{ KCL_MOD_FILE, }; use kclvm_config::path::ModRelativePath; -use kclvm_error::{ErrorKind, Message, Position, Style}; +use kclvm_error::diagnostic::Range; +use kclvm_error::{ErrorKind, Message, Style}; use kclvm_sema::plugin::PLUGIN_MODULE_PREFIX; use kclvm_utils::path::PathPrefix; use kclvm_utils::pkgpath::parse_external_pkg_name; @@ -355,7 +356,7 @@ impl Loader { self.sess.1.borrow_mut().add_error( ErrorKind::CannotFindModule, &[Message { - range: Into::<(Position, Position)>::into(pos), + range: Into::::into(pos), style: Style::Line, message: format!( "the `{}` is found multiple times in the current package and vendor package", @@ -374,7 +375,7 @@ impl Loader { self.sess.1.borrow_mut().add_error( ErrorKind::CannotFindModule, &[Message { - range: Into::<(Position, Position)>::into(pos), + range: Into::::into(pos), style: Style::Line, message: format!("pkgpath {} not found in the program", pkg_path), note: None, @@ -544,7 +545,7 @@ impl Loader { self.sess.1.borrow_mut().add_error( ErrorKind::CannotFindModule, &[Message { - range: Into::<(Position, Position)>::into(pos), + range: Into::::into(pos), style: Style::Line, message: format!("the plugin package `{}` is not found, please confirm if plugin mode is enabled", pkgpath), note: None, diff --git a/kclvm/sema/src/resolver/attr.rs b/kclvm/sema/src/resolver/attr.rs index 5f4089839..958eb0bcc 100644 --- a/kclvm/sema/src/resolver/attr.rs +++ b/kclvm/sema/src/resolver/attr.rs @@ -4,12 +4,13 @@ use crate::builtin::system_module::{get_system_module_members, UNITS, UNITS_NUMB use crate::builtin::STRING_MEMBER_FUNCTIONS; use crate::resolver::Resolver; use crate::ty::{ModuleKind, Type, TypeKind}; +use kclvm_error::diagnostic::Range; use kclvm_error::*; use super::node::ResolvedResult; impl<'ctx> Resolver<'ctx> { - pub fn check_attr_ty(&mut self, attr_ty: &Type, range: (Position, Position)) { + pub fn check_attr_ty(&mut self, attr_ty: &Type, range: Range) { if !attr_ty.is_any() && !attr_ty.is_key() { self.handler.add_error( ErrorKind::IllegalAttributeError, @@ -26,12 +27,7 @@ impl<'ctx> Resolver<'ctx> { } } - pub fn load_attr( - &mut self, - obj: Rc, - attr: &str, - range: (Position, Position), - ) -> ResolvedResult { + pub fn load_attr(&mut self, obj: Rc, attr: &str, range: Range) -> ResolvedResult { let (result, return_ty) = match &obj.kind { TypeKind::Any => (true, self.any_ty()), TypeKind::None diff --git a/kclvm/sema/src/resolver/calculation.rs b/kclvm/sema/src/resolver/calculation.rs index 8e25c612f..e63369295 100644 --- a/kclvm/sema/src/resolver/calculation.rs +++ b/kclvm/sema/src/resolver/calculation.rs @@ -3,6 +3,7 @@ use std::rc::Rc; use crate::resolver::Resolver; use crate::ty::{has_any_type, is_upper_bound, sup, Type, TypeInferMethods, ZERO_LIT_TYPES}; use kclvm_ast::ast; +use kclvm_error::diagnostic::Range; use kclvm_error::Position; const DIV_OR_MOD_ZERO_MSG: &str = "integer division or modulo by zero"; @@ -56,7 +57,7 @@ impl<'ctx> Resolver<'ctx> { left: Rc, right: Rc, op: &ast::BinOp, - range: (Position, Position), + range: Range, ) -> Rc { let t1 = self .ctx @@ -213,12 +214,7 @@ impl<'ctx> Resolver<'ctx> { /// - number unary negation (int, float) /// ~ number unary bitwise inversion (int) /// not x logical negation (any type) - pub fn unary( - &mut self, - ty: Rc, - op: &ast::UnaryOp, - range: (Position, Position), - ) -> Rc { + pub fn unary(&mut self, ty: Rc, op: &ast::UnaryOp, range: Range) -> Rc { if has_any_type(&[ty.clone()]) { return self.any_ty(); } @@ -259,7 +255,7 @@ impl<'ctx> Resolver<'ctx> { left: Rc, right: Rc, op: &ast::CmpOp, - range: (Position, Position), + range: Range, ) -> Rc { let t1 = self.ctx.ty_ctx.literal_union_type_to_variable_type(left); let t2 = self.ctx.ty_ctx.literal_union_type_to_variable_type(right); diff --git a/kclvm/sema/src/resolver/config.rs b/kclvm/sema/src/resolver/config.rs index f3bf6d190..4754fea72 100644 --- a/kclvm/sema/src/resolver/config.rs +++ b/kclvm/sema/src/resolver/config.rs @@ -10,7 +10,7 @@ use crate::ty::SchemaType; use crate::ty::{Type, TypeKind}; use kclvm_ast::ast; use kclvm_ast::pos::GetPos; -use kclvm_error::{ErrorKind, Message, Position, Style}; +use kclvm_error::{diagnostic::Range, ErrorKind, Message, Position, Style}; /// Config Expr type check state. /// @@ -314,12 +314,7 @@ impl<'ctx> Resolver<'ctx> { } /// Check config attr has been defined. - pub(crate) fn check_config_attr( - &mut self, - attr: &str, - range: &(Position, Position), - schema_ty: &SchemaType, - ) { + pub(crate) fn check_config_attr(&mut self, attr: &str, range: &Range, schema_ty: &SchemaType) { let runtime_type = kclvm_runtime::schema_runtime_type(&schema_ty.name, &schema_ty.pkgpath); match self.ctx.schema_mapping.get(&runtime_type) { Some(schema_mapping_ty) => { diff --git a/kclvm/sema/src/resolver/loop.rs b/kclvm/sema/src/resolver/loop.rs index dedb565a5..cdff8b773 100644 --- a/kclvm/sema/src/resolver/loop.rs +++ b/kclvm/sema/src/resolver/loop.rs @@ -4,7 +4,7 @@ use crate::resolver::Resolver; use crate::ty::{sup, Type, TypeKind}; use kclvm_ast::ast; use kclvm_ast::pos::GetPos; -use kclvm_error::Position; +use kclvm_error::diagnostic::Range; impl<'ctx> Resolver<'ctx> { /// Do loop type check including quant and comp for expression. @@ -14,7 +14,7 @@ impl<'ctx> Resolver<'ctx> { first_var_name: Option, second_var_name: Option, iter_ty: Rc, - iter_range: (Position, Position), + iter_range: Range, ) { let types = match &iter_ty.kind { TypeKind::Union(types) => types.clone(), diff --git a/kclvm/sema/src/resolver/mod.rs b/kclvm/sema/src/resolver/mod.rs index 122b389a6..a104bf53b 100644 --- a/kclvm/sema/src/resolver/mod.rs +++ b/kclvm/sema/src/resolver/mod.rs @@ -19,6 +19,7 @@ mod var; mod tests; use indexmap::IndexMap; +use kclvm_error::diagnostic::Range; use std::{cell::RefCell, rc::Rc}; use crate::lint::{CombinedLintPass, Linter}; @@ -118,7 +119,7 @@ pub struct Context { /// Import pkgpath and name pub import_names: IndexMap>, /// Global names at top level of the program. - pub global_names: IndexMap>, + pub global_names: IndexMap>, /// Are we resolving the left value. pub l_value: bool, /// Are we resolving the statement start position. diff --git a/kclvm/sema/src/resolver/scope.rs b/kclvm/sema/src/resolver/scope.rs index 1d3821440..ab1d13ff9 100644 --- a/kclvm/sema/src/resolver/scope.rs +++ b/kclvm/sema/src/resolver/scope.rs @@ -2,6 +2,7 @@ use anyhow::bail; use compiler_base_session::Session; use indexmap::{IndexMap, IndexSet}; use kclvm_ast::{ast, MAIN_PKG}; +use kclvm_error::diagnostic::Range; use kclvm_error::{Handler, Level}; use std::sync::Arc; use std::{ @@ -50,7 +51,7 @@ impl ContainsPos for ScopeObject { } impl GetPos for ScopeObject { - fn get_span_pos(&self) -> (Position, Position) { + fn get_span_pos(&self) -> Range { (self.start.clone(), self.end.clone()) } fn get_pos(&self) -> Position { @@ -399,7 +400,7 @@ impl<'ctx> Resolver<'ctx> { /// Lookup type from the scope by name, if not found, emit a compile error and /// return the any type. - pub fn lookup_type_from_scope(&mut self, name: &str, range: (Position, Position)) -> Rc { + pub fn lookup_type_from_scope(&mut self, name: &str, range: Range) -> Rc { match self.find_type_in_scope(name) { Some(ty) => ty, None => { @@ -413,7 +414,7 @@ impl<'ctx> Resolver<'ctx> { } /// Set type to the scope exited object, if not found, emit a compile error. - pub fn set_type_to_scope(&mut self, name: &str, ty: Rc, range: (Position, Position)) { + pub fn set_type_to_scope(&mut self, name: &str, ty: Rc, range: Range) { let mut scope = self.scope.borrow_mut(); match scope.elems.get_mut(name) { Some(obj) => { diff --git a/kclvm/sema/src/resolver/ty.rs b/kclvm/sema/src/resolver/ty.rs index 1a45ed041..e9c754f62 100644 --- a/kclvm/sema/src/resolver/ty.rs +++ b/kclvm/sema/src/resolver/ty.rs @@ -6,6 +6,7 @@ use crate::ty::{assignable_to, SchemaType, Type, TypeKind}; use indexmap::IndexMap; use kclvm_ast::ast; use kclvm_ast::pos::GetPos; +use kclvm_error::diagnostic::Range; use kclvm_error::*; use super::node::ResolvedResult; @@ -56,11 +57,7 @@ impl<'ctx> Resolver<'ctx> { } /// Parse the type string with the scope, if parse_ty returns a Named type(schema type or type alias), /// found it from the scope. - pub fn parse_ty_with_scope( - &mut self, - ty: &ast::Type, - range: (Position, Position), - ) -> ResolvedResult { + pub fn parse_ty_with_scope(&mut self, ty: &ast::Type, range: Range) -> ResolvedResult { let ty: Rc = Rc::new(ty.clone().into()); // If a named type, find it from scope to get the specific type let ret_ty = self.upgrade_named_ty_with_scope(ty.clone(), &range); @@ -71,11 +68,7 @@ impl<'ctx> Resolver<'ctx> { ret_ty } - pub fn parse_ty_str_with_scope( - &mut self, - ty_str: &str, - range: (Position, Position), - ) -> ResolvedResult { + pub fn parse_ty_str_with_scope(&mut self, ty_str: &str, range: Range) -> ResolvedResult { let ty: Rc = parse_type_str(ty_str); // If a named type, find it from scope to get the specific type let ret_ty = self.upgrade_named_ty_with_scope(ty, &range); @@ -96,8 +89,8 @@ impl<'ctx> Resolver<'ctx> { &mut self, ty: Rc, expected_ty: Rc, - range: (Position, Position), - expected_pos: Option<(Position, Position)>, + range: Range, + expected_pos: Option, ) { if !self.check_type(ty.clone(), expected_ty.clone(), &range) { let mut msgs = vec![Message { @@ -125,12 +118,7 @@ impl<'ctx> Resolver<'ctx> { /// The check type main function, returns a boolean result. #[inline] - pub fn check_type( - &mut self, - ty: Rc, - expected_ty: Rc, - range: &(Position, Position), - ) -> bool { + pub fn check_type(&mut self, ty: Rc, expected_ty: Rc, range: &Range) -> bool { match (&ty.kind, &expected_ty.kind) { (TypeKind::List(item_ty), TypeKind::List(expected_item_ty)) => { self.check_type(item_ty.clone(), expected_item_ty.clone(), range) @@ -159,7 +147,7 @@ impl<'ctx> Resolver<'ctx> { key_ty: Rc, val_ty: Rc, schema_ty: &SchemaType, - range: &(Position, Position), + range: &Range, ) -> bool { if let Some(index_signature) = &schema_ty.index_signature { if !assignable_to(val_ty.clone(), index_signature.val_ty.clone()) { @@ -182,11 +170,7 @@ impl<'ctx> Resolver<'ctx> { } } - fn upgrade_named_ty_with_scope( - &mut self, - ty: Rc, - range: &(Position, Position), - ) -> ResolvedResult { + fn upgrade_named_ty_with_scope(&mut self, ty: Rc, range: &Range) -> ResolvedResult { match &ty.kind { TypeKind::List(item_ty) => { Type::list_ref(self.upgrade_named_ty_with_scope(item_ty.clone(), range)) diff --git a/kclvm/sema/src/resolver/var.rs b/kclvm/sema/src/resolver/var.rs index 536f56396..028ace36c 100644 --- a/kclvm/sema/src/resolver/var.rs +++ b/kclvm/sema/src/resolver/var.rs @@ -1,6 +1,7 @@ use crate::resolver::Resolver; use crate::ty::TypeKind; use indexmap::IndexMap; +use kclvm_error::diagnostic::Range; use kclvm_error::*; use super::node::ResolvedResult; @@ -8,12 +9,7 @@ use super::scope::{ScopeObject, ScopeObjectKind}; impl<'ctx> Resolver<'ctx> { /// Resolve variables. - pub fn resolve_var( - &mut self, - names: &[String], - pkgpath: &str, - range: (Position, Position), - ) -> ResolvedResult { + pub fn resolve_var(&mut self, names: &[String], pkgpath: &str, range: Range) -> ResolvedResult { if !pkgpath.is_empty() && self.ctx.l_value { self.handler.add_compile_error( "only schema and dict object can be updated attribute", @@ -122,7 +118,7 @@ impl<'ctx> Resolver<'ctx> { } /// Resolve an unique key in the current package. - pub(crate) fn resolve_unique_key(&mut self, name: &str, range: &(Position, Position)) { + pub(crate) fn resolve_unique_key(&mut self, name: &str, range: &Range) { if !self.contains_global_name(name) && self.scope_level == 0 { self.insert_global_name(name, range); } else { @@ -145,7 +141,7 @@ impl<'ctx> Resolver<'ctx> { } /// Insert global name in the current package. - pub(crate) fn insert_global_name(&mut self, name: &str, range: &(Position, Position)) { + pub(crate) fn insert_global_name(&mut self, name: &str, range: &Range) { match self.ctx.global_names.get_mut(&self.ctx.pkgpath) { Some(mapping) => { mapping.insert(name.to_string(), range.clone()); @@ -169,7 +165,7 @@ impl<'ctx> Resolver<'ctx> { } /// Get global name position in the current package. - pub(crate) fn get_global_name_pos(&mut self, name: &str) -> Option<&(Position, Position)> { + pub(crate) fn get_global_name_pos(&mut self, name: &str) -> Option<&Range> { match self.ctx.global_names.get_mut(&self.ctx.pkgpath) { Some(mapping) => mapping.get(name), None => None,