builder: improve the function redefinition detector

pull/6729/head
Delyan Angelov 2020-11-03 10:00:06 +02:00
parent 2c3f695469
commit 204fd8be03
1 changed files with 28 additions and 4 deletions

View File

@ -309,26 +309,50 @@ fn (b &Builder) print_warnings_and_errors() {
exit(1)
}
if b.table.redefined_fns.len > 0 {
mut total_conflicts := 0
for fn_name in b.table.redefined_fns {
eprintln('redefinition of function `$fn_name`')
// eprintln('previous declaration at')
// Find where this function was already declared
mut redefines := []FunctionRedefinition{}
mut redefine_conflicts := map[string]int{}
for file in b.parsed_files {
for stmt in file.stmts {
if stmt is ast.FnDecl {
if stmt.name == fn_name {
fline := stmt.pos.line_nr
println('$file.path:$fline:')
fheader := stmt.stringify(b.table, 'main')
redefines << FunctionRedefinition{
fpath: file.path
fline: stmt.pos.line_nr
f: stmt
fheader: fheader
}
redefine_conflicts[fheader]++
}
}
}
}
if redefine_conflicts.len > 1 {
eprintln('redefinition of function `$fn_name`')
for redefine in redefines {
eprintln(util.formatted_error('conflicting declaration:', redefine.fheader,
redefine.fpath, redefine.f.pos))
}
total_conflicts++
}
}
if total_conflicts > 0 {
b.show_total_warns_and_errors_stats()
exit(1)
}
}
}
struct FunctionRedefinition {
fpath string
fline int
fheader string
f ast.FnDecl
}
fn verror(s string) {
util.verror('builder error', s)
}