parser: add duplicate import symbol detect (fix #25185) (#25187)

This commit is contained in:
kbkpbot 2025-08-29 18:31:59 +08:00 committed by GitHub
parent 07997816d9
commit eaf005e29f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 15 additions and 18 deletions

View File

@ -1,18 +0,0 @@
import math
import os
import math
// keep comment
import gg
import gg { MouseButton }
import time { Duration }
import time { Duration }
import math.complex { Complex }
import math.complex { Complex }
const mypi = math.pi
const mb = MouseButton{}
const complex = Complex{}
fn main() {
println(os.path_separator)
}

View File

@ -310,6 +310,11 @@ fn (mut p Parser) import_syms(mut parent ast.Import) {
for p.tok.kind == .name {
pos := p.tok.pos()
alias := p.check_name()
if alias in p.imported_symbols {
p.error_with_pos('cannot register symbol `${alias}`, it was already imported',
pos)
return
}
p.imported_symbols[alias] = parent.mod + '.' + alias
// so we can work with this in fmt+checker
parent.syms << ast.ImportSymbol{

View File

@ -0,0 +1,3 @@
vlib/v/parser/tests/module_import_same_symbol2_err.vv:1:20: error: cannot register symbol `max`, it was already imported
1 | import math { max, max }
| ~~~

View File

@ -0,0 +1 @@
import math { max, max }

View File

@ -0,0 +1,4 @@
vlib/v/parser/tests/module_import_same_symbol_err.vv:2:17: error: cannot register symbol `max`, it was already imported
1 | import math { max }
2 | import arrays { max }
| ~~~

View File

@ -0,0 +1,2 @@
import math { max }
import arrays { max }