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 2 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
5 changes: 5 additions & 0 deletions kclvm/cmd/src/test_data/multi_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
1 change: 1 addition & 0 deletions kclvm/cmd/src/test_data/multi_import/sub/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub = "sub"
1 change: 1 addition & 0 deletions kclvm/cmd/src/test_data/multi_import/sub/sub/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub_sub = "sub_sub"
19 changes: 19 additions & 0 deletions kclvm/cmd/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,22 @@ fn test_error_message_fuzz_unmatched() {
}
}
}

#[test]
fn test_multi_import() {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
let test_case_path = PathBuf::from("./src/test_data/multi_import/main.k");
let matches = app().arg_required_else_help(true).get_matches_from(&[
ROOT_CMD,
"run",
&test_case_path.canonicalize().unwrap().display().to_string(),
]);
let settings = must_build_settings(matches.subcommand_matches("run").unwrap());
let sess = Arc::new(ParseSession::default());
match exec_program(sess.clone(), &settings.try_into().unwrap()) {
Ok(_) => panic!("unreachable code."),
Err(msg) => {
assert!(msg
.contains("the name 's' is defined multiple times, 's' must be defined only once"))
}
}
}
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