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: schema init in same pkg and add more unit test cases. #87

Merged
merged 3 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Empty file.
5 changes: 5 additions & 0 deletions kclvm/sema/src/resolver/test_data/pkg/pkg.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
schema Name:
name?: str

schema Person:
name: Name = Name {name = "Alice"}
3 changes: 3 additions & 0 deletions kclvm/sema/src/resolver/test_data/pkg_init_in_schema.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pkg

person = pkg.Person {}
35 changes: 34 additions & 1 deletion kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::builtin::BUILTIN_FUNCTION_NAMES;
use crate::resolver::resolve_program;
use crate::resolver::scope::*;
use crate::ty::Type;
use kclvm_parser::parse_program;
use kclvm_ast::ast;
use kclvm_parser::{load_program, parse_program};
use std::rc::Rc;

#[test]
Expand Down Expand Up @@ -34,3 +35,35 @@ fn test_resolve_program() {
assert!(main_scope.lookup("b").is_some());
assert!(main_scope.lookup("print").is_none());
}

#[test]
fn test_pkg_init_in_schema_resolve() {
let mut program =
load_program(&["./src/resolver/test_data/pkg_init_in_schema.k"], None).unwrap();
let scope = resolve_program(&mut program);
assert_eq!(
scope.pkgpaths(),
vec!["__main__".to_string(), "pkg".to_string()]
);
let module = &program.pkgs["pkg"][0];
if let ast::Stmt::Schema(schema) = &module.body[1].node {
if let ast::Stmt::SchemaAttr(attr) = &schema.body[0].node {
let value = attr.value.as_ref().unwrap();
if let ast::Expr::Schema(schema_expr) = &value.node {
assert_eq!(schema_expr.name.node.names, vec!["Name".to_string()]);
} else {
panic!("test failed, expect schema expr, got {:?}", value)
}
} else {
panic!(
"test failed, expect schema attribute, got {:?}",
schema.body[0]
)
}
} else {
panic!(
"test failed, expect schema statement, got {:?}",
module.body[1]
)
}
}
24 changes: 20 additions & 4 deletions kclvm/sema/src/resolver/ty_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use kclvm_ast::{ast, walk_if_mut, walk_list_mut};

#[derive(Default)]
struct TypeAliasTransformer {
pub pkgpath: String,
pub type_alias_mapping: IndexMap<String, String>,
}

Expand Down Expand Up @@ -83,10 +84,22 @@ impl<'ctx> MutSelfMutWalker<'ctx> for TypeAliasTransformer {
}
fn walk_identifier(&mut self, identifier: &'ctx mut ast::Identifier) {
if let Some(type_alias) = self.type_alias_mapping.get(&identifier.get_name()) {
if type_alias.starts_with('@') {
if type_alias.starts_with('@') && type_alias.contains('.') {
let splits: Vec<&str> = type_alias.rsplitn(2, '.').collect();
identifier.pkgpath = splits[1].to_string();
identifier.names = vec![splits[1].to_string(), splits[0].to_string()];
let pkgpath = splits[1].to_string();
// Do not replace package identifier name in the same package.
// For example, the following code:
//
// ```
// schema Name:
// name: str
// schema Person:
// name: Name
// ```
if self.pkgpath != &pkgpath[1..] {
identifier.pkgpath = pkgpath;
identifier.names = vec![splits[1].to_string(), splits[0].to_string()];
}
} else {
let names = type_alias.split('.').collect::<Vec<&str>>();
identifier.names = names.iter().map(|n| n.to_string()).collect();
Expand All @@ -100,7 +113,10 @@ fn fix_type_alias_identifier<'ctx>(
module: &'ctx mut ast::Module,
type_alias_mapping: IndexMap<String, String>,
) {
let mut type_alias_transformer = TypeAliasTransformer { type_alias_mapping };
let mut type_alias_transformer = TypeAliasTransformer {
pkgpath: module.pkg.clone(),
type_alias_mapping,
};
type_alias_transformer.walk_module(module);
}

Expand Down