From 9a3e0ec34b27f3294e9ec3e36b93d7897e86e226 Mon Sep 17 00:00:00 2001 From: He1pa <56333845+He1pa@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:19:14 +0800 Subject: [PATCH] feat: add some position info about import stmt node (#974) * feat: add some position info about import stmt node Signed-off-by: he1pa <18012015693@163.com> * add invalid import asname test case Signed-off-by: he1pa <18012015693@163.com> --------- Signed-off-by: he1pa <18012015693@163.com> --- kclvm/ast/src/ast.rs | 4 +- kclvm/ast_pretty/src/node.rs | 4 +- kclvm/compiler/src/codegen/llvm/node.rs | 12 ++--- kclvm/parser/src/lib.rs | 12 ++--- kclvm/parser/src/parser/stmt.rs | 20 ++++++-- kclvm/parser/src/tests/error_recovery.rs | 1 + ...ts__error_recovery__import_recovery_0.snap | 37 +++++++++++++++ ...rror_recovery__import_stmt_recovery_0.snap | 9 +++- ...rror_recovery__import_stmt_recovery_1.snap | 9 +++- ...rror_recovery__import_stmt_recovery_2.snap | 9 +++- ...rror_recovery__import_stmt_recovery_3.snap | 9 +++- ...rror_recovery__import_stmt_recovery_4.snap | 9 +++- ...rror_recovery__import_stmt_recovery_5.snap | 18 ++++++- ...rror_recovery__import_stmt_recovery_6.snap | 18 ++++++- .../kclvm_parser__tests__file__import_1.snap | 2 +- kclvm/query/src/override.rs | 6 +-- kclvm/sema/src/advanced_resolver/node.rs | 4 +- kclvm/sema/src/lint/lints_def.rs | 4 +- kclvm/sema/src/namer/node.rs | 2 +- kclvm/sema/src/pre_process/identifier.rs | 8 ++-- kclvm/sema/src/pre_process/mod.rs | 3 +- kclvm/sema/src/resolver/import.rs | 47 ++++++++++--------- kclvm/sema/src/resolver/scope.rs | 2 +- kclvm/tools/src/LSP/src/completion.rs | 2 +- 24 files changed, 185 insertions(+), 66 deletions(-) create mode 100644 kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_recovery_0.snap diff --git a/kclvm/ast/src/ast.rs b/kclvm/ast/src/ast.rs index 2dc80a7f3..d5ff6d34b 100644 --- a/kclvm/ast/src/ast.rs +++ b/kclvm/ast/src/ast.rs @@ -482,10 +482,10 @@ pub struct IfStmt { #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct ImportStmt { /// `path` is the import path, if 'import a.b.c' in kcl, `path` is a.b.c - pub path: String, + pub path: Node, pub rawpath: String, pub name: String, - pub asname: Option, + pub asname: Option>, /// `pkg_name` means the name of the package that the current import statement indexs to. /// /// 1. If the current import statement indexs to the kcl plugins, kcl builtin methods or the internal kcl packages, diff --git a/kclvm/ast_pretty/src/node.rs b/kclvm/ast_pretty/src/node.rs index 23604fc2d..2901fdf8a 100644 --- a/kclvm/ast_pretty/src/node.rs +++ b/kclvm/ast_pretty/src/node.rs @@ -143,10 +143,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> { fn walk_import_stmt(&mut self, import_stmt: &'ctx ast::ImportStmt) -> Self::Result { self.write("import "); - self.write(&import_stmt.path); + self.write(&import_stmt.path.node); if let Some(as_name) = &import_stmt.asname { self.write(" as "); - self.write(as_name); + self.write(&as_name.node); } self.write_newline_without_fill(); } diff --git a/kclvm/compiler/src/codegen/llvm/node.rs b/kclvm/compiler/src/codegen/llvm/node.rs index 3d4ac8375..ef50b25b8 100644 --- a/kclvm/compiler/src/codegen/llvm/node.rs +++ b/kclvm/compiler/src/codegen/llvm/node.rs @@ -288,7 +288,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> { } fn walk_import_stmt(&self, import_stmt: &'ctx ast::ImportStmt) -> Self::Result { check_backtrack_stop!(self); - let pkgpath = import_stmt.path.as_str(); + let pkgpath = import_stmt.path.node.as_str(); { let imported = self.imported.borrow_mut(); if imported.contains(pkgpath) { @@ -302,10 +302,10 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> { // Nothing to do on the builtin system module import because the check has been done. return self.ok_result(); } else { - let pkgpath = format!("{}{}", PKG_PATH_PREFIX, import_stmt.path); + let pkgpath = format!("{}{}", PKG_PATH_PREFIX, import_stmt.path.node); self.pkgpath_stack.borrow_mut().push(pkgpath); - let pkgpath = format!("{}{}", PKG_PATH_PREFIX, import_stmt.path); - let has_pkgpath = self.program.pkgs.contains_key(&import_stmt.path); + let pkgpath = format!("{}{}", PKG_PATH_PREFIX, import_stmt.path.node); + let has_pkgpath = self.program.pkgs.contains_key(&import_stmt.path.node); let func_before_block = if self.no_link { if has_pkgpath { let func_before_block = self.append_block(""); @@ -346,7 +346,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> { for ast_module in self .program .pkgs - .get(&import_stmt.path) + .get(&import_stmt.path.node) .expect(kcl_error::INTERNAL_ERROR_MSG) { { @@ -362,7 +362,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> { for ast_module in self .program .pkgs - .get(&import_stmt.path) + .get(&import_stmt.path.node) .expect(kcl_error::INTERNAL_ERROR_MSG) { { diff --git a/kclvm/parser/src/lib.rs b/kclvm/parser/src/lib.rs index b414fcf7a..c2efdd6fa 100644 --- a/kclvm/parser/src/lib.rs +++ b/kclvm/parser/src/lib.rs @@ -478,22 +478,22 @@ impl Loader { for stmt in &mut m.body { let pos = stmt.pos().clone(); if let ast::Stmt::Import(ref mut import_spec) = &mut stmt.node { - import_spec.path = kclvm_config::vfs::fix_import_path( + import_spec.path.node = kclvm_config::vfs::fix_import_path( pkgroot, &m.filename, - import_spec.path.as_str(), + import_spec.path.node.as_str(), ); import_spec.pkg_name = pkg_name.to_string(); // Load the import package source code and compile. if let Some(pkg_info) = self.load_package( &pkgroot, pkg_name.to_string(), - import_spec.path.to_string(), + import_spec.path.node.to_string(), pos.into(), pkgs, )? { // Add the external package name as prefix of the [`kclvm_ast::ImportStmt`]'s member [`path`]. - import_spec.path = pkg_info.pkg_path.to_string(); + import_spec.path.node = pkg_info.pkg_path.to_string(); import_spec.pkg_name = pkg_info.pkg_name } } @@ -505,10 +505,10 @@ impl Loader { fn fix_rel_import_path(&mut self, pkgroot: &str, m: &mut ast::Module) { for stmt in &mut m.body { if let ast::Stmt::Import(ref mut import_spec) = &mut stmt.node { - import_spec.path = kclvm_config::vfs::fix_import_path( + import_spec.path.node = kclvm_config::vfs::fix_import_path( pkgroot, &m.filename, - import_spec.path.as_str(), + import_spec.path.node.as_str(), ); } } diff --git a/kclvm/parser/src/parser/stmt.rs b/kclvm/parser/src/parser/stmt.rs index 34545704c..665f0705a 100644 --- a/kclvm/parser/src/parser/stmt.rs +++ b/kclvm/parser/src/parser/stmt.rs @@ -421,6 +421,7 @@ impl<'a> Parser<'a> { fn parse_import_stmt(&mut self) -> NodeRef { let token = self.token; self.bump_keyword(kw::Import); + let dot_name_token = self.token; let mut leading_dot = Vec::new(); while let TokenKind::DotDotDot = self.token.kind { @@ -432,10 +433,19 @@ impl<'a> Parser<'a> { self.bump_token(TokenKind::Dot); } let dot_name = self.parse_identifier().node; + let dot_name_end_token = self.prev_token; + let asname = if self.token.is_keyword(kw::As) { self.bump_keyword(kw::As); let ident = self.parse_identifier().node; - Some(ident.get_names().join(".")) + match ident.names.len() { + 1 => Some(ident.names[0].clone()), + _ => { + self.sess + .struct_span_error("Invalid import asname", self.token.span); + None + } + } } else { None }; @@ -444,16 +454,20 @@ impl<'a> Parser<'a> { path.push_str(dot_name.get_names().join(".").as_str()); let rawpath = path.clone(); + let path_node = Node::node_with_pos( + path, + self.token_span_pos(dot_name_token, dot_name_end_token), + ); let name = if let Some(as_name_value) = asname.clone() { - as_name_value + as_name_value.node } else { dot_name.get_names().last().unwrap().clone() }; let t = node_ref!( Stmt::Import(ImportStmt { - path, + path: path_node, rawpath, name, asname, diff --git a/kclvm/parser/src/tests/error_recovery.rs b/kclvm/parser/src/tests/error_recovery.rs index afd73ca3c..dbfe1a163 100644 --- a/kclvm/parser/src/tests/error_recovery.rs +++ b/kclvm/parser/src/tests/error_recovery.rs @@ -324,3 +324,4 @@ parse_module_snapshot! { fn_ty_annotation_recovery_20, r#"a:(str|int) -> i"#} parse_module_snapshot! { fn_ty_annotation_recovery_21, r#"a:(str|int, int) -> i"#} parse_module_snapshot! { fn_ty_annotation_recovery_22, r#"a:(str|int, int|"#} parse_module_snapshot! { fn_ty_annotation_recovery_23, r#"a:(str|int, int|) ->"#} +parse_module_snapshot! { import_recovery_0, r#"import json as j.a"#} diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_recovery_0.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_recovery_0.snap new file mode 100644 index 000000000..2e07fd5ae --- /dev/null +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_recovery_0.snap @@ -0,0 +1,37 @@ +--- +source: parser/src/tests/error_recovery.rs +expression: "crate::tests::parsing_module_string(r#\"import json as j.a\"#)" +--- +Module { + filename: "", + pkg: "", + doc: None, + name: "", + body: [ + Node { + node: Import( + ImportStmt { + path: Node { + node: "json", + filename: "", + line: 1, + column: 7, + end_line: 1, + end_column: 11, + }, + rawpath: "json", + name: "json", + asname: None, + pkg_name: "", + }, + ), + filename: "", + line: 1, + column: 0, + end_line: 1, + end_column: 18, + }, + ], + comments: [], +} + diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_0.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_0.snap index 46f620c0d..d8bee29dd 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_0.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_0.snap @@ -11,7 +11,14 @@ Module { Node { node: Import( ImportStmt { - path: "", + path: Node { + node: "", + filename: "", + line: 1, + column: 6, + end_line: 1, + end_column: 6, + }, rawpath: "", name: "", asname: None, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_1.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_1.snap index 7e68f5708..0fcc1117b 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_1.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_1.snap @@ -11,7 +11,14 @@ Module { Node { node: Import( ImportStmt { - path: "", + path: Node { + node: "", + filename: "", + line: 1, + column: 7, + end_line: 1, + end_column: 6, + }, rawpath: "", name: "", asname: None, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_2.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_2.snap index 94919fb6b..731f2e915 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_2.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_2.snap @@ -11,7 +11,14 @@ Module { Node { node: Import( ImportStmt { - path: "pkg_path.", + path: Node { + node: "pkg_path.", + filename: "", + line: 1, + column: 7, + end_line: 1, + end_column: 16, + }, rawpath: "pkg_path.", name: "", asname: None, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_3.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_3.snap index 5fe779980..ebbc792ed 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_3.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_3.snap @@ -11,7 +11,14 @@ Module { Node { node: Import( ImportStmt { - path: "pkg_path", + path: Node { + node: "pkg_path", + filename: "", + line: 1, + column: 7, + end_line: 1, + end_column: 15, + }, rawpath: "pkg_path", name: "pkg_path", asname: None, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_4.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_4.snap index c4f4c1dd3..657f42f79 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_4.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_4.snap @@ -11,7 +11,14 @@ Module { Node { node: Import( ImportStmt { - path: ".pkg_path.", + path: Node { + node: ".pkg_path.", + filename: "", + line: 1, + column: 7, + end_line: 1, + end_column: 17, + }, rawpath: ".pkg_path.", name: "", asname: None, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_5.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_5.snap index 44151fe8e..714829607 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_5.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_5.snap @@ -11,11 +11,25 @@ Module { Node { node: Import( ImportStmt { - path: "pkg_path", + path: Node { + node: "pkg_path", + filename: "", + line: 1, + column: 7, + end_line: 1, + end_column: 15, + }, rawpath: "pkg_path", name: "", asname: Some( - "", + Node { + node: "", + filename: "", + line: 1, + column: 19, + end_line: 1, + end_column: 19, + }, ), pkg_name: "", }, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_6.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_6.snap index 8b3a860c4..dd46cb3d0 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_6.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__import_stmt_recovery_6.snap @@ -11,11 +11,25 @@ Module { Node { node: Import( ImportStmt { - path: "pkg_path", + path: Node { + node: "pkg_path", + filename: "", + line: 1, + column: 7, + end_line: 1, + end_column: 15, + }, rawpath: "pkg_path", name: "", asname: Some( - "", + Node { + node: "", + filename: "", + line: 1, + column: 19, + end_line: 1, + end_column: 19, + }, ), pkg_name: "", }, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__file__import_1.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__file__import_1.snap index d102fb90e..6697c392f 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__file__import_1.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__file__import_1.snap @@ -2,4 +2,4 @@ source: parser/src/tests/file.rs expression: "crate::tests::parsing_file_string(\"testdata/import-01.k\")" --- -{"filename":"import-01.k","pkg":"__main__","doc":null,"name":"__main__","body":[{"node":{"Import":{"path":"a1","rawpath":"a1","name":"a1","asname":null,"pkg_name":""}},"filename":"import-01.k","line":1,"column":0,"end_line":1,"end_column":9},{"node":{"Import":{"path":"a2","rawpath":"a2","name":"a2_pkg","asname":"a2_pkg","pkg_name":""}},"filename":"import-01.k","line":3,"column":0,"end_line":3,"end_column":19},{"node":{"Import":{"path":"subpkg.b1.c1","rawpath":"subpkg.b1.c1","name":"c1","asname":null,"pkg_name":""}},"filename":"import-01.k","line":5,"column":0,"end_line":5,"end_column":19},{"node":{"Import":{"path":".a3","rawpath":".a3","name":"a3","asname":null,"pkg_name":""}},"filename":"import-01.k","line":7,"column":0,"end_line":7,"end_column":10}],"comments":[]} +{"filename":"import-01.k","pkg":"__main__","doc":null,"name":"__main__","body":[{"node":{"Import":{"path":{"node":"a1","filename":"import-01.k","line":1,"column":7,"end_line":1,"end_column":9},"rawpath":"a1","name":"a1","asname":null,"pkg_name":""}},"filename":"import-01.k","line":1,"column":0,"end_line":1,"end_column":9},{"node":{"Import":{"path":{"node":"a2","filename":"import-01.k","line":3,"column":7,"end_line":3,"end_column":9},"rawpath":"a2","name":"a2_pkg","asname":{"node":"a2_pkg","filename":"import-01.k","line":3,"column":13,"end_line":3,"end_column":19},"pkg_name":""}},"filename":"import-01.k","line":3,"column":0,"end_line":3,"end_column":19},{"node":{"Import":{"path":{"node":"subpkg.b1.c1","filename":"import-01.k","line":5,"column":7,"end_line":5,"end_column":19},"rawpath":"subpkg.b1.c1","name":"c1","asname":null,"pkg_name":""}},"filename":"import-01.k","line":5,"column":0,"end_line":5,"end_column":19},{"node":{"Import":{"path":{"node":".a3","filename":"import-01.k","line":7,"column":7,"end_line":7,"end_column":10},"rawpath":".a3","name":"a3","asname":null,"pkg_name":""}},"filename":"import-01.k","line":7,"column":0,"end_line":7,"end_column":10}],"comments":[]} diff --git a/kclvm/query/src/override.rs b/kclvm/query/src/override.rs index 9a3c284bd..1c3b36011 100644 --- a/kclvm/query/src/override.rs +++ b/kclvm/query/src/override.rs @@ -190,9 +190,9 @@ fn apply_import_paths_on_module(m: &mut ast::Module, import_paths: &[String]) -> for stmt in &m.body { if let ast::Stmt::Import(import_stmt) = &stmt.node { if let Some(asname) = &import_stmt.asname { - exist_import_set.insert(format!("{} as {}", import_stmt.path, asname)); + exist_import_set.insert(format!("{} as {}", import_stmt.path.node, asname.node)); } else { - exist_import_set.insert(import_stmt.path.to_string()); + exist_import_set.insert(import_stmt.path.node.to_string()); } } } @@ -206,7 +206,7 @@ fn apply_import_paths_on_module(m: &mut ast::Module, import_paths: &[String]) -> .last() .ok_or_else(|| anyhow!("Invalid import path {}", path))?; let import_node = ast::ImportStmt { - path: path.to_string(), + path: ast::Node::dummy_node(path.to_string()), rawpath: "".to_string(), name: name.to_string(), asname: None, diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 769f9e473..efe2f239a 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -119,11 +119,11 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { let ast_id = self.ctx.cur_node.clone(); let (start_pos, end_pos) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); let mut unresolved = - UnresolvedSymbol::new(import_stmt.path.clone(), start_pos, end_pos, None); + UnresolvedSymbol::new(import_stmt.path.node.clone(), start_pos, end_pos, None); let package_symbol = self .gs .get_symbols() - .get_symbol_by_fully_qualified_name(&import_stmt.path)?; + .get_symbol_by_fully_qualified_name(&import_stmt.path.node)?; unresolved.def = Some(package_symbol); let unresolved_ref = self .gs diff --git a/kclvm/sema/src/lint/lints_def.rs b/kclvm/sema/src/lint/lints_def.rs index cc1386a68..e4292ed07 100644 --- a/kclvm/sema/src/lint/lints_def.rs +++ b/kclvm/sema/src/lint/lints_def.rs @@ -154,7 +154,7 @@ impl LintPass for ReImport { let mut import_names = IndexSet::::new(); for stmt in &module.body { if let ast::Stmt::Import(import_stmt) = &stmt.node { - if import_names.contains(&import_stmt.path) { + if import_names.contains(&import_stmt.path.node) { handler.add_warning( WarningKind::ReimportWarning, &[Message { @@ -169,7 +169,7 @@ impl LintPass for ReImport { }], ); } else { - import_names.insert(import_stmt.path.clone()); + import_names.insert(import_stmt.path.node.clone()); } } } diff --git a/kclvm/sema/src/namer/node.rs b/kclvm/sema/src/namer/node.rs index c2d27f377..5c0b33e19 100644 --- a/kclvm/sema/src/namer/node.rs +++ b/kclvm/sema/src/namer/node.rs @@ -160,7 +160,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Namer<'ctx> { .unwrap() .add_import_info(ImportInfo::new( import_stmt.name.clone(), - import_stmt.path.clone(), + import_stmt.path.node.clone(), )); None diff --git a/kclvm/sema/src/pre_process/identifier.rs b/kclvm/sema/src/pre_process/identifier.rs index 4f25bcf1f..89855e9c6 100644 --- a/kclvm/sema/src/pre_process/identifier.rs +++ b/kclvm/sema/src/pre_process/identifier.rs @@ -200,11 +200,11 @@ impl<'ctx> MutSelfMutWalker<'ctx> for RawIdentifierTransformer { walk_if_mut!(self, walk_identifier, rule_stmt.for_host_name); } fn walk_import_stmt(&mut self, import_stmt: &'ctx mut ast::ImportStmt) { - if let Some(name) = import_stmt.asname.as_mut() { - *name = remove_raw_ident_prefix(name); + if let Some(name) = &mut import_stmt.asname { + name.node = remove_raw_ident_prefix(&name.node); } import_stmt.name = remove_raw_ident_prefix(&import_stmt.name); - import_stmt.path = remove_raw_ident_prefix(&import_stmt.path); + import_stmt.path.node = remove_raw_ident_prefix(&import_stmt.path.node); } } @@ -218,7 +218,7 @@ pub fn fix_qualified_identifier<'ctx>( // 0. init import names. for stmt in &module.body { if let ast::Stmt::Import(import_stmt) = &stmt.node { - import_names.insert(import_stmt.name.clone(), import_stmt.path.clone()); + import_names.insert(import_stmt.name.clone(), import_stmt.path.node.clone()); } } // 1. fix_global_ident diff --git a/kclvm/sema/src/pre_process/mod.rs b/kclvm/sema/src/pre_process/mod.rs index d37a803ff..49d6ac928 100644 --- a/kclvm/sema/src/pre_process/mod.rs +++ b/kclvm/sema/src/pre_process/mod.rs @@ -22,7 +22,8 @@ pub fn pre_process_program(program: &mut ast::Program, opts: &Options) { for module in modules.iter_mut() { for stmt in &module.body { if let ast::Stmt::Import(import_stmt) = &stmt.node { - import_names.insert(import_stmt.name.clone(), import_stmt.path.clone()); + import_names + .insert(import_stmt.name.clone(), import_stmt.path.node.clone()); } } } diff --git a/kclvm/sema/src/resolver/import.rs b/kclvm/sema/src/resolver/import.rs index d9856fb51..0bf2d3717 100644 --- a/kclvm/sema/src/resolver/import.rs +++ b/kclvm/sema/src/resolver/import.rs @@ -25,7 +25,7 @@ impl<'ctx> Resolver<'ctx> { for m in modules { for stmt in &m.body { if let ast::Stmt::Import(import_stmt) = &stmt.node { - let pkgpath = &import_stmt.path; + let pkgpath = &import_stmt.path.node; // System module. if STANDARD_SYSTEM_MODULES.contains(&pkgpath.as_str()) { continue; @@ -114,7 +114,7 @@ impl<'ctx> Resolver<'ctx> { // 'import sub' and 'import sub' will not raise this error. // 'import sub as s' and 'import sub as s' will not raise this error. if let Some(path) = mapping.get(&import_stmt.name) { - if path != &import_stmt.path { + if path != &import_stmt.path.node { self.handler.add_compile_error( &format!( "the name '{}' is defined multiple times, '{}' must be defined only once", @@ -126,14 +126,14 @@ impl<'ctx> Resolver<'ctx> { } mapping.insert( import_stmt.name.to_string(), - import_stmt.path.to_string(), + import_stmt.path.node.to_string(), ); } None => { let mut mapping = IndexMap::default(); mapping.insert( import_stmt.name.to_string(), - import_stmt.path.to_string(), + import_stmt.path.node.to_string(), ); self.ctx .import_names @@ -141,7 +141,7 @@ impl<'ctx> Resolver<'ctx> { } } let mut scope = self.scope.borrow_mut(); - let is_user_module = match scope.elems.get(&import_stmt.path) { + let is_user_module = match scope.elems.get(&import_stmt.path.node) { Some(scope_obj) => { let mut obj = scope_obj.borrow_mut(); match &mut obj.kind { @@ -173,27 +173,30 @@ impl<'ctx> Resolver<'ctx> { } } None => { - let kind = - if import_stmt.path.starts_with(PLUGIN_MODULE_PREFIX) { - ModuleKind::Plugin - } else if STANDARD_SYSTEM_MODULES - .contains(&import_stmt.path.as_str()) - { - ModuleKind::System - } else { - ModuleKind::User - }; + let kind = if import_stmt + .path + .node + .starts_with(PLUGIN_MODULE_PREFIX) + { + ModuleKind::Plugin + } else if STANDARD_SYSTEM_MODULES + .contains(&import_stmt.path.node.as_str()) + { + ModuleKind::System + } else { + ModuleKind::User + }; let ty = Type::module( - &import_stmt.path, + &import_stmt.path.node, &[self.ctx.filename.clone()], kind.clone(), ); let (start, end) = stmt.get_span_pos(); scope.elems.insert( - import_stmt.path.to_string(), + import_stmt.path.node.to_string(), Rc::new(RefCell::new(ScopeObject { - name: import_stmt.path.to_string(), + name: import_stmt.path.node.to_string(), start, end, ty: Arc::new(ty), @@ -215,19 +218,19 @@ impl<'ctx> Resolver<'ctx> { let current_filename = self.ctx.filename.clone(); self.ctx .ty_ctx - .add_dependencies(&self.ctx.pkgpath, &import_stmt.path); + .add_dependencies(&self.ctx.pkgpath, &import_stmt.path.node); if self.ctx.ty_ctx.is_cyclic() { self.handler.add_compile_error( &format!( "There is a circular import reference between module {} and {}", - self.ctx.pkgpath, import_stmt.path, + self.ctx.pkgpath, import_stmt.path.node, ), stmt.get_span_pos(), ); } // Switch pkgpath context - if !self.scope_map.contains_key(&import_stmt.path) { - self.check(&import_stmt.path); + if !self.scope_map.contains_key(&import_stmt.path.node) { + self.check(&import_stmt.path.node); } // Restore the current context self.change_package_context(¤t_pkgpath, ¤t_filename); diff --git a/kclvm/sema/src/resolver/scope.rs b/kclvm/sema/src/resolver/scope.rs index d342235bb..65d853283 100644 --- a/kclvm/sema/src/resolver/scope.rs +++ b/kclvm/sema/src/resolver/scope.rs @@ -552,7 +552,7 @@ impl DependencyGraph { if let Some(pkgpaths) = self.module_map.get(&module_file) { for stmt in &new_module.body { if let ast::Stmt::Import(import_stmt) = &stmt.node { - let parent_pkg = &import_stmt.path; + let parent_pkg = &import_stmt.path.node; if let Some(parent_node) = self.node_map.get_mut(parent_pkg) { parent_node.children.insert(new_module.filename.clone()); } diff --git a/kclvm/tools/src/LSP/src/completion.rs b/kclvm/tools/src/LSP/src/completion.rs index 06b598c36..4d3091bd8 100644 --- a/kclvm/tools/src/LSP/src/completion.rs +++ b/kclvm/tools/src/LSP/src/completion.rs @@ -541,7 +541,7 @@ fn completion_import( program: &Program, ) -> Option { let mut items: IndexSet = IndexSet::new(); - let pkgpath = &stmt.path; + let pkgpath = &stmt.path.node; let mut real_path = Path::new(&program.root).join(pkgpath.replace('.', std::path::MAIN_SEPARATOR_STR)); if !real_path.exists() {