Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix advanced_resolver schema def scope. reomve duplicate scopes #1480

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,51 @@ impl<'ctx> AdvancedResolver<'ctx> {
self.ctx.scopes.push(scope_ref);
}

fn enter_schema_def_scope(
&mut self,
name: &str,
filepath: &str,
start: Position,
end: Position,
kind: LocalSymbolScopeKind,
) {
let parent = *self.ctx.scopes.last().unwrap();
let local_scope = LocalSymbolScope::new(parent, start, end, kind);
let fqn_name = format!("{filepath}.{name}");

let scope_ref = match self.gs.get_scopes().schema_scope_map.get(&fqn_name) {
Some(scope_ref) => scope_ref.clone(),
None => {
let scope_ref = self.gs.get_scopes_mut().alloc_local_scope(local_scope);
self.gs
.get_scopes_mut()
.schema_scope_map
.insert(fqn_name, scope_ref);

match parent.get_kind() {
ScopeKind::Root => {
self.gs
.get_scopes_mut()
.roots
.get_mut(parent.get_id())
.unwrap()
.add_child(filepath, scope_ref);
}
ScopeKind::Local => {
self.gs
.get_scopes_mut()
.locals
.get_mut(parent.get_id())
.unwrap()
.add_child(scope_ref);
}
}
scope_ref
}
};
self.ctx.scopes.push(scope_ref);
}

fn leave_scope(&mut self) {
self.ctx.scopes.pop();
}
Expand All @@ -244,6 +289,7 @@ mod tests {
use crate::namer::Namer;
use crate::resolver;

use kclvm_ast::MAIN_PKG;
use kclvm_error::Position;
use kclvm_parser::load_program;
use kclvm_parser::ParseSession;
Expand Down Expand Up @@ -1448,4 +1494,32 @@ mod tests {
assert_eq!(all_defs.len(), *def_num)
}
}

#[test]
fn test_schema_def_scope() {
let sess = Arc::new(ParseSession::default());

let path = "src/advanced_resolver/test_data/schema_def_scope.k"
.to_string()
.replace("/", &std::path::MAIN_SEPARATOR.to_string());
let mut program = load_program(sess.clone(), &[&path], None, None)
.unwrap()
.program;
let mut gs = GlobalState::default();
Namer::find_symbols(&program, &mut gs);
let node_ty_map = resolver::resolve_program(&mut program).node_ty_map;
AdvancedResolver::resolve_program(&program, &mut gs, node_ty_map).unwrap();
let main_pkg_root_scope = gs
.get_scopes()
.get_root_scope(MAIN_PKG.to_string())
.unwrap();
assert_eq!(
gs.get_scopes()
.get_scope(&main_pkg_root_scope)
.unwrap()
.get_children()
.len(),
2
);
}
}
3 changes: 2 additions & 1 deletion kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {

let mut last_end_pos = start.clone();

self.enter_local_scope(
self.enter_schema_def_scope(
&schema_ty.into_schema_type().name,
&self.ctx.current_filename.clone().unwrap(),
start,
end.clone(),
Expand Down
8 changes: 8 additions & 0 deletions kclvm/sema/src/advanced_resolver/test_data/schema_def_scope.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
schema Person:
name: str
age: int

p1 = Person {
name: "Alice"
age: 18
}
4 changes: 4 additions & 0 deletions kclvm/sema/src/core/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ impl ScopeRef {
pub struct ScopeData {
/// map pkgpath to root_scope
pub(crate) root_map: IndexMap<String, ScopeRef>,
/// map schema fully qualified name to schema local scope
pub(crate) schema_scope_map: IndexMap<String, ScopeRef>,
pub(crate) locals: generational_arena::Arena<LocalSymbolScope>,
pub(crate) roots: generational_arena::Arena<RootSymbolScope>,
}
Expand Down Expand Up @@ -208,6 +210,8 @@ impl ScopeData {
self.clear_scope_and_child(scope_ref);
self.roots.remove(scope_ref.get_id());
}
self.schema_scope_map
.retain(|key, _| !key.starts_with(invalidate_pkg));
}
}

Expand Down
Loading