parser: make duplicated functions an error (#8792)

pull/8811/head
zakuro 2021-02-18 04:50:10 +09:00 committed by GitHub
parent 33d8074846
commit 21bf8fe14e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 33 additions and 163 deletions

View File

@ -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

View File

@ -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 {}

View File

@ -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
}

View File

@ -4,9 +4,6 @@ import sync
import runtime
[trusted]
fn C.atomic_fetch_add_u32(voidptr, u32) u32
pub const (
no_result = voidptr(0)
)

View File

@ -8,25 +8,6 @@ import time
#flag -lpthread
#include <semaphore.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.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 {

View File

@ -9,27 +9,6 @@ import time
#include <semaphore.h>
#include <sys/errno.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
// [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 {

View File

@ -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]

View File

@ -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

View File

@ -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 {

View File

@ -0,0 +1,4 @@
vlib/v/parser/tests/fn_duplicate.vv:2:4: error: redefinition of function `foo`
1 | fn foo() {}
2 | fn foo() {}
| ~~~

View File

@ -0,0 +1,2 @@
fn foo() {}
fn foo() {}