checker: add a notice for global variable redeclarations (#23162)

This commit is contained in:
Felipe Pena 2024-12-17 11:34:44 -03:00 committed by GitHub
parent a1de8dbd10
commit 00148d11fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 0 deletions

View file

@ -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', c.error('cannot reassign using range expression on the left side of an assignment',
left.pos) 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() is_blank_ident := left.is_blank_ident()
mut left_type := ast.void_type mut left_type := ast.void_type

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

View file

@ -0,0 +1,6 @@
__global foobar = 1
fn main() {
foobar := 2
println(foobar)
}