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 sema info about config entry left key and right value #1541

Merged
merged 9 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 3 additions & 17 deletions kclvm/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ pub enum ScopeKind {
Quant,
Lambda,
SchemaDef,
SchemaConfig,
Value,
Config,
Check,
Callable,
}
Expand Down Expand Up @@ -253,8 +252,7 @@ impl From<LocalSymbolScopeKind> for ScopeKind {
LocalSymbolScopeKind::Quant => ScopeKind::Quant,
LocalSymbolScopeKind::Lambda => ScopeKind::Lambda,
LocalSymbolScopeKind::SchemaDef => ScopeKind::SchemaDef,
LocalSymbolScopeKind::SchemaConfig => ScopeKind::SchemaConfig,
LocalSymbolScopeKind::Value => ScopeKind::Value,
LocalSymbolScopeKind::Config => ScopeKind::Config,
LocalSymbolScopeKind::Check => ScopeKind::Check,
LocalSymbolScopeKind::Callable => ScopeKind::Callable,
}
Expand All @@ -274,18 +272,6 @@ fn collect_scope_info(
} else {
kind
};
let get_def_from_owner = match scope_ref.get_kind() {
kclvm_sema::core::scope::ScopeKind::Local => {
match scope_data.try_get_local_scope(&scope_ref) {
Some(local) => match local.get_kind() {
LocalSymbolScopeKind::SchemaConfig | LocalSymbolScopeKind::Check => true,
_ => false,
},
None => false,
}
}
kclvm_sema::core::scope::ScopeKind::Root => false,
};
scopes.insert(
*scope_ref,
ScopeInfo {
Expand All @@ -294,7 +280,7 @@ fn collect_scope_info(
owner: scope.get_owner(),
children: scope.get_children(),
defs: scope
.get_all_defs(scope_data, symbol_data, None, false, get_def_from_owner)
.get_all_defs(scope_data, symbol_data, None, false, true)
.values()
.copied()
.collect::<Vec<_>>(),
Expand Down
31 changes: 15 additions & 16 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ pub struct Context<'ctx> {
// which means advanced resolver will will create the corresponding
// ValueSymbol instead of an UnresolvedSymbol
maybe_def: bool,
// whether lookup def in scope owner, default true, only in schema attr value is false
look_up_in_owner: bool,
// whether in schema config right value, affect lookup def
in_config_r_value: bool,
}

impl<'ctx> Context<'ctx> {
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
end_pos: Position::dummy_pos(),
cur_node: AstIndex::default(),
maybe_def: false,
look_up_in_owner: true,
in_config_r_value: false,
},
};
// Scan all scehma symbol
Expand Down Expand Up @@ -1407,7 +1407,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
17_u64,
26_u64,
5_usize,
10_usize,
),
// __main__.Main schema expr scope
(
Expand All @@ -1425,7 +1425,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
30,
20,
5,
7,
),
// pkg.Person schema expr scope
(
Expand All @@ -1443,7 +1443,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
34,
17,
5,
6,
),
// __main__ package scope
(
Expand All @@ -1461,7 +1461,7 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
15,
11,
4,
6,
),
// import_test.a.Name expr scope
(
Expand All @@ -1479,20 +1479,19 @@ mod tests {
.replace("/", &std::path::MAIN_SEPARATOR.to_string()),
12,
21,
4,
8,
),
];

for (filepath, line, col, def_num) in scope_test_cases.iter() {
let abs_scope_file_path = adjust_canonicalization(base_path.join(filepath));
let scope_ref = gs
.look_up_scope(&Position {
filename: abs_scope_file_path.clone(),
line: *line,
column: Some(*col),
})
.unwrap();
let all_defs = gs.get_all_defs_in_scope(scope_ref).unwrap();
let pos = Position {
filename: abs_scope_file_path.clone(),
line: *line,
column: Some(*col),
};
let scope_ref = gs.look_up_scope(&pos).unwrap();
let all_defs = gs.get_all_defs_in_scope(scope_ref, &pos).unwrap();
assert_eq!(all_defs.len(), *def_num)
}
}
Expand Down
111 changes: 74 additions & 37 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use kclvm_error::{diagnostic::Range, Position};

