mirror of
https://github.com/vlang/v.git
synced 2025-09-14 06:52:36 +03:00
checker: add a notice for global variable redeclarations (#23162)
This commit is contained in:
parent
a1de8dbd10
commit
00148d11fc
3 changed files with 17 additions and 0 deletions
|
@ -139,6 +139,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
|||
c.error('cannot reassign using range expression on the left side of an assignment',
|
||||
left.pos)
|
||||
}
|
||||
} else if mut left is ast.Ident && node.op == .decl_assign {
|
||||
if left.name in c.global_names {
|
||||
c.note('the global variable named `${left.name}` already exists', left.pos)
|
||||
}
|
||||
}
|
||||
is_blank_ident := left.is_blank_ident()
|
||||
mut left_type := ast.void_type
|
||||
|
|
7
vlib/v/checker/tests/globals/global_var_redeclare.out
Normal file
7
vlib/v/checker/tests/globals/global_var_redeclare.out
Normal file
|
@ -0,0 +1,7 @@
|
|||
vlib/v/checker/tests/globals/global_var_redeclare.vv:4:2: notice: the global variable named `foobar` already exists
|
||||
2 |
|
||||
3 | fn main() {
|
||||
4 | foobar := 2
|
||||
| ~~~~~~
|
||||
5 | println(foobar)
|
||||
6 | }
|
6
vlib/v/checker/tests/globals/global_var_redeclare.vv
Normal file
6
vlib/v/checker/tests/globals/global_var_redeclare.vv
Normal file
|
@ -0,0 +1,6 @@
|
|||
__global foobar = 1
|
||||
|
||||
fn main() {
|
||||
foobar := 2
|
||||
println(foobar)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue