diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1ac6cf9a62..be4cd408cf 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -239,16 +239,21 @@ pub fn (mut c Checker) check(mut ast_file ast.File) { sym.pos) } } + + cmp_mod_name := if ast_import.mod != ast_import.alias && ast_import.alias != '_' { + ast_import.alias + } else { + ast_import.mod + } for j in 0 .. i { - 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_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) + if cmp_mod_name == if ast_file.imports[j].mod != ast_file.imports[j].alias + && ast_file.imports[j].alias != '_' { + ast_file.imports[j].alias + } else { + ast_file.imports[j].mod + } { + c.error('A module `${cmp_mod_name}` was already imported on line ${ + ast_file.imports[j].mod_pos.line_nr + 1}`.', ast_import.mod_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 index 976fb4b9e1..8e81ca0880 100644 --- 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 @@ -1,6 +1,6 @@ -vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.vv:2:19: error: `json` was already imported on line 1 +vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.vv:2:8: error: A module `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_duplicate_err.out b/vlib/v/checker/tests/import_mod_as_import_duplicate_err.out index 0c437ee865..bedd4297a2 100644 --- a/vlib/v/checker/tests/import_mod_as_import_duplicate_err.out +++ b/vlib/v/checker/tests/import_mod_as_import_duplicate_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/import_mod_as_import_duplicate_err.vv:2:8: error: `x.json2` was already imported as `json` on line 1 +vlib/v/checker/tests/import_mod_as_import_duplicate_err.vv:2:8: error: A module `json` was already imported on line 1`. 1 | import x.json2 as json 2 | import json | ~~~~ diff --git a/vlib/v/tests/import_order_1_test.v b/vlib/v/tests/import_order_1_test.v new file mode 100644 index 0000000000..b57c460beb --- /dev/null +++ b/vlib/v/tests/import_order_1_test.v @@ -0,0 +1,13 @@ +// NOTE: the order of these import statements is the opposite of the one in import_order_2_test.v, +// but *both* should compile and work: +import x.benchmark +import benchmark as jj + +fn test_runs() { + mut b := jj.start() + mut action := benchmark.setup(fn () ! { + return error('no') + })! + action.run() + b.measure('nothing') +} diff --git a/vlib/v/tests/import_order_2_test.v b/vlib/v/tests/import_order_2_test.v new file mode 100644 index 0000000000..a0b6dccc8a --- /dev/null +++ b/vlib/v/tests/import_order_2_test.v @@ -0,0 +1,13 @@ +// NOTE: the order of these import statements is the opposite of the one in import_order_1_test.v, +// but *both* should compile and work: +import benchmark as jj +import x.benchmark + +fn test_runs() { + mut b := jj.start() + mut action := benchmark.setup(fn () ! { + return error('no') + })! + action.run() + b.measure('nothing') +}