Skip to content
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
4 changes: 2 additions & 2 deletions kclvm/runtime/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl crate::Context {
// check dup
for i in 0..self.option_helps.len() {
if self.option_helps[i].name == name {
if typ.is_empty() && !required && default_value == None && help.is_empty() {
if typ.is_empty() && !required && default_value.is_none() && help.is_empty() {
return;
}

Expand All @@ -192,7 +192,7 @@ impl crate::Context {
if !self.option_helps[i].required {
self.option_helps[i].required = required;
}
if self.option_helps[i].default_value == None {
if self.option_helps[i].default_value.is_none() {
self.option_helps[i].default_value = default_value;
}
if self.option_helps[i].help.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/crypto/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub extern "C" fn kclvm_crypto_md5(
let args = ptr_as_ref(args);

if let Some(s) = args.arg_i_str(0, None) {
let hex = format!("{:x}", md5::compute(&s));
let hex = format!("{:x}", md5::compute(s));
return ValueRef::str(hex.as_ref()).into_raw();
}
panic!("md5() missing 1 required positional argument: 'value'");
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/units/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ pub fn to_quantity(quantity: &str) -> i64 {
1000
};
let exponent = EXPONENTS.get(&suffix[0..1]).unwrap();
number * (base.pow(*exponent as u32)) as i64
number * (base.pow(*exponent as u32))
}

/// Calculate number based on value and binary suffix.
Expand Down
10 changes: 5 additions & 5 deletions kclvm/runtime/src/value/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,20 +1745,20 @@ pub extern "C" fn kclvm_value_union(
let mut entry = b.dict_get_entry(k).unwrap().deep_copy();
entry.dict_update_key_value(k, v);
result = a
.union(&entry, true, false, false, false)
.union_entry(&entry, true, false, false, false)
.clone()
.into_raw();
} else {
let entry = b.dict_get_entry(k).unwrap();
result = a
.union(&entry, true, false, false, false)
.union_entry(&entry, true, false, false, false)
.clone()
.into_raw();
}
}
result
} else {
a.union(b, true, false, false, false).into_raw()
a.union_entry(b, true, false, false, false).into_raw()
}
}

Expand Down Expand Up @@ -2075,7 +2075,7 @@ pub extern "C" fn kclvm_schema_value_check(
if should_add_attr && is_not_in_schema {
let value = index_sign_value
.deep_copy()
.union(value, true, false, false, true);
.union_entry(value, true, false, false, true);
let op = config
.ops
.get(key)
Expand Down Expand Up @@ -2277,7 +2277,7 @@ pub extern "C" fn kclvm_schema_value_new(
let config = ptr_as_ref(config);
let result = schema_value_or_func
.deep_copy()
.union(config, true, false, true, true);
.union_entry(config, true, false, true, true);
result.into_raw()
}
}
Expand Down
9 changes: 4 additions & 5 deletions kclvm/runtime/src/value/val_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,10 @@ impl ValueRef {
}

pub fn bin_bit_or(&self, x: &Self) -> Self {
match (&*self.rc.borrow(), &*x.rc.borrow()) {
(Value::int_value(a), Value::int_value(b)) => return Self::int(*a | *b),
_ => {}
}
self.deep_copy().union(x, true, false, true, true)
if let (Value::int_value(a), Value::int_value(b)) = (&*self.rc.borrow(), &*x.rc.borrow()) {
return Self::int(*a | *b);
};
self.deep_copy().union_entry(x, true, false, true, true)
}

pub fn bin_subscr(&self, x: &Self) -> Self {
Expand Down
10 changes: 5 additions & 5 deletions kclvm/runtime/src/value/val_bin_aug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl ValueRef {
if strict_range_check_32 && is_f32_overflow_pow(*a, *b) {
panic_f32_overflow!(a.powf(*b));
}
*a = a.powf(*b as f64);
*a = a.powf(*b);
true
}
(Value::int_value(a), Value::float_value(b)) => {
Expand Down Expand Up @@ -355,7 +355,7 @@ impl ValueRef {
pub fn bin_aug_bit_and(&mut self, x: &Self) -> &mut Self {
let valid = match (&mut *self.rc.borrow_mut(), &*x.rc.borrow()) {
(Value::int_value(a), Value::int_value(b)) => {
*a &= *b as i64;
*a &= *b;
true
}
_ => false,
Expand All @@ -369,7 +369,7 @@ impl ValueRef {
pub fn bin_aug_bit_xor(&mut self, x: &Self) -> &mut Self {
let valid = match (&mut *self.rc.borrow_mut(), &*x.rc.borrow()) {
(Value::int_value(a), Value::int_value(b)) => {
*a ^= *b as i64;
*a ^= *b;
true
}
_ => false,
Expand All @@ -383,14 +383,14 @@ impl ValueRef {
pub fn bin_aug_bit_or(&mut self, x: &Self) -> &mut Self {
let valid = match (&mut *self.rc.borrow_mut(), &*x.rc.borrow()) {
(Value::int_value(a), Value::int_value(b)) => {
*a |= *b as i64;
*a |= *b;
true
}
_ => false,
};
if !valid {
if self.is_list_or_config() || x.is_list_or_config() {
self.union(x, true, false, true, true);
self.union_entry(x, true, false, true, true);
} else {
panic_unsupported_bin_op!("|", self.type_str(), x.type_str());
}
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/value/val_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl ValueRef {
dict.values.insert(key.to_string(), v.clone());
dict.ops.insert(key.to_string(), op);
dict.insert_indexs.insert(key.to_string(), insert_index);
self.union(
self.union_entry(
&ValueRef::from(Value::dict_value(Box::new(dict))),
true,
false,
Expand Down
9 changes: 3 additions & 6 deletions kclvm/runtime/src/value/val_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,9 @@ pub fn convert_collection_value(value: &ValueRef, tpe: &str) -> ValueRef {
}
None => {
for (_, mapping) in &ctx.import_names {
match mapping.get(pkgname) {
Some(pkgpath) => {
schema_type_name = format!("{}.{}", pkgpath, name);
break;
}
None => {}
if let Some(pkgpath) = mapping.get(pkgname) {
schema_type_name = format!("{}.{}", pkgpath, name);
break;
}
}
}
Expand Down
47 changes: 30 additions & 17 deletions kclvm/runtime/src/value/val_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ impl ValueRef {
{
panic!("conflicting values on the attribute '{}' between {:?} and {:?}", k, self, x);
}
let value = obj.values.get_mut(k).unwrap().union(
obj.values.get_mut(k).unwrap().union(
v,
false,
should_list_override,
should_idempotent_check,
should_config_resolve,
);
obj.values.insert(k.clone(), value);
}
ConfigEntryOperationKind::Override => {
if index < 0 {
Expand All @@ -67,20 +66,18 @@ impl ValueRef {
}
}
ConfigEntryOperationKind::Insert => {
let value = v.deep_copy();
let origin_value = obj.values.get_mut(k).unwrap();
if origin_value.is_none_or_undefined() {
let list = ValueRef::list(None);
obj.values.insert(k.to_string(), list);
}
let origin_value = obj.values.get_mut(k).unwrap();
match (
&mut *origin_value.rc.borrow_mut(),
&mut *value.rc.borrow_mut(),
) {
match (&mut *origin_value.rc.borrow_mut(), &*v.rc.borrow()) {
(Value::list_value(origin_value), Value::list_value(value)) => {
if index == -1 {
origin_value.values.append(&mut value.clone().values);
for elem in value.values.iter() {
origin_value.values.push(elem.clone());
}
} else if index >= 0 {
let mut insert_index = index;
for v in &value.values {
Expand Down Expand Up @@ -183,8 +180,7 @@ impl ValueRef {
}
self.clone()
}

pub fn union(
fn union(
&mut self,
x: &Self,
or_mode: bool,
Expand All @@ -207,13 +203,12 @@ impl ValueRef {
should_config_resolve,
);
} else if or_mode {
match (&mut *self.rc.borrow_mut(), &*x.rc.borrow()) {
(Value::int_value(a), Value::int_value(b)) => {
*a |= *b;
return self.clone();
}
_ => {}
}
if let (Value::int_value(a), Value::int_value(b)) =
(&mut *self.rc.borrow_mut(), &*x.rc.borrow())
{
*a |= *b;
return self.clone();
};
panic!(
"unsupported operand type(s) for |: '{:?}' and '{:?}'",
self.type_str(),
Expand All @@ -224,6 +219,24 @@ impl ValueRef {
}
self.clone()
}

// Deep copy the right value of the union call to avoid the left value part refering to the right value
pub fn union_entry(
&mut self,
x: &Self,
or_mode: bool,
should_list_override: bool,
should_idempotent_check: bool,
should_config_resolve: bool,
) -> Self {
self.union(
&x.deep_copy(),
or_mode,
should_list_override,
should_idempotent_check,
should_config_resolve,
)
}
}

#[cfg(test)]
Expand Down
12 changes: 12 additions & 0 deletions test/grammar/schema/duplicated_key/duplicated_key1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
schema Config:
cpu?: int

schema A:
k?: Config

schema C(A):
k?: Config

a = C {
k: Config {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a:
k: {}
12 changes: 12 additions & 0 deletions test/grammar/schema/duplicated_key/duplicated_key2/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
schema Config:
cpu?: int

schema A:
k?: Config

schema C(A):
k: Config

a = C {
k: Config {cpu: 1}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a:
k:
cpu: 1
12 changes: 12 additions & 0 deletions test/grammar/schema/duplicated_key/duplicated_key3/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
schema Container:
cpu?: int

schema App:
container: Container

container = Container {cpu: 1}

app = App {
container: container
container: container
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
container:
cpu: 1
app:
container:
cpu: 1