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: fix schema index signature type check #1450

Merged
merged 2 commits into from
Jun 28, 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: 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())
}
16 changes: 16 additions & 0 deletions kclvm/sema/src/resolver/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ impl<'ctx> Resolver<'ctx> {
range: &Range,
) -> bool {
if let Some(index_signature) = &schema_ty.index_signature {
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 !self.check_type(val_ty.clone(), index_signature.val_ty.clone(), range) {
self.handler.add_type_error(
&format!(
Expand All @@ -299,6 +314,7 @@ impl<'ctx> Resolver<'ctx> {
range.clone(),
);
}

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
Loading