From 77a1f5928fd062e527ea07d05d052ef005929da0 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+tobealive@users.noreply.github.com> Date: Thu, 15 Jun 2023 01:06:44 +0200 Subject: [PATCH] checker: include import aliases when checking for import duplicates (#18450) --- vlib/v/checker/checker.v | 6 ++++++ .../import_mod_as_import_alias_duplicate_err.out | 6 ++++++ .../import_mod_as_import_alias_duplicate_err.vv | 12 ++++++++++++ .../tests/import_mod_as_import_duplicate_err.out | 6 ++++++ .../tests/import_mod_as_import_duplicate_err.vv | 12 ++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.out create mode 100755 vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.vv create mode 100755 vlib/v/checker/tests/import_mod_as_import_duplicate_err.out create mode 100644 vlib/v/checker/tests/import_mod_as_import_duplicate_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 029da8f25f..b601c161fa 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -179,6 +179,12 @@ pub fn (mut c Checker) check(ast_file_ &ast.File) { if ast_import.mod == ast_file.imports[j].mod { c.error('`${ast_import.mod}` was already imported on line ${ ast_file.imports[j].mod_pos.line_nr + 1}', ast_import.mod_pos) + } else if ast_import.mod == ast_file.imports[j].alias { + c.error('`${ast_file.imports[j].mod}` was already imported as `${ast_import.alias}` on line ${ + ast_file.imports[j].mod_pos.line_nr + 1}', ast_import.mod_pos) + } else if ast_import.alias == ast_file.imports[j].alias { + c.error('`${ast_file.imports[j].mod}` was already imported on line ${ + ast_file.imports[j].alias_pos.line_nr + 1}', ast_import.alias_pos) } } } diff --git a/vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.out b/vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.out new file mode 100644 index 0000000000..976fb4b9e1 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.vv:2:19: error: `json` was already imported on line 1 + 1 | import json + 2 | import x.json2 as json + | ~~~~ + 3 | + 4 | numbers := { diff --git a/vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.vv b/vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.vv new file mode 100755 index 0000000000..5f1285c919 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.vv @@ -0,0 +1,12 @@ +import json +import x.json2 as json + +numbers := { + 'one': 1 + 'two': 2 + 'three': 3 + 'four': 4 +} + +out := json.encode(numbers) +println(out) diff --git a/vlib/v/checker/tests/import_mod_as_import_duplicate_err.out b/vlib/v/checker/tests/import_mod_as_import_duplicate_err.out new file mode 100755 index 0000000000..e1aeede348 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_as_import_duplicate_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/import_mod_as_import_duplicate_err.vv:2:8: error: `x.json2` was already imported as `json` on line 1 + 1 | import x.json2 as json + 2 | import json + | ~~~~ + 3 | + 4 | numbers := { \ No newline at end of file diff --git a/vlib/v/checker/tests/import_mod_as_import_duplicate_err.vv b/vlib/v/checker/tests/import_mod_as_import_duplicate_err.vv new file mode 100644 index 0000000000..e86992b61e --- /dev/null +++ b/vlib/v/checker/tests/import_mod_as_import_duplicate_err.vv @@ -0,0 +1,12 @@ +import x.json2 as json +import json + +numbers := { + 'one': 1 + 'two': 2 + 'three': 3 + 'four': 4 +} + +out := json.encode(numbers) +println(out)