diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index f40c4151a3..b47c9e1dc0 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -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 diff --git a/vlib/v/checker/tests/globals/global_var_redeclare.out b/vlib/v/checker/tests/globals/global_var_redeclare.out new file mode 100644 index 0000000000..c94c052e7a --- /dev/null +++ b/vlib/v/checker/tests/globals/global_var_redeclare.out @@ -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 | } diff --git a/vlib/v/checker/tests/globals/global_var_redeclare.vv b/vlib/v/checker/tests/globals/global_var_redeclare.vv new file mode 100644 index 0000000000..47ce3638af --- /dev/null +++ b/vlib/v/checker/tests/globals/global_var_redeclare.vv @@ -0,0 +1,6 @@ +__global foobar = 1 + +fn main() { + foobar := 2 + println(foobar) +}