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: config merge and unpack in loop for #1175

Merged
merged 1 commit into from
Mar 29, 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
8 changes: 7 additions & 1 deletion kclvm/compiler/src/codegen/llvm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,13 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
.expect(kcl_error::COMPILE_ERROR_MSG);
let key = self.walk_expr(elt).expect(kcl_error::COMPILE_ERROR_MSG);
let op = op.expect(kcl_error::INTERNAL_ERROR_MSG);
self.dict_insert_with_key_value(collection_value, key, value, op.value(), -1);
self.dict_insert_with_key_value(
collection_value,
key,
self.value_deep_copy(value),
op.value(),
-1,
);
}
}
} else {
Expand Down
8 changes: 7 additions & 1 deletion kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,13 @@ impl<'ctx> Evaluator<'ctx> {
.expect(kcl_error::COMPILE_ERROR_MSG);
let key = self.walk_expr(elt).expect(kcl_error::COMPILE_ERROR_MSG);
let op = op.expect(kcl_error::INTERNAL_ERROR_MSG);
self.dict_insert(collection_value, &key.as_str(), &value, op, -1);
self.dict_insert(
collection_value,
&key.as_str(),
&value.deep_copy(),
op,
-1,
);
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions kclvm/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ impl FastRunner {
unsafe {
let plugin_method: extern "C" fn(
method: *const i8,
args: *const i8,
kwargs: *const i8,
) -> *const i8 = std::mem::transmute(self.opts.plugin_agent_ptr);
args: *const c_char,
kwargs: *const c_char,
) -> *const c_char = std::mem::transmute(self.opts.plugin_agent_ptr);
kclvm_plugin_init(plugin_method);
}
}
Expand Down
80 changes: 63 additions & 17 deletions kclvm/sema/src/resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,24 +705,70 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
}

fn walk_dict_comp(&mut self, dict_comp: &'ctx ast::DictComp) -> Self::Result {
let key = dict_comp.entry.key.as_ref().unwrap();
let start = key.get_pos();
let end = match dict_comp.generators.last() {
Some(last) => last.get_end_pos(),
None => dict_comp.entry.value.get_end_pos(),
};
self.enter_scope(start.clone(), end, ScopeKind::Loop);
for comp_clause in &dict_comp.generators {
self.walk_comp_clause(&comp_clause.node);
if dict_comp.entry.key.is_none() {
self.handler.add_compile_error(
"dict unpacking cannot be used in dict comprehension",
dict_comp.entry.value.get_span_pos(),
);
let start = dict_comp.entry.value.get_pos();
let end = match dict_comp.generators.last() {
Some(last) => last.get_end_pos(),
None => dict_comp.entry.value.get_end_pos(),
};
self.enter_scope(start.clone(), end, ScopeKind::Loop);
for comp_clause in &dict_comp.generators {
self.walk_comp_clause(&comp_clause.node);
}
let stack_depth = self.switch_config_expr_context_by_key(&dict_comp.entry.key);
let val_ty = self.expr(&dict_comp.entry.value);
let key_ty = match &val_ty.kind {
TypeKind::None | TypeKind::Any => val_ty.clone(),
TypeKind::Dict(DictType { key_ty, .. }) => key_ty.clone(),
TypeKind::Schema(schema_ty) => schema_ty.key_ty().clone(),
TypeKind::Union(types)
if self
.ctx
.ty_ctx
.is_config_type_or_config_union_type(val_ty.clone()) =>
{
sup(&types
.iter()
.map(|ty| ty.config_key_ty())
.collect::<Vec<TypeRef>>())
}
_ => {
self.handler.add_compile_error(
&format!(
"only dict and schema can be used ** unpack, got '{}'",
val_ty.ty_str()
),
dict_comp.entry.value.get_span_pos(),
);
self.any_ty()
}
};
self.clear_config_expr_context(stack_depth, false);
self.leave_scope();
Type::dict_ref(key_ty, val_ty)
} else {
let key = dict_comp.entry.key.as_ref().unwrap();
let end = match dict_comp.generators.last() {
Some(last) => last.get_end_pos(),
None => dict_comp.entry.value.get_end_pos(),
};
let start = key.get_pos();
self.enter_scope(start.clone(), end, ScopeKind::Loop);
for comp_clause in &dict_comp.generators {
self.walk_comp_clause(&comp_clause.node);
}
let key_ty = self.expr(key);
self.check_attr_ty(&key_ty, key.get_span_pos());
let stack_depth = self.switch_config_expr_context_by_key(&dict_comp.entry.key);
let val_ty = self.expr(&dict_comp.entry.value);
self.clear_config_expr_context(stack_depth, false);
self.leave_scope();
Type::dict_ref(key_ty, val_ty)
}
let key_ty = self.expr(key);
// TODO: Naming both dict keys and schema attributes as `attribute`
self.check_attr_ty(&key_ty, key.get_span_pos());
let stack_depth = self.switch_config_expr_context_by_key(&dict_comp.entry.key);
let val_ty = self.expr(&dict_comp.entry.value);
self.clear_config_expr_context(stack_depth, false);
self.leave_scope();
Type::dict_ref(key_ty, val_ty)
}

fn walk_list_if_item_expr(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
z = {**i for i in [{}, {}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
z = {**i for i in [1, 2]}
2 changes: 2 additions & 0 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ fn test_resolve_program_fail() {
"comp_clause_error_0.k",
"comp_clause_error_1.k",
"comp_clause_error_2.k",
"comp_clause_error_3.k",
"comp_clause_error_4.k",
"config_expr.k",
"invalid_mixin_0.k",
"module_optional_select.k",
Expand Down
9 changes: 9 additions & 0 deletions test/grammar/datatype/dict/merge_in_comprehension_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
x = {
a = 1
b = True
}
y = {
c = 3
d = "4"
}
z = {"foo": i for i in [x, y]}.foo
11 changes: 11 additions & 0 deletions test/grammar/datatype/dict/merge_in_comprehension_0/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
x:
a: 1
b: true
y:
c: 3
d: '4'
z:
a: 1
b: true
c: 3
d: '4'
10 changes: 10 additions & 0 deletions test/grammar/datatype/dict/merge_in_comprehension_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
x = {
a = 1
b = True
}
y = {
c = 3
d = "4"
}
z1 = {**x, **y}
z2 = {"foo": {**i, a = 2, e = "five"} for i in [x, y]}.foo
17 changes: 17 additions & 0 deletions test/grammar/datatype/dict/merge_in_comprehension_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
x:
a: 1
b: true
y:
c: 3
d: '4'
z1:
a: 1
b: true
c: 3
d: '4'
z2:
a: 2
b: true
e: five
c: 3
d: '4'
Loading