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

v: forbid local variable names, shadowing imported module names (#17197)

This commit is contained in:
ChAoS_UnItY 2023-02-03 15:09:41 +08:00 committed by GitHub
parent 603469b856
commit 0b7a1cd7ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 17 deletions

View File

@ -1424,8 +1424,8 @@ import os { input, user_os }
name := input('Enter your name: ') name := input('Enter your name: ')
println('Name: ${name}') println('Name: ${name}')
os := user_os() current_os := user_os()
println('Your OS is ${os}.') println('Your OS is ${current_os}.')
``` ```
### Module import aliasing ### Module import aliasing

View File

@ -293,16 +293,16 @@ fn main() {
} }
fn test_join() { fn test_join() {
mut strings := ['a', 'b', 'c'] mut strs := ['a', 'b', 'c']
mut s := strings.join(' ') mut s := strs.join(' ')
assert s == 'a b c' assert s == 'a b c'
strings = [ strs = [
'one 'one
two ', two ',
'three! 'three!
four!', four!',
] ]
s = strings.join(' ') s = strs.join(' ')
assert s.contains('one') && s.contains('two ') && s.contains('four') assert s.contains('one') && s.contains('two ') && s.contains('four')
empty := []string{len: 0} empty := []string{len: 0}
assert empty.join('A') == '' assert empty.join('A') == ''

View File

@ -119,9 +119,9 @@ pub fn (mut ws Client) connect() ! {
// listen listens and processes incoming messages // listen listens and processes incoming messages
pub fn (mut ws Client) listen() ! { pub fn (mut ws Client) listen() ! {
mut log := 'Starting client listener, server(${ws.is_server})...' mut log_msg := 'Starting client listener, server(${ws.is_server})...'
ws.logger.info(log) ws.logger.info(log_msg)
unsafe { log.free() } unsafe { log_msg.free() }
defer { defer {
ws.logger.info('Quit client listener, server(${ws.is_server})...') ws.logger.info('Quit client listener, server(${ws.is_server})...')
if ws.state == .open { if ws.state == .open {
@ -143,9 +143,9 @@ pub fn (mut ws Client) listen() ! {
ws.debug_log('got message: ${msg.opcode}') ws.debug_log('got message: ${msg.opcode}')
match msg.opcode { match msg.opcode {
.text_frame { .text_frame {
log = 'read: text' log_msg = 'read: text'
ws.debug_log(log) ws.debug_log(log_msg)
unsafe { log.free() } unsafe { log_msg.free() }
ws.send_message_event(msg) ws.send_message_event(msg)
unsafe { msg.free() } unsafe { msg.free() }
} }
@ -177,9 +177,9 @@ pub fn (mut ws Client) listen() ! {
} }
} }
.close { .close {
log = 'read: close' log_msg = 'read: close'
ws.debug_log(log) ws.debug_log(log_msg)
unsafe { log.free() } unsafe { log_msg.free() }
defer { defer {
ws.manage_clean_close() ws.manage_clean_close()
} }

View File

@ -311,6 +311,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
c.warn('duplicate of a const name `${full_name}`', left.pos) c.warn('duplicate of a const name `${full_name}`', left.pos)
} }
} }
// Check if variable name is already registered as imported module symbol
if c.file.imports.any(it.mod == left.name) {
c.error('duplicate of an import symbol `${left.name}`', left.pos)
}
} }
} }
} }

View File

@ -0,0 +1,17 @@
vlib/v/checker/tests/var_decl_import_sym_conflict.vv:1:8: warning: module 'arrays' is imported but never used
1 | import arrays
| ~~~~~~
2 |
3 | fn x() {
vlib/v/checker/tests/var_decl_import_sym_conflict.vv:4:2: warning: unused variable: `arrays`
2 |
3 | fn x() {
4 | arrays := 1
| ~~~~~~
5 | }
vlib/v/checker/tests/var_decl_import_sym_conflict.vv:4:2: error: duplicate of an import symbol `arrays`
2 |
3 | fn x() {
4 | arrays := 1
| ~~~~~~
5 | }

View File

@ -0,0 +1,5 @@
import arrays
fn x() {
arrays := 1
}

View File

@ -247,9 +247,9 @@ pub fn (prefs &Preferences) should_compile_asm(path string) bool {
if arch != prefs.arch && prefs.arch != ._auto && arch != ._auto { if arch != prefs.arch && prefs.arch != ._auto && arch != ._auto {
return false return false
} }
os := os_from_string(file.all_after_last('_').all_before('.')) or { OS._auto } file_os := os_from_string(file.all_after_last('_').all_before('.')) or { OS._auto }
if os != prefs.os && prefs.os != ._auto && os != ._auto { if file_os != prefs.os && prefs.os != ._auto && file_os != ._auto {
return false return false
} }
return true return true