From 86a11b92c74b08ec742efc16625eccd9d17a2404 Mon Sep 17 00:00:00 2001 From: he1pa <18012015693@163.com> Date: Thu, 27 Jun 2024 20:09:32 +0800 Subject: [PATCH 1/2] fix: fix schema index signature type check Signed-off-by: he1pa <18012015693@163.com> --- .../test_data/index_signature_check.k | 8 ++++++ kclvm/sema/src/resolver/tests.rs | 16 ++++++++++++ kclvm/sema/src/resolver/ty.rs | 25 +++++++++++-------- 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 kclvm/sema/src/resolver/test_data/index_signature_check.k diff --git a/kclvm/sema/src/resolver/test_data/index_signature_check.k b/kclvm/sema/src/resolver/test_data/index_signature_check.k new file mode 100644 index 000000000..a0b10414e --- /dev/null +++ b/kclvm/sema/src/resolver/test_data/index_signature_check.k @@ -0,0 +1,8 @@ +schema Template: + [...str]: int + name: str + +t: Template = { + name: "" + id: 1 +} diff --git a/kclvm/sema/src/resolver/tests.rs b/kclvm/sema/src/resolver/tests.rs index 65c80a23f..811267ede 100644 --- a/kclvm/sema/src/resolver/tests.rs +++ b/kclvm/sema/src/resolver/tests.rs @@ -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()) +} diff --git a/kclvm/sema/src/resolver/ty.rs b/kclvm/sema/src/resolver/ty.rs index b8e338f24..ecf37f85c 100644 --- a/kclvm/sema/src/resolver/ty.rs +++ b/kclvm/sema/src/resolver/ty.rs @@ -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 = 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); From c0ddc1a60af0ffb378a656ffe282caa48d368f0f Mon Sep 17 00:00:00 2001 From: he1pa <18012015693@163.com> Date: Fri, 28 Jun 2024 13:45:26 +0800 Subject: [PATCH 2/2] fixt ut Signed-off-by: he1pa <18012015693@163.com> --- kclvm/sema/src/resolver/ty.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kclvm/sema/src/resolver/ty.rs b/kclvm/sema/src/resolver/ty.rs index ecf37f85c..dd8592bc2 100644 --- a/kclvm/sema/src/resolver/ty.rs +++ b/kclvm/sema/src/resolver/ty.rs @@ -304,6 +304,17 @@ impl<'ctx> Resolver<'ctx> { _ => val_ty, }; + 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(), + ); + } + 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);