Skip to content

Commit 0b7a1cd

Browse files
authored
v: forbid local variable names, shadowing imported module names (#17197)
1 parent 603469b commit 0b7a1cd

7 files changed

Lines changed: 43 additions & 17 deletions

File tree

doc/docs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,8 +1424,8 @@ import os { input, user_os }
14241424
14251425
name := input('Enter your name: ')
14261426
println('Name: ${name}')
1427-
os := user_os()
1428-
println('Your OS is ${os}.')
1427+
current_os := user_os()
1428+
println('Your OS is ${current_os}.')
14291429
```
14301430

14311431
### Module import aliasing

vlib/builtin/string_test.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,16 @@ fn main() {
293293
}
294294

295295
fn test_join() {
296-
mut strings := ['a', 'b', 'c']
297-
mut s := strings.join(' ')
296+
mut strs := ['a', 'b', 'c']
297+
mut s := strs.join(' ')
298298
assert s == 'a b c'
299-
strings = [
299+
strs = [
300300
'one
301301
two ',
302302
'three!
303303
four!',
304304
]
305-
s = strings.join(' ')
305+
s = strs.join(' ')
306306
assert s.contains('one') && s.contains('two ') && s.contains('four')
307307
empty := []string{len: 0}
308308
assert empty.join('A') == ''

vlib/net/websocket/websocket_client.v

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ pub fn (mut ws Client) connect() ! {
119119

120120
// listen listens and processes incoming messages
121121
pub fn (mut ws Client) listen() ! {
122-
mut log := 'Starting client listener, server(${ws.is_server})...'
123-
ws.logger.info(log)
124-
unsafe { log.free() }
122+
mut log_msg := 'Starting client listener, server(${ws.is_server})...'
123+
ws.logger.info(log_msg)
124+
unsafe { log_msg.free() }
125125
defer {
126126
ws.logger.info('Quit client listener, server(${ws.is_server})...')
127127
if ws.state == .open {
@@ -143,9 +143,9 @@ pub fn (mut ws Client) listen() ! {
143143
ws.debug_log('got message: ${msg.opcode}')
144144
match msg.opcode {
145145
.text_frame {
146-
log = 'read: text'
147-
ws.debug_log(log)
148-
unsafe { log.free() }
146+
log_msg = 'read: text'
147+
ws.debug_log(log_msg)
148+
unsafe { log_msg.free() }
149149
ws.send_message_event(msg)
150150
unsafe { msg.free() }
151151
}
@@ -177,9 +177,9 @@ pub fn (mut ws Client) listen() ! {
177177
}
178178
}
179179
.close {
180-
log = 'read: close'
181-
ws.debug_log(log)
182-
unsafe { log.free() }
180+
log_msg = 'read: close'
181+
ws.debug_log(log_msg)
182+
unsafe { log_msg.free() }
183183
defer {
184184
ws.manage_clean_close()
185185
}

vlib/v/checker/assign.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
311311
c.warn('duplicate of a const name `${full_name}`', left.pos)
312312
}
313313
}
314+
// Check if variable name is already registered as imported module symbol
315+
if c.file.imports.any(it.mod == left.name) {
316+
c.error('duplicate of an import symbol `${left.name}`', left.pos)
317+
}
314318
}
315319
}
316320
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
vlib/v/checker/tests/var_decl_import_sym_conflict.vv:1:8: warning: module 'arrays' is imported but never used
2+
1 | import arrays
3+
| ~~~~~~
4+
2 |
5+
3 | fn x() {
6+
vlib/v/checker/tests/var_decl_import_sym_conflict.vv:4:2: warning: unused variable: `arrays`
7+
2 |
8+
3 | fn x() {
9+
4 | arrays := 1
10+
| ~~~~~~
11+
5 | }
12+
vlib/v/checker/tests/var_decl_import_sym_conflict.vv:4:2: error: duplicate of an import symbol `arrays`
13+
2 |
14+
3 | fn x() {
15+
4 | arrays := 1
16+
| ~~~~~~
17+
5 | }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import arrays
2+
3+
fn x() {
4+
arrays := 1
5+
}

vlib/v/pref/should_compile.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ pub fn (prefs &Preferences) should_compile_asm(path string) bool {
247247
if arch != prefs.arch && prefs.arch != ._auto && arch != ._auto {
248248
return false
249249
}
250-
os := os_from_string(file.all_after_last('_').all_before('.')) or { OS._auto }
250+
file_os := os_from_string(file.all_after_last('_').all_before('.')) or { OS._auto }
251251

252-
if os != prefs.os && prefs.os != ._auto && os != ._auto {
252+
if file_os != prefs.os && prefs.os != ._auto && file_os != ._auto {
253253
return false
254254
}
255255
return true

0 commit comments

Comments
 (0)