Skip to content

Commit

Permalink
fix: mutual reference for config multiple keys
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Oct 30, 2023
1 parent f5a2bc2 commit 9e4584c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
12 changes: 7 additions & 5 deletions kclvm/compiler/src/codegen/llvm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2964,11 +2964,8 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
_ => None,
};
// Store a local variable for every entry key.
let key = match optional_name {
Some(name) => {
self.add_or_update_local_variable(&name, value);
self.string_value(&name)
}
let key = match &optional_name {
Some(name) => self.string_value(name),
None => self.walk_expr(key)?,
};
self.dict_insert_with_key_value(
Expand All @@ -2978,6 +2975,11 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
item.node.operation.value(),
insert_index as i32,
);
if let Some(name) = &optional_name {
let value =
self.dict_get(config_value, self.native_global_string(name, "").into());
self.add_or_update_local_variable(name, value);
}
} else {
// If the key does not exist, execute the logic of unpacking expression `**expr` here.
self.build_void_call(
Expand Down
32 changes: 21 additions & 11 deletions kclvm/sema/src/resolver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ impl<'ctx> Resolver<'ctx> {
self.enter_scope(start, end, ScopeKind::Config);
let mut key_types: Vec<TypeRef> = vec![];
let mut val_types: Vec<TypeRef> = vec![];
let mut attrs = IndexMap::new();
let mut attrs: IndexMap<String, Attr> = IndexMap::new();
for item in entries {
let key = &item.node.key;
let value = &item.node.value;
Expand All @@ -423,10 +423,15 @@ impl<'ctx> Resolver<'ctx> {
Rc::new(Type::str_lit(name))
};
self.check_attr_ty(&key_ty, key.get_span_pos());
let ty = if let Some(attr) = attrs.get(name) {
sup(&[attr.ty.clone(), val_ty.clone()])
} else {
val_ty.clone()
};
attrs.insert(
name.to_string(),
Attr {
ty: val_ty.clone(),
ty: ty.clone(),
range: key.get_span_pos(),
},
);
Expand All @@ -436,7 +441,7 @@ impl<'ctx> Resolver<'ctx> {
name: name.to_string(),
start: key.get_pos(),
end: key.get_end_pos(),
ty: val_ty.clone(),
ty,
kind: ScopeObjectKind::Attribute,
doc: None,
},
Expand All @@ -463,24 +468,29 @@ impl<'ctx> Resolver<'ctx> {
let val_ty = self.expr(value);
self.check_attr_ty(&key_ty, key.get_span_pos());
if let ast::Expr::StringLit(string_lit) = &key.node {
let ty = if let Some(attr) = attrs.get(&string_lit.value) {
sup(&[attr.ty.clone(), val_ty.clone()])
} else {
val_ty.clone()
};
attrs.insert(
string_lit.value.clone(),
Attr {
ty: ty.clone(),
range: key.get_span_pos(),
},
);
self.insert_object(
&string_lit.value,
ScopeObject {
name: string_lit.value.clone(),
start: key.get_pos(),
end: key.get_end_pos(),
ty: val_ty.clone(),
ty,
kind: ScopeObjectKind::Attribute,
doc: None,
},
);
attrs.insert(
string_lit.value.clone(),
Attr {
ty: val_ty.clone(),
range: key.get_span_pos(),
},
);
}
key_types.push(key_ty);
val_types.push(val_ty.clone());
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::path::Path;
use crate::{
dispatcher::NotificationDispatcher,
from_lsp,
state::{log_message, LanguageServerState},
state::LanguageServerState,
util::apply_document_changes,
util::{build_word_index_for_file_content, word_index_add, word_index_subtract},
};
Expand Down
6 changes: 6 additions & 0 deletions test/grammar/datatype/dict/mutual_ref_14/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
deployment = {
metadata.labels.k1 = "v1"
metadata.namespace = "default"
spec.selector.matchLabels = metadata.labels | {k2 = "v2"}
}
labels: {str:str} = deployment.metadata.labels
12 changes: 12 additions & 0 deletions test/grammar/datatype/dict/mutual_ref_14/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
deployment:
metadata:
labels:
k1: v1
namespace: default
spec:
selector:
matchLabels:
k1: v1
k2: v2
labels:
k1: v1

0 comments on commit 9e4584c

Please sign in to comment.