v.parser: prohibit redeclaration of imported functions (#10564)

pull/10569/head
shadowninja55 2021-06-25 07:27:58 -04:00 committed by GitHub
parent be8be3d319
commit 9b84faad6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -267,11 +267,18 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
}
}
}
// cannot redefine buildin function
if !is_method && !p.builtin_mod && name in builtin_functions {
p.error_with_pos('cannot redefine builtin function `$name`', name_pos)
return ast.FnDecl{
scope: 0
if !p.pref.is_fmt {
if !is_method && !p.builtin_mod && name in builtin_functions {
p.error_with_pos('cannot redefine builtin function `$name`', name_pos)
return ast.FnDecl{
scope: 0
}
}
if name in p.imported_symbols {
p.error_with_pos('cannot redefine imported function `$name`', name_pos)
return ast.FnDecl{
scope: 0
}
}
}
} else if p.tok.kind in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && p.peek_tok.kind == .lpar {

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/redeclaration_of_imported_fn.vv:3:4: error: cannot redefine imported function `input`
1 | import os { input }
2 |
3 | fn input() {}
| ~~~~~
4 |
5 | input()

View File

@ -0,0 +1,5 @@
import os { input }
fn input() {}
input()