Skip to content

Commit

Permalink
feat: enhance config to schema type check on union expr
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Aug 13, 2024
1 parent dcbd9d9 commit aaada9a
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 11 deletions.
22 changes: 17 additions & 5 deletions kclvm/sema/src/resolver/calculation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use std::sync::Arc;

use crate::resolver::Resolver;
use crate::ty::{
has_any_type, is_upper_bound, sup, Type, TypeInferMethods, TypeRef, ZERO_LIT_TYPES,
has_any_type, is_upper_bound, sup, Type, TypeInferMethods, TypeKind, TypeRef, ZERO_LIT_TYPES,
};
use indexmap::IndexMap;
use kclvm_ast::ast;
use kclvm_error::diagnostic::Range;

Expand Down Expand Up @@ -162,12 +163,23 @@ impl<'ctx> Resolver<'ctx> {
true,
Type::list_ref(sup(&[t1.list_item_ty(), t2.list_item_ty()])),
)
} else if t1.is_dict() && t2.is_dict() {
let (t1_key_ty, t1_val_ty) = t1.dict_entry_ty();
let (t2_key_ty, t2_val_ty) = t2.dict_entry_ty();
} else if let (TypeKind::Dict(t1_dict_ty), TypeKind::Dict(t2_dict_ty)) =
(&t1.kind, &t2.kind)
{
let mut attrs = IndexMap::new();
for (k, v) in &t1_dict_ty.attrs {
attrs.insert(k.to_string(), v.clone());
}
for (k, v) in &t2_dict_ty.attrs {
attrs.insert(k.to_string(), v.clone());
}
(
true,
Type::dict_ref(sup(&[t1_key_ty, t2_key_ty]), sup(&[t1_val_ty, t2_val_ty])),
Arc::new(Type::dict_with_attrs(
sup(&[t1_dict_ty.key_ty.clone(), t2_dict_ty.key_ty.clone()]),
sup(&[t1_dict_ty.val_ty.clone(), t2_dict_ty.val_ty.clone()]),
attrs,
)),
)
} else if t1.is_schema() && (t2.is_schema() || t2.is_dict()) {
(true, t1)
Expand Down
14 changes: 8 additions & 6 deletions kclvm/sema/src/resolver/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,14 @@ impl<'ctx> Resolver<'ctx> {
// check whether the type of key value pair in dict matches the attribute type in the schema.
if let TypeKind::StrLit(key_name) = &key_ty.kind {
if let Some(attr_obj) = schema_ty.attrs.get(key_name) {
self.must_assignable_to(
val_ty.clone(),
attr_obj.ty.clone(),
range.clone(),
Some(attr_obj.range.clone()),
);
if let Some(attr) = dict_ty.attrs.get(key_name) {
self.must_assignable_to(
attr.ty.clone(),
attr_obj.ty.clone(),
range.clone(),
Some(attr_obj.range.clone()),
);
}
return true;
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/grammar/types/union_expr/union_expr_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema Container:
name: str
image: str
volumeMounts: [{str:}]

config = {
image = "test/test-container:test-cluster"
volumeMounts = [{
name = "config"
mountPath = "/app/config"
}]
}

expected: Container = config | {name = "test-repo/test-image:test-tag"}
11 changes: 11 additions & 0 deletions test/grammar/types/union_expr/union_expr_0/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
config:
image: test/test-container:test-cluster
volumeMounts:
- name: config
mountPath: /app/config
expected:
name: test-repo/test-image:test-tag
image: test/test-container:test-cluster
volumeMounts:
- name: config
mountPath: /app/config
16 changes: 16 additions & 0 deletions test/grammar/types/union_expr/union_expr_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
schema Container:
name: str
image: str
volumeMounts: [{str:}]

schema Config:
_config = {
image = "test/test-container:test-cluster"
volumeMounts = [{
name = "config"
mountPath = "/app/config"
}]
}
expected: Container = _config | {name = "test-repo/test-image:test-tag"}

config = Config {}
7 changes: 7 additions & 0 deletions test/grammar/types/union_expr/union_expr_1/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
config:
expected:
name: test-repo/test-image:test-tag
image: test/test-container:test-cluster
volumeMounts:
- name: config
mountPath: /app/config
14 changes: 14 additions & 0 deletions test/grammar/types/union_expr/union_expr_fail_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema Container:
name: str
image: str
volumeMounts: [{str:}]

config = {
image = "test/test-container:test-cluster"
volumeMounts = [{
name = "config"
mountPath = "/app/config"
}]
}

expected: Container = config | {name = 1}
6 changes: 6 additions & 0 deletions test/grammar/types/union_expr/union_expr_fail_0/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E2G22]: TypeError
--> ${CWD}/main.k:14:33
|
14 | expected: Container = config | {name = 1}
| ^ expected str, got int(1)
|
16 changes: 16 additions & 0 deletions test/grammar/types/union_expr/union_expr_fail_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
schema Container:
name: str
image: str
volumeMounts: [{str:}]

schema Config:
_config = {
image = "test/test-container:test-cluster"
volumeMounts = [{
name = "config"
mountPath = "/app/config"
}]
}
expected: Container = _config | {name = 1}

config = Config {}
6 changes: 6 additions & 0 deletions test/grammar/types/union_expr/union_expr_fail_1/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E2G22]: TypeError
--> ${CWD}/main.k:14:38
|
14 | expected: Container = _config | {name = 1}
| ^ expected str, got int(1)
|
17 changes: 17 additions & 0 deletions test/grammar/types/union_expr/union_expr_fail_2/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
schema Container:
name: str
image: str
volumeMounts: [{str:}]

schema Config:
_config = {
name = 1
image = "test/test-container:test-cluster"
volumeMounts = [{
name = "config"
mountPath = "/app/config"
}]
}
expected: Container = _config | {}

config = Config {}
6 changes: 6 additions & 0 deletions test/grammar/types/union_expr/union_expr_fail_2/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E3M38]: EvaluationError
--> ${CWD}/main.k:2:1
|
2 | name: str
| expect str, got int
|

0 comments on commit aaada9a

Please sign in to comment.