diff --git a/kclvm/sema/src/advanced_resolver/mod.rs b/kclvm/sema/src/advanced_resolver/mod.rs index 759e80cac..ad596b66a 100644 --- a/kclvm/sema/src/advanced_resolver/mod.rs +++ b/kclvm/sema/src/advanced_resolver/mod.rs @@ -34,7 +34,7 @@ └─────────────────────┘ */ -use indexmap::IndexMap; +use indexmap::{IndexMap, IndexSet}; use kclvm_error::Position; use crate::{ @@ -112,7 +112,11 @@ impl<'ctx> AdvancedResolver<'ctx> { if !advanced_resolver.ctx.scopes.is_empty() { advanced_resolver.ctx.scopes.clear(); } - advanced_resolver.enter_root_scope(name.clone(), pkg_info.pkg_filepath.clone()); + advanced_resolver.enter_root_scope( + name.clone(), + pkg_info.pkg_filepath.clone(), + pkg_info.kfile_paths.clone(), + ); for module in modules.iter() { advanced_resolver.ctx.current_filename = Some(module.filename.clone()); advanced_resolver.walk_module(module); @@ -125,14 +129,19 @@ impl<'ctx> AdvancedResolver<'ctx> { advanced_resolver.gs } - fn enter_root_scope(&mut self, pkgpath: String, filename: String) { + fn enter_root_scope( + &mut self, + pkgpath: String, + filename: String, + kfile_paths: IndexSet, + ) { let package_ref = self .gs .get_symbols_mut() .get_symbol_by_fully_qualified_name(&pkgpath) .unwrap(); - let root_scope = RootSymbolScope::new(pkgpath, filename, package_ref); + let root_scope = RootSymbolScope::new(pkgpath, filename, package_ref, kfile_paths); let scope_ref = self.gs.get_scopes_mut().alloc_root_scope(root_scope); self.ctx.scopes.push(scope_ref); } diff --git a/kclvm/sema/src/core/scope.rs b/kclvm/sema/src/core/scope.rs index 864c57365..3d3ade990 100644 --- a/kclvm/sema/src/core/scope.rs +++ b/kclvm/sema/src/core/scope.rs @@ -1,6 +1,6 @@ -use std::{collections::HashMap, path::Path}; +use std::collections::HashMap; -use indexmap::IndexMap; +use indexmap::{IndexMap, IndexSet}; use kclvm_error::Position; use crate::core::symbol::SymbolRef; @@ -145,6 +145,8 @@ pub struct RootSymbolScope { pub(crate) filename: String, + pub(crate) kfile_path: IndexSet, + /// PackageSymbol of this scope pub(crate) owner: SymbolRef, @@ -173,25 +175,7 @@ impl Scope for RootSymbolScope { } fn contains_pos(&self, pos: &Position) -> bool { - let real_pkg_path = if self.filename.ends_with(".k") { - Path::new(self.filename.strip_suffix(".k").unwrap()) - } else { - Path::new(&self.filename) - }; - let real_pos_path = if pos.filename.ends_with(".k") { - Path::new(pos.filename.strip_suffix(".k").unwrap()) - } else { - Path::new(&pos.filename) - }; - if real_pkg_path != real_pos_path { - if let Some(parent) = real_pos_path.parent() { - real_pkg_path == parent - } else { - false - } - } else { - true - } + self.kfile_path.contains(&pos.filename) } fn get_owner(&self) -> Option { Some(self.owner) @@ -269,9 +253,15 @@ impl Scope for RootSymbolScope { } impl RootSymbolScope { - pub fn new(pkgpath: String, filename: String, owner: SymbolRef) -> Self { + pub fn new( + pkgpath: String, + filename: String, + owner: SymbolRef, + kfile_path: IndexSet, + ) -> Self { Self { pkgpath, + kfile_path, filename, owner, children: IndexMap::default(),