Skip to content

Commit

Permalink
feat: enhance evaluator func type_pack_and_check err msg
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa committed Jan 14, 2025
1 parent 2562e63 commit 4e9b8a2
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions kclvm/evaluator/src/ty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use kclvm_runtime::{
check_type, dereference_type, is_dict_type, is_list_type, is_type_union, schema_config_meta,
schema_runtime_type, separate_kv, split_type_union, val_plan, ConfigEntryOperationKind,
ValueRef, BUILTIN_TYPES, KCL_TYPE_ANY, PKG_PATH_PREFIX,
check_type, dereference_type, is_dict_type, is_list_type, is_schema_type, is_type_union,
schema_config_meta, schema_runtime_type, separate_kv, split_type_union, val_plan,
ConfigEntryOperationKind, ValueRef, BUILTIN_TYPES, KCL_TYPE_ANY, PKG_PATH_PREFIX,
};
use scopeguard::defer;

Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn type_pack_and_check(
let mut checked = false;
let mut converted_value = value.clone();
let expected_type = &expected_types.join(" | ").replace('@', "");
for tpe in expected_types {
for tpe in &expected_types {
if !is_schema {
converted_value = convert_collection_value(s, value, tpe);
}
Expand All @@ -88,9 +88,50 @@ pub fn type_pack_and_check(
}
}
if !checked {
let mut error_msgs = vec![];
for tpe in &expected_types {
if is_schema_type(tpe) {
let schema_type_name = if tpe.contains('.') {
if tpe.starts_with(PKG_PATH_PREFIX) {
tpe.to_string()
} else {
format!("{PKG_PATH_PREFIX}{tpe}")
}
} else {
format!("{}.{}", s.current_pkgpath(), tpe)
};

if let Some(index) = s.schemas.borrow().get(&schema_type_name) {
let frame = {
let frames = s.frames.borrow();
frames
.get(*index)
.expect(kcl_error::INTERNAL_ERROR_MSG)
.clone()
};
if let Proxy::Schema(caller) = &frame.proxy {
if value.is_config() {
let config = value.as_dict_ref();
for (key, _) in &config.values {
let no_such_attr =
!SchemaEvalContext::has_attr(s, &caller.ctx, key)
&& !key.starts_with('_');
let has_index_signature =
SchemaEvalContext::has_index_signature(s, &caller.ctx);
if !has_index_signature && no_such_attr {
error_msgs
.push(format!("Schema {} not contains attr {}", tpe, key));
}
}
}
}
}
}
}
panic!(
"expect {expected_type}, got {}",
val_plan::type_of(value, true)
"expect {expected_type}, got {}. For details: {}",
val_plan::type_of(value, true),
error_msgs.join(" ")
);
}
converted_value
Expand Down

0 comments on commit 4e9b8a2

Please sign in to comment.