From dc04c3196b8cd06bb6835b28e65a3d7f9ee5f688 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Mon, 1 Mar 2021 20:38:31 +0000 Subject: [PATCH] builder: fix detecting duplicate functions (#9033) --- vlib/v/builder/builder.v | 2 +- vlib/v/checker/tests/fn_duplicate.out | 13 +++++++++++++ vlib/v/checker/tests/fn_duplicate.vv | 7 +++++++ vlib/v/parser/fn.v | 19 ++----------------- 4 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 vlib/v/checker/tests/fn_duplicate.out create mode 100644 vlib/v/checker/tests/fn_duplicate.vv diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index 99a213838e..ff45f5313d 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -366,7 +366,7 @@ fn (b &Builder) print_warnings_and_errors() { } } } - if redefine_conflicts.len > 1 { + if redefines.len > 0 { eprintln('redefinition of function `$fn_name`') for redefine in redefines { eprintln(util.formatted_error('conflicting declaration:', redefine.fheader, diff --git a/vlib/v/checker/tests/fn_duplicate.out b/vlib/v/checker/tests/fn_duplicate.out new file mode 100644 index 0000000000..3cf949c733 --- /dev/null +++ b/vlib/v/checker/tests/fn_duplicate.out @@ -0,0 +1,13 @@ +redefinition of function `main.f` +vlib/v/checker/tests/fn_duplicate.vv:1:1: conflicting declaration: fn f() + 1 | fn f() { + | ~~~~~~ + 2 | + 3 | } +vlib/v/checker/tests/fn_duplicate.vv:5:1: conflicting declaration: fn f(i int) + 3 | } + 4 | + 5 | fn f(i int) { + | ~~~~~~~~~~~ + 6 | + 7 | } diff --git a/vlib/v/checker/tests/fn_duplicate.vv b/vlib/v/checker/tests/fn_duplicate.vv new file mode 100644 index 0000000000..08c1dfbe2a --- /dev/null +++ b/vlib/v/checker/tests/fn_duplicate.vv @@ -0,0 +1,7 @@ +fn f() { + +} + +fn f(i int) { + +} diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 3ece8c3d2a..e3eb079466 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -329,8 +329,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl { } else { name = p.prepend_mod(name) } - if _ := p.table.find_fn(name) { - p.fn_redefinition_error(name) + if !p.pref.translated && language == .v && name in p.table.fns { + p.table.redefined_fns << name } // p.warn('reg functn $name ()') p.table.register_fn(table.Fn{ @@ -809,21 +809,6 @@ fn (mut p Parser) check_fn_atomic_arguments(typ table.Type, pos token.Position) } } -fn (mut p Parser) fn_redefinition_error(name string) { - if p.pref.translated { - return - } - // Find where this function was already declared - // TODO - /* - for file in p.ast_files { - - } - */ - p.table.redefined_fns << name - // p.error('redefinition of function `$name`') -} - fn have_fn_main(stmts []ast.Stmt) bool { for stmt in stmts { if stmt is ast.FnDecl {