Skip to content

Commit 299ebdf

Browse files
authored
checker: revise logic for reporting import conflicts with module names (#24539)
1 parent 140c7ea commit 299ebdf

5 files changed

Lines changed: 43 additions & 12 deletions

File tree

‎vlib/v/checker/checker.v‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,21 @@ pub fn (mut c Checker) check(mut ast_file ast.File) {
239239
sym.pos)
240240
}
241241
}
242+
243+
cmp_mod_name := if ast_import.mod != ast_import.alias && ast_import.alias != '_' {
244+
ast_import.alias
245+
} else {
246+
ast_import.mod
247+
}
242248
for j in 0 .. i {
243-
if ast_import.mod == ast_file.imports[j].mod {
244-
c.error('`${ast_import.mod}` was already imported on line ${
245-
ast_file.imports[j].mod_pos.line_nr + 1}', ast_import.mod_pos)
246-
} else if ast_import.mod == ast_file.imports[j].alias {
247-
c.error('`${ast_file.imports[j].mod}` was already imported as `${ast_import.alias}` on line ${
248-
ast_file.imports[j].mod_pos.line_nr + 1}', ast_import.mod_pos)
249-
} else if ast_import.alias != '_' && ast_import.alias == ast_file.imports[j].alias {
250-
c.error('`${ast_file.imports[j].mod}` was already imported on line ${
251-
ast_file.imports[j].alias_pos.line_nr + 1}', ast_import.alias_pos)
249+
if cmp_mod_name == if ast_file.imports[j].mod != ast_file.imports[j].alias
250+
&& ast_file.imports[j].alias != '_' {
251+
ast_file.imports[j].alias
252+
} else {
253+
ast_file.imports[j].mod
254+
} {
255+
c.error('A module `${cmp_mod_name}` was already imported on line ${
256+
ast_file.imports[j].mod_pos.line_nr + 1}`.', ast_import.mod_pos)
252257
}
253258
}
254259
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
vlib/v/checker/tests/import_mod_as_import_alias_duplicate_err.vv:2:19: error: `json` was already imported on line 1
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`.
22
1 | import json
33
2 | import x.json2 as json
4-
| ~~~~
4+
| ~~~~~~~
55
3 |
66
4 | numbers := {

‎vlib/v/checker/tests/import_mod_as_import_duplicate_err.out‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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+
vlib/v/checker/tests/import_mod_as_import_duplicate_err.vv:2:8: error: A module `json` was already imported on line 1`.
22
1 | import x.json2 as json
33
2 | import json
44
| ~~~~

‎vlib/v/tests/import_order_1_test.v‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// NOTE: the order of these import statements is the opposite of the one in import_order_2_test.v,
2+
// but *both* should compile and work:
3+
import x.benchmark
4+
import benchmark as jj
5+
6+
fn test_runs() {
7+
mut b := jj.start()
8+
mut action := benchmark.setup(fn () ! {
9+
return error('no')
10+
})!
11+
action.run()
12+
b.measure('nothing')
13+
}

‎vlib/v/tests/import_order_2_test.v‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// NOTE: the order of these import statements is the opposite of the one in import_order_1_test.v,
2+
// but *both* should compile and work:
3+
import benchmark as jj
4+
import x.benchmark
5+
6+
fn test_runs() {
7+
mut b := jj.start()
8+
mut action := benchmark.setup(fn () ! {
9+
return error('no')
10+
})!
11+
action.run()
12+
b.measure('nothing')
13+
}

0 commit comments

Comments
 (0)