From 21bf8fe14e12ac5001ae523ae8d0977bc4a49e92 Mon Sep 17 00:00:00 2001 From: zakuro Date: Thu, 18 Feb 2021 04:50:10 +0900 Subject: [PATCH] parser: make duplicated functions an error (#8792) --- vlib/builtin/cfns.c.v | 29 ++++---------- vlib/net/unix/aasocket.c.v | 57 ---------------------------- vlib/sync/channels.v | 12 +++++- vlib/sync/pool/pool.v | 3 -- vlib/sync/sync_default.c.v | 19 ---------- vlib/sync/sync_macos.c.v | 21 ---------- vlib/sync/waitgroup.v | 5 +-- vlib/term/ui/consoleapi_windows.c.v | 25 ------------ vlib/v/parser/fn.v | 19 +++++----- vlib/v/parser/tests/fn_duplicate.out | 4 ++ vlib/v/parser/tests/fn_duplicate.vv | 2 + 11 files changed, 33 insertions(+), 163 deletions(-) create mode 100644 vlib/v/parser/tests/fn_duplicate.out create mode 100644 vlib/v/parser/tests/fn_duplicate.vv diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index 2603d7edf3..8ee5f0c250 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -346,54 +346,39 @@ fn C.ReleaseSRWLockShared(voidptr) fn C.ReleaseSRWLockExclusive(voidptr) // pthread.h +[trusted] fn C.pthread_mutex_init(voidptr, voidptr) int - fn C.pthread_mutex_lock(voidptr) int - fn C.pthread_mutex_unlock(voidptr) int - fn C.pthread_mutex_destroy(voidptr) int - fn C.pthread_rwlockattr_init(voidptr) int - fn C.pthread_rwlockattr_setkind_np(voidptr, int) int - fn C.pthread_rwlockattr_setpshared(voidptr, int) int - fn C.pthread_rwlock_init(voidptr, voidptr) int - fn C.pthread_rwlock_rdlock(voidptr) int - fn C.pthread_rwlock_wrlock(voidptr) int - fn C.pthread_rwlock_unlock(voidptr) int fn C.pthread_condattr_init(voidptr) int - fn C.pthread_condattr_setpshared(voidptr, int) int - fn C.pthread_condattr_destroy(voidptr) int - fn C.pthread_cond_init(voidptr, voidptr) int - fn C.pthread_cond_signal(voidptr) int - fn C.pthread_cond_wait(voidptr, voidptr) int - fn C.pthread_cond_timedwait(voidptr, voidptr, voidptr) int - fn C.pthread_cond_destroy(voidptr) int +[trusted] fn C.sem_init(voidptr, int, u32) int - +[trusted] fn C.sem_post(voidptr) int - +[trusted] fn C.sem_wait(voidptr) int - +[trusted] fn C.sem_trywait(voidptr) int - +[trusted] fn C.sem_timedwait(voidptr, voidptr) int - +[trusted] fn C.sem_destroy(voidptr) int // MacOS semaphore functions diff --git a/vlib/net/unix/aasocket.c.v b/vlib/net/unix/aasocket.c.v index a40639e54a..118874a4f4 100644 --- a/vlib/net/unix/aasocket.c.v +++ b/vlib/net/unix/aasocket.c.v @@ -43,60 +43,3 @@ mut: struct C.sockaddr_storage { } - -fn C.socket() int - -fn C.setsockopt() int - -fn C.htonl() int - -fn C.htons() int - -fn C.bind() int - -fn C.listen() int - -fn C.accept() int - -fn C.getaddrinfo() int - -fn C.connect() int - -fn C.send() int - -fn C.sendto() int - -fn C.recv() int - -fn C.recvfrom() int - -fn C.shutdown() int - -fn C.ntohs() int - -fn C.getpeername() int - -fn C.inet_ntop(af int, src voidptr, dst charptr, dst_size int) charptr - -fn C.WSAAddressToStringA() int - -fn C.getsockname() int - -// defined in builtin -// fn C.read() int -// fn C.close() int - -fn C.ioctlsocket() int - -fn C.fcntl() int - -fn C.@select() int - -fn C.FD_ZERO() - -fn C.FD_SET() - -fn C.FD_ISSET() bool - -[typedef] -struct C.fd_set {} diff --git a/vlib/sync/channels.v b/vlib/sync/channels.v index 017a75a74e..8b66594c3b 100644 --- a/vlib/sync/channels.v +++ b/vlib/sync/channels.v @@ -24,7 +24,9 @@ fn C.atomic_store_ptr(voidptr, voidptr) fn C.atomic_compare_exchange_weak_ptr(voidptr, voidptr, voidptr) bool fn C.atomic_compare_exchange_strong_ptr(voidptr, voidptr, voidptr) bool fn C.atomic_exchange_ptr(voidptr, voidptr) voidptr +[trusted] fn C.atomic_fetch_add_ptr(voidptr, voidptr) voidptr +[trusted] fn C.atomic_fetch_sub_ptr(voidptr, voidptr) voidptr fn C.atomic_load_u16(voidptr) u16 @@ -32,7 +34,9 @@ fn C.atomic_store_u16(voidptr, u16) fn C.atomic_compare_exchange_weak_u16(voidptr, voidptr, u16) bool fn C.atomic_compare_exchange_strong_u16(voidptr, voidptr, u16) bool fn C.atomic_exchange_u16(voidptr, u16) u16 +[trusted] fn C.atomic_fetch_add_u16(voidptr, u16) u16 +[trusted] fn C.atomic_fetch_sub_u16(voidptr, u16) u16 fn C.atomic_load_u32(voidptr) u32 @@ -40,7 +44,9 @@ fn C.atomic_store_u32(voidptr, u32) fn C.atomic_compare_exchange_weak_u32(voidptr, voidptr, u32) bool fn C.atomic_compare_exchange_strong_u32(voidptr, voidptr, u32) bool fn C.atomic_exchange_u32(voidptr, u32) u32 +[trusted] fn C.atomic_fetch_add_u32(voidptr, u32) u32 +[trusted] fn C.atomic_fetch_sub_u32(voidptr, u32) u32 fn C.atomic_load_u64(voidptr) u64 @@ -48,7 +54,9 @@ fn C.atomic_store_u64(voidptr, u64) fn C.atomic_compare_exchange_weak_u64(voidptr, voidptr, u64) bool fn C.atomic_compare_exchange_strong_u64(voidptr, voidptr, u64) bool fn C.atomic_exchange_u64(voidptr, u64) u64 +[trusted] fn C.atomic_fetch_add_u64(voidptr, u64) u64 +[trusted] fn C.atomic_fetch_sub_u64(voidptr, u64) u64 const ( @@ -359,7 +367,7 @@ pub fn (mut ch Channel) try_pop(dest voidptr) ChanState { } fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState { - spinloops_sem_, spinloops_ := if no_block { 1, 1 } else { spinloops, spinloops_sem } + spinloops_sem_, spinloops_ := if no_block { 1, 1 } else { spinloops, spinloops_sem } mut have_swapped := false mut write_in_progress := false for { @@ -513,7 +521,7 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState { dest2 = dest } } - break + break } return .success } diff --git a/vlib/sync/pool/pool.v b/vlib/sync/pool/pool.v index 090c07b283..368f6564f3 100644 --- a/vlib/sync/pool/pool.v +++ b/vlib/sync/pool/pool.v @@ -4,9 +4,6 @@ import sync import runtime -[trusted] -fn C.atomic_fetch_add_u32(voidptr, u32) u32 - pub const ( no_result = voidptr(0) ) diff --git a/vlib/sync/sync_default.c.v b/vlib/sync/sync_default.c.v index 244d8dd367..e7d6eea59b 100644 --- a/vlib/sync/sync_default.c.v +++ b/vlib/sync/sync_default.c.v @@ -8,25 +8,6 @@ import time #flag -lpthread #include -[trusted] -fn C.pthread_mutex_init(voidptr, voidptr) int -fn C.pthread_mutex_lock(voidptr) int -fn C.pthread_mutex_unlock(voidptr) int -fn C.pthread_mutex_destroy(voidptr) int -fn C.pthread_rwlockattr_init(voidptr) int -fn C.pthread_rwlockattr_setkind_np(voidptr, int) int -fn C.pthread_rwlockattr_setpshared(voidptr, int) int -fn C.pthread_rwlock_init(voidptr, voidptr) int -fn C.pthread_rwlock_rdlock(voidptr) int -fn C.pthread_rwlock_wrlock(voidptr) int -fn C.pthread_rwlock_unlock(voidptr) int -fn C.sem_init(voidptr, int, u32) int -fn C.sem_post(voidptr) int -fn C.sem_wait(voidptr) int -fn C.sem_trywait(voidptr) int -fn C.sem_timedwait(voidptr, voidptr) int -fn C.sem_destroy(voidptr) int - // [init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function. [heap] pub struct Mutex { diff --git a/vlib/sync/sync_macos.c.v b/vlib/sync/sync_macos.c.v index c62b133a85..4b954ba12e 100644 --- a/vlib/sync/sync_macos.c.v +++ b/vlib/sync/sync_macos.c.v @@ -9,27 +9,6 @@ import time #include #include -[trusted] -fn C.pthread_mutex_init(voidptr, voidptr) int -fn C.pthread_mutex_lock(voidptr) int -fn C.pthread_mutex_unlock(voidptr) int -fn C.pthread_mutex_destroy(voidptr) int -fn C.pthread_rwlockattr_init(voidptr) int -fn C.pthread_rwlockattr_setkind_np(voidptr, int) int -fn C.pthread_rwlockattr_setpshared(voidptr, int) int -fn C.pthread_rwlock_init(voidptr, voidptr) int -fn C.pthread_rwlock_rdlock(voidptr) int -fn C.pthread_rwlock_wrlock(voidptr) int -fn C.pthread_rwlock_unlock(voidptr) int -fn C.pthread_condattr_init(voidptr) int -fn C.pthread_condattr_setpshared(voidptr, int) int -fn C.pthread_condattr_destroy(voidptr) int -fn C.pthread_cond_init(voidptr, voidptr) int -fn C.pthread_cond_signal(voidptr) int -fn C.pthread_cond_wait(voidptr, voidptr) int -fn C.pthread_cond_timedwait(voidptr, voidptr, voidptr) int -fn C.pthread_cond_destroy(voidptr) int - // [init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function. [heap] pub struct Mutex { diff --git a/vlib/sync/waitgroup.v b/vlib/sync/waitgroup.v index d9d4aca668..9dd3c8c8b4 100644 --- a/vlib/sync/waitgroup.v +++ b/vlib/sync/waitgroup.v @@ -3,9 +3,6 @@ // that can be found in the LICENSE file. module sync -[trusted] -fn C.atomic_fetch_add_u32(voidptr, u32) u32 - // WaitGroup // Do not copy an instance of WaitGroup, use a ref instead. // @@ -15,7 +12,7 @@ fn C.atomic_fetch_add_u32(voidptr, u32) u32 // `wg.wait()` to wait for all jobs to have finished // // in each parallel job: -// `wg.done()` when finished +// `wg.done()` when finished // // [init_with=new_waitgroup] // TODO: implement support for init_with struct attribute, and disallow WaitGroup{} from outside the sync.new_waitgroup() function. [heap] diff --git a/vlib/term/ui/consoleapi_windows.c.v b/vlib/term/ui/consoleapi_windows.c.v index 19d521a0d0..c8a185eb41 100644 --- a/vlib/term/ui/consoleapi_windows.c.v +++ b/vlib/term/ui/consoleapi_windows.c.v @@ -54,31 +54,6 @@ struct C.FOCUS_EVENT_RECORD { bSetFocus int } -[typedef] -struct C.COORD { - X i16 - Y i16 -} - -[typedef] -struct C.SMALL_RECT { - Left u16 - Top u16 - Right u16 - Bottom u16 -} - -[typedef] -struct C.CONSOLE_SCREEN_BUFFER_INFO { - dwSize C.COORD - dwCursorPosition C.COORD - wAttributes u16 - srWindow C.SMALL_RECT - dwMaximumWindowSize C.COORD -} - fn C.ReadConsoleInput() bool fn C.GetNumberOfConsoleInputEvents() bool - -fn C.GetConsoleScreenBufferInfo(handle os.HANDLE, info &C.CONSOLE_SCREEN_BUFFER_INFO) bool diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index f2e40e317c..78d4aac296 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -215,13 +215,13 @@ fn (mut p Parser) fn_decl() ast.FnDecl { language = rec.language } mut name := '' + name_pos := p.tok.position() 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.builtin_mod { p.error_with_pos('function names cannot contain uppercase letters, use snake_case instead', - pos) + name_pos) return ast.FnDecl{ scope: 0 } @@ -229,14 +229,14 @@ fn (mut p Parser) fn_decl() ast.FnDecl { type_sym := p.table.get_type_symbol(rec.typ) // 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_with_pos('duplicate method `$name`', pos) + p.error_with_pos('duplicate method `$name`', name_pos) return ast.FnDecl{ scope: 0 } } // cannot redefine buildin function if !is_method && !p.builtin_mod && name in builtin_functions { - p.error_with_pos('cannot redefine builtin function `$name`', pos) + p.error_with_pos('cannot redefine builtin function `$name`', name_pos) return ast.FnDecl{ scope: 0 } @@ -252,8 +252,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { p.error_with_pos('cannot overload `!=`, `>`, `<=` and `>=` as they are auto generated from `==` and`<`', p.tok.position()) } else { - pos := p.tok.position() - p.error_with_pos('expecting method name', pos) + p.error_with_pos('expecting method name', name_pos) return ast.FnDecl{ scope: 0 } @@ -335,8 +334,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.table.known_fn(name) { + p.fn_redefinition_error(name, name_pos) } // p.warn('reg functn $name ()') p.table.register_fn(table.Fn{ @@ -808,7 +807,7 @@ fn (mut p Parser) check_fn_atomic_arguments(typ table.Type, pos token.Position) } } -fn (mut p Parser) fn_redefinition_error(name string) { +fn (mut p Parser) fn_redefinition_error(name string, pos token.Position) { if p.pref.translated { return } @@ -820,7 +819,7 @@ fn (mut p Parser) fn_redefinition_error(name string) { } */ p.table.redefined_fns << name - // p.error('redefinition of function `$name`') + p.error_with_pos('redefinition of function `$name`', pos) } fn have_fn_main(stmts []ast.Stmt) bool { diff --git a/vlib/v/parser/tests/fn_duplicate.out b/vlib/v/parser/tests/fn_duplicate.out new file mode 100644 index 0000000000..21fda15e94 --- /dev/null +++ b/vlib/v/parser/tests/fn_duplicate.out @@ -0,0 +1,4 @@ +vlib/v/parser/tests/fn_duplicate.vv:2:4: error: redefinition of function `foo` + 1 | fn foo() {} + 2 | fn foo() {} + | ~~~ diff --git a/vlib/v/parser/tests/fn_duplicate.vv b/vlib/v/parser/tests/fn_duplicate.vv new file mode 100644 index 0000000000..1eb40d3025 --- /dev/null +++ b/vlib/v/parser/tests/fn_duplicate.vv @@ -0,0 +1,2 @@ +fn foo() {} +fn foo() {}