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: local var scope in the evaluator #1456

Merged
merged 1 commit into from
Jun 28, 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
15 changes: 14 additions & 1 deletion kclvm/evaluator/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::rc::Rc;
use std::{collections::HashSet, rc::Rc};

use generational_arena::Index;
use kclvm_ast::ast;
Expand Down Expand Up @@ -146,6 +146,19 @@ impl<'ctx> Evaluator<'ctx> {
self.local_vars.borrow_mut().clear();
}

#[inline]
pub(crate) fn clean_and_cloned_local_vars(&self) -> HashSet<String> {
let mut local_vars = self.local_vars.borrow_mut();
let r = local_vars.clone();
local_vars.clear();
r
}

#[inline]
pub(crate) fn set_local_vars(&self, vars: HashSet<String>) {
self.local_vars.borrow_mut().extend(vars);
}

#[inline]
pub(crate) fn add_target_var(&self, name: &str) {
self.target_vars.borrow_mut().push(name.to_string());
Expand Down
11 changes: 9 additions & 2 deletions kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,14 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
ast::QuantOperation::All => {
if !is_truth {
self.leave_scope();
self.clear_local_vars();
return Ok(self.bool_value(false));
}
}
ast::QuantOperation::Any => {
if is_truth {
self.leave_scope();
self.clear_local_vars();
return Ok(self.bool_value(true));
}
}
Expand All @@ -466,11 +468,13 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
}
}
self.leave_scope();
self.clear_local_vars();
// End for block.
Ok(result)
}

fn walk_schema_attr(&self, schema_attr: &'ctx ast::SchemaAttr) -> Self::Result {
self.clear_local_vars();
let name = schema_attr.name.node.as_str();
self.add_target_var(name);
for decorator in &schema_attr.decorators {
Expand Down Expand Up @@ -687,7 +691,8 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
};
self.dict_insert_value(&mut dict_value, name.node.as_str(), &value);
}
if let Some(proxy) = func.try_get_proxy() {
let vars = self.clean_and_cloned_local_vars();
let result = if let Some(proxy) = func.try_get_proxy() {
// Invoke user defined functions, schemas or rules.
Ok(self.invoke_proxy_function(proxy, &list_value, &dict_value))
} else {
Expand All @@ -698,7 +703,9 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
&dict_value,
&mut self.runtime_ctx.borrow_mut(),
))
}
};
self.set_local_vars(vars);
result
}

fn walk_subscript(&self, subscript: &'ctx ast::Subscript) -> Self::Result {
Expand Down
25 changes: 25 additions & 0 deletions test/grammar/schema/index_signature/normal_11/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
schema User:
id: str

schema Users:
[str]: User

schema DB:
users: Users = {}

check:
all user in users {
user == users[user].id
}

schema DBs:
[str]: DB

dbs_user: DBs = {
user = DB {
users: {
app = User {id = "app"}
}
}
}
db_user = dbs_user.user
9 changes: 9 additions & 0 deletions test/grammar/schema/index_signature/normal_11/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dbs_user:
user:
users:
app:
id: app
db_user:
users:
app:
id: app
11 changes: 11 additions & 0 deletions test/grammar/schema/init/init_schema_6/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
schema Config:
name?: str

makeCopy = lambda p: Config -> Config {
Config {name = p.name + "-copy"}
}
configs = {
"foo": Config {name = "foo"}
"bar": Config {name = "bar"}
}
copies = {"${name}-copy": makeCopy(config) for name, config in configs}
10 changes: 10 additions & 0 deletions test/grammar/schema/init/init_schema_6/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
configs:
foo:
name: foo
bar:
name: bar
copies:
foo-copy:
name: foo-copy
bar-copy:
name: bar-copy
Loading