From 0d3f13343696b913695c2d27c155ea88cacf71c1 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 20 May 2020 17:27:31 +0200 Subject: [PATCH] parser: show location of a previously defined function --- vlib/v/builder/builder.v | 22 +++++++++++++++++++--- vlib/v/parser/fn.v | 3 ++- vlib/v/parser/parser.v | 10 +++++++--- vlib/v/table/table.v | 1 + 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index 8d82b3364e..e139797c8d 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -212,14 +212,12 @@ fn (b &Builder) print_warnings_and_errors() { if b.checker.nr_errors > 0 { exit(1) } - return } - if b.pref.is_verbose && b.checker.nr_warnings > 1 { println('$b.checker.nr_warnings warnings') } - if b.checker.nr_warnings > 0 && !b.pref.skip_warnings { + if b.checker.nr_warnings > 0 && !b.pref.skip_warnings { for i, err in b.checker.warnings { kind := if b.pref.is_verbose { '$err.reporter warning #$b.checker.nr_warnings:' } else { 'warning:' } ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) @@ -246,6 +244,24 @@ fn (b &Builder) print_warnings_and_errors() { } exit(1) } + if b.table.redefined_fns.len > 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 + for file in b.parsed_files { + for stmt in file.stmts { + if stmt is ast.FnDecl { + f := stmt as ast.FnDecl + if f.name == fn_name { + println(file.path + ':' + f.pos.line_nr.str()) + } + } + } + } + exit(1) + } + } } fn verror(s string) { diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 6ec667ef59..6ca8ad1dd2 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -456,7 +456,8 @@ fn (mut p Parser) fn_redefinition_error(name string) { } */ - p.error('redefinition of function `$name`') + p.table.redefined_fns << name + // p.error('redefinition of function `$name`') } fn have_fn_main(stmts []ast.Stmt) bool { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index fcf8ed4447..a2364bee8c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -664,12 +664,12 @@ fn (mut p Parser) parse_multi_expr() ast.Stmt { } else if p.tok.kind.is_assign() { epos := p.tok.position() if collected.len == 1 { - return ast.ExprStmt { + return ast.ExprStmt{ expr: p.assign_expr(collected[0]) pos: epos } } else { - return ast.ExprStmt { + return ast.ExprStmt{ expr: p.assign_expr(ast.ConcatExpr{ vals: collected }) @@ -1044,7 +1044,11 @@ fn (mut p Parser) string_expr() ast.Expr { node = ast.StringLiteral{ val: val is_raw: is_raw - language: if is_cstr { table.Language.c } else { table.Language.v } + language: if is_cstr { + table.Language.c + } else { + table.Language.v + } pos: pos } return node diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 3c60736466..02e45bed29 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -14,6 +14,7 @@ pub mut: imports []string // List of all imports modules []string // List of all modules registered by the application cflags []cflag.CFlag + redefined_fns []string } pub struct Fn {