Skip to content

Commit

Permalink
fix: fix the lambda argument type, when it is schema type, into schem…
Browse files Browse the repository at this point in the history
…a type instance (kcl-lang#803)

* fix: fix the lambda argument type, when it is schema type, into schema type instance

Signed-off-by: zongz <[email protected]>

* fix: make fmt

Signed-off-by: zongz <[email protected]>

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Oct 26, 2023
1 parent b4a784e commit 4eac50d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions kclvm/sema/src/resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,17 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
let name = arg.node.get_name();
let arg_ty = args.node.get_arg_type(i);
let ty = self.parse_ty_with_scope(&arg_ty, arg.get_span_pos());

// If the arguments type of a lambda is a schema type,
// It should be marked as an schema instance type.
let ty = if let TypeKind::Schema(sty) = &ty.kind {
let mut arg_ty = sty.clone();
arg_ty.is_instance = true;
Rc::new(Type::schema(arg_ty))
} else {
ty.clone()
};

params.push(Parameter {
name,
ty: ty.clone(),
Expand Down
9 changes: 9 additions & 0 deletions test/grammar/lambda/arg_type/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sub as s

identity = lambda res: s.Test {
res {
name = "world"
}
}

c = identity(s.Test{name="hello"})
2 changes: 2 additions & 0 deletions test/grammar/lambda/arg_type/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
c:
name: world
2 changes: 2 additions & 0 deletions test/grammar/lambda/arg_type/sub.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema Test:
name: str

0 comments on commit 4eac50d

Please sign in to comment.