Skip to content

Commit

Permalink
fix: fix schema index signature type check
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Jun 27, 2024
1 parent 59a5719 commit 86a11b9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
8 changes: 8 additions & 0 deletions kclvm/sema/src/resolver/test_data/index_signature_check.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
schema Template:
[...str]: int
name: str

t: Template = {
name: ""
id: 1
}
16 changes: 16 additions & 0 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,3 +886,19 @@ fn test_builtin_file_invalid() {
assert_eq!(diags[0].messages[0].message, *expected_message);
}
}

#[test]
fn test_schema_index_signature_check() {
let sess = Arc::new(ParseSession::default());
let mut program = load_program(
sess.clone(),
&["./src/resolver/test_data/index_signature_check.k"],
None,
None,
)
.unwrap()
.program;
let scope = resolve_program(&mut program);
let diags = scope.handler.diagnostics;
assert!(diags.is_empty())
}
25 changes: 15 additions & 10 deletions kclvm/sema/src/resolver/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,21 @@ impl<'ctx> Resolver<'ctx> {
range: &Range,
) -> bool {
if let Some(index_signature) = &schema_ty.index_signature {
if !self.check_type(val_ty.clone(), index_signature.val_ty.clone(), range) {
self.handler.add_type_error(
&format!(
"expected schema index signature value type {}, got {}",
index_signature.val_ty.ty_str(),
val_ty.ty_str()
),
range.clone(),
);
}
let val_ty = match (&key_ty.kind, &val_ty.kind) {
(TypeKind::Union(key_tys), TypeKind::Union(val_tys)) => {
let mut index_signature_val_tys: Vec<TypeRef> = vec![];
for (i, key_ty) in key_tys.iter().enumerate() {
if let TypeKind::StrLit(s) = &key_ty.kind {
if schema_ty.attrs.get(s).is_none() && val_tys.get(i).is_some() {
index_signature_val_tys.push(val_tys.get(i).unwrap().clone());
}
}
}
crate::ty::sup(&index_signature_val_tys).into()
}
_ => val_ty,
};

if index_signature.any_other {
return self.check_type(key_ty, index_signature.key_ty.clone(), range)
&& self.check_type(val_ty, index_signature.val_ty.clone(), range);
Expand Down

0 comments on commit 86a11b9

Please sign in to comment.