checker: revise logic for reporting import conflicts with module names (#24539)

This commit is contained in:
Jose L Cuevas 2025-05-22 04:46:20 -04:00 committed by GitHub
parent 140c7ea889
commit 299ebdff04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 43 additions and 12 deletions

View File

@ -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)
}
}
}

View File

@ -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 := {

View File

@ -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
| ~~~~

View File

@ -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')
}

View File

@ -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')
}