use crate::{
core::{
scope::LocalSymbolScopeKind,
scope::{ConfigScopeContext, LocalSymbolScopeKind},
symbol::{
CommentOrDocSymbol, DecoratorSymbol, ExpressionSymbol, Symbol, SymbolHint,
SymbolHintKind, SymbolRef, SymbolSemanticInfo, UnresolvedSymbol, ValueSymbol,
Expand Down Expand Up @@ -407,7 +407,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
if has_check {
self.leave_scope();
}

self.leave_scope();

Ok(Some(schema_symbol))
Expand Down Expand Up @@ -568,6 +567,16 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
}

self.resolve_decorator(&schema_attr.decorators);
let cur_scope = *self.ctx.scopes.last().unwrap();
let name = self
.gs
.get_symbols()
.get_symbol(attr_symbol)
.ok_or(anyhow!("attribute_symbol not found"))?
.get_name();
self.gs
.get_scopes_mut()
.add_def_to_scope(cur_scope, name, attr_symbol);
Ok(Some(attr_symbol))
}

Expand Down Expand Up @@ -1043,7 +1052,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
cur_scope,
self.get_current_module_info(),
maybe_def,
self.ctx.look_up_in_owner,
!self.ctx.in_config_r_value,
);
if first_symbol.is_none() {
// Maybe import package symbol
Expand Down Expand Up @@ -1083,29 +1092,60 @@ impl<'ctx> AdvancedResolver<'ctx> {
match first_symbol {
Some(symbol_ref) => {
let (start_pos, end_pos): Range = first_name.get_span_pos();
let (def_start_pos, def_end_pos) = self
let def_symbol = self
.gs
.get_symbols()
.get_symbol(symbol_ref)
.ok_or(anyhow!("first name symbol not found"))?
.get_range();
.ok_or(anyhow!("first name symbol not found"))?;
let (def_start_pos, def_end_pos) = def_symbol.get_range();

let cur_scope = *self.ctx.scopes.last().unwrap();
let ast_id = first_name.id.clone();
let mut first_unresolved = UnresolvedSymbol::new(
first_name.node.clone(),
start_pos.clone(),
end_pos.clone(),
None,
);
let name = def_symbol.get_name();
first_unresolved.def = Some(symbol_ref);
let first_unresolved_ref = self.gs.get_symbols_mut().alloc_unresolved_symbol(
first_unresolved,
self.ctx.get_node_key(&ast_id),
self.ctx.current_pkgpath.clone().unwrap(),
);

match cur_scope.get_kind() {
crate::core::scope::ScopeKind::Local => {
let local_scope = self
.gs
.get_scopes()
.try_get_local_scope(&cur_scope)
.unwrap();
match local_scope.get_kind() {
LocalSymbolScopeKind::Config => {
if let crate::core::symbol::SymbolKind::Attribute =
symbol_ref.get_kind()
{
self.gs.get_scopes_mut().add_def_to_scope(
cur_scope,
name,
first_unresolved_ref,
);
}
}
_ => {}
}
}
_ => {}
}

// Get an unresolved symbol
if def_start_pos != start_pos || def_end_pos != end_pos {
let ast_id = first_name.id.clone();
let mut first_unresolved =
UnresolvedSymbol::new(first_name.node.clone(), start_pos, end_pos, None);
first_unresolved.def = Some(symbol_ref);
let first_unresolved_ref = self.gs.get_symbols_mut().alloc_unresolved_symbol(
first_unresolved,
self.ctx.get_node_key(&ast_id),
self.ctx.current_pkgpath.clone().unwrap(),
);
let cur_scope = *self.ctx.scopes.last().unwrap();
self.gs
.get_scopes_mut()
.add_ref_to_scope(cur_scope, first_unresolved_ref);
}

if names.len() > 1 {
let mut parent_ty = match self
.ctx
Expand Down Expand Up @@ -1251,7 +1291,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
cur_scope,
self.get_current_module_info(),
true,
self.ctx.look_up_in_owner,
!self.ctx.in_config_r_value,
);
match first_symbol {
Some(symbol_ref) => {
Expand Down Expand Up @@ -1765,10 +1805,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone());

let schema_symbol = self.ctx.schema_symbol_stack.last().unwrap_or(&None).clone();
let kind = match &schema_symbol {
Some(_) => LocalSymbolScopeKind::SchemaConfig,
None => LocalSymbolScopeKind::Value,
};
let kind = LocalSymbolScopeKind::Config;

self.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
Expand All @@ -1784,28 +1821,28 @@ impl<'ctx> AdvancedResolver<'ctx> {
.set_owner_to_scope(*cur_scope, owner);
}

let mut entries_range = vec![];
for entry in entries.iter() {
entries_range.push((
entry.node.key.clone().map(|k| k.get_span_pos()),
entry.node.value.get_span_pos(),
));
self.ctx.in_config_r_value = true;
self.expr(&entry.node.value)?;
self.ctx.in_config_r_value = false;

if let Some(key) = &entry.node.key {
self.ctx.maybe_def = true;
self.ctx.look_up_in_owner = true;
self.expr(key)?;
self.ctx.look_up_in_owner = false;
self.ctx.maybe_def = false;
}

let (start, end) = entry.node.value.get_span_pos();

self.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
start,
end,
LocalSymbolScopeKind::Value,
);
self.ctx.look_up_in_owner = false;
self.expr(&entry.node.value)?;
self.ctx.look_up_in_owner = true;
self.leave_scope();
}

let cur_scope = self.ctx.scopes.last().unwrap();
self.gs
.get_scopes_mut()
.config_scope_context
.insert(cur_scope.get_id(), ConfigScopeContext { entries_range });
self.leave_scope();
Ok(())
}
Expand Down
Loading
Loading