parser: add duplicate import symbol detect

This commit is contained in:
kbkpbot 2025-08-29 15:50:49 +08:00
parent 9e1273ae71
commit 2b1978ab38
2 changed files with 5 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{