Skip to content

Commit 00148d1

Browse files
authored
checker: add a notice for global variable redeclarations (#23162)
1 parent a1de8db commit 00148d1

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

vlib/v/checker/assign.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
139139
c.error('cannot reassign using range expression on the left side of an assignment',
140140
left.pos)
141141
}
142+
} else if mut left is ast.Ident && node.op == .decl_assign {
143+
if left.name in c.global_names {
144+
c.note('the global variable named `${left.name}` already exists', left.pos)
145+
}
142146
}
143147
is_blank_ident := left.is_blank_ident()
144148
mut left_type := ast.void_type
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/globals/global_var_redeclare.vv:4:2: notice: the global variable named `foobar` already exists
2+
2 |
3+
3 | fn main() {
4+
4 | foobar := 2
5+
| ~~~~~~
6+
5 | println(foobar)
7+
6 | }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__global foobar = 1
2+
3+
fn main() {
4+
foobar := 2
5+
println(foobar)
6+
}

0 commit comments

Comments
 (0)