1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: check for builtin function redefinitions (fix #7499) (#7507)

This commit is contained in:
yuyi 2020-12-24 00:17:09 +08:00 committed by GitHub
parent 6f5c1f060c
commit 7b9756be72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 2 deletions

View File

@ -229,10 +229,12 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
}
mut name := ''
if p.tok.kind == .name {
pos := p.tok.position()
// TODO high order fn
name = if language == .js { p.check_js_name() } else { p.check_name() }
if language == .v && !p.pref.translated && util.contains_capital(name) && p.mod != 'builtin' {
p.error('function names cannot contain uppercase letters, use snake_case instead')
p.error_with_pos('function names cannot contain uppercase letters, use snake_case instead',
pos)
return ast.FnDecl{
scope: 0
}
@ -240,7 +242,14 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
type_sym := p.table.get_type_symbol(rec_type)
// interfaces are handled in the checker, methods can not be defined on them this way
if is_method && (type_sym.has_method(name) && type_sym.kind != .interface_) {
p.error('duplicate method `$name`')
p.error_with_pos('duplicate method `$name`', pos)
return ast.FnDecl{
scope: 0
}
}
// cannot redefine buildin function
if !is_method && p.mod != 'builtin' && name in builtin_functions {
p.error_with_pos('cannot redefine builtin function `$name`', pos)
return ast.FnDecl{
scope: 0
}

View File

@ -14,6 +14,10 @@ import os
import runtime
import time
const (
builtin_functions = ['print', 'println', 'eprint', 'eprintln', 'isnil', 'panic', 'exit']
)
pub struct Parser {
pref &pref.Preferences
mut:

View File

@ -0,0 +1,5 @@
vlib/v/parser/tests/fn_use_builtin_err.vv:1:4: error: cannot redefine builtin function `print`
1 | fn print(strings ...string) {
| ~~~~~
2 | for s in strings {
3 | println(s)

View File

@ -0,0 +1,9 @@
fn print(strings ...string) {
for s in strings {
println(s)
}
}
fn main() {
print('text')
}