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: raise an error when the asname of import statment is defined multi-times #727

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions kclvm/sema/src/resolver/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ impl<'ctx> Resolver<'ctx> {
{
match self.ctx.import_names.get_mut(&self.ctx.filename) {
Some(mapping) => {
// 'import sub as s' and 'import sub.sub as s' will raise this error.
// 'import sub' and 'import sub' will not raise this error.
if mapping.get(&import_stmt.name).is_some()
Peefy marked this conversation as resolved.
Show resolved Hide resolved
&& import_stmt.asname.is_some()
{
self.handler.add_compile_error(
&format!(
"the name '{}' is defined multiple times, '{}' must be defined only once",
import_stmt.name, import_stmt.name
),
stmt.get_span_pos(),
);
}
mapping.insert(
import_stmt.name.to_string(),
import_stmt.path.to_string(),
Expand Down
5 changes: 5 additions & 0 deletions kclvm/sema/src/resolver/test_fail_data/redefine_import/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sub as s

import sub.sub as s

main = s.sub_sub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub = "sub"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub_sub = "sub_sub"
24 changes: 24 additions & 0 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ fn test_resolve_program_fail() {
}
}

#[test]
fn test_resolve_program_redefine() {
let sess = Arc::new(ParseSession::default());
let mut program = load_program(
sess.clone(),
&["./src/resolver/test_fail_data/redefine_import/main.k"],
None,
)
.unwrap();

let scope = resolve_program(&mut program);
assert_eq!(scope.handler.diagnostics.len(), 2);
let diag = &scope.handler.diagnostics[0];
assert_eq!(
diag.code,
Some(DiagnosticId::Error(ErrorKind::CompileError))
);
assert_eq!(diag.messages.len(), 1);
assert_eq!(
diag.messages[0].message,
"the name 's' is defined multiple times, 's' must be defined only once"
);
}

#[test]
fn test_resolve_program_mismatch_type_fail() {
let mut program = parse_program("./src/resolver/test_fail_data/config_expr.k").unwrap();
Expand Down