diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 725120bb61..029ec9da22 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2621,6 +2621,9 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type { return info.typ } else if ident.kind == .unresolved { // first use + if ident.tok_kind == .assign && ident.is_mut { + c.error('`mut` not allowed with `=` (use `:=` to declare a variable)', ident.pos) + } start_scope := c.file.scope.innermost(ident.pos.pos) if obj1 := start_scope.find(ident.name) { match mut obj1 as obj { @@ -2731,7 +2734,7 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type { ident.mod = saved_mod } if ident.tok_kind == .assign { - c.error('undefined ident: `$ident.name` (use `:=` to assign a variable)', + c.error('undefined ident: `$ident.name` (use `:=` to declare a variable)', ident.pos) } else { c.error('undefined ident: `$ident.name`', ident.pos) diff --git a/vlib/v/checker/tests/assign_mut.out b/vlib/v/checker/tests/assign_mut.out new file mode 100644 index 0000000000..178baf4817 --- /dev/null +++ b/vlib/v/checker/tests/assign_mut.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/assign_mut.vv:3:9: error: `mut` not allowed with `=` (use `:=` to declare a variable) + 1 | fn main() { + 2 | mut z := 1 + 3 | mut z = 1 + | ^ + 4 | mut i := 2 + 5 | i, mut z = 2,3 +vlib/v/checker/tests/assign_mut.vv:5:12: error: `mut` not allowed with `=` (use `:=` to declare a variable) + 3 | mut z = 1 + 4 | mut i := 2 + 5 | i, mut z = 2,3 + | ^ + 6 | } diff --git a/vlib/v/checker/tests/assign_mut.vv b/vlib/v/checker/tests/assign_mut.vv new file mode 100644 index 0000000000..be80643ee8 --- /dev/null +++ b/vlib/v/checker/tests/assign_mut.vv @@ -0,0 +1,6 @@ +fn main() { + mut z := 1 + mut z = 1 + mut i := 2 + i, mut z = 2,3 +} diff --git a/vlib/v/checker/tests/error_with_several_comments_with_crlf_ending.out b/vlib/v/checker/tests/error_with_several_comments_with_crlf_ending.out index 883c3e919b..d1b88fa9b7 100644 Binary files a/vlib/v/checker/tests/error_with_several_comments_with_crlf_ending.out and b/vlib/v/checker/tests/error_with_several_comments_with_crlf_ending.out differ diff --git a/vlib/v/checker/tests/unknown_var_assign.out b/vlib/v/checker/tests/unknown_var_assign.out index 566768d9fb..6a12a8cbbc 100644 --- a/vlib/v/checker/tests/unknown_var_assign.out +++ b/vlib/v/checker/tests/unknown_var_assign.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/unknown_var_assign.vv:2:5: error: undefined ident: `x` (use `:=` to assign a variable) +vlib/v/checker/tests/unknown_var_assign.vv:2:5: error: undefined ident: `x` (use `:=` to declare a variable) 1 | fn main() { 2 | x = 'hello v' | ^ diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 005b0c824a..c8d07c04ef 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -30,7 +30,7 @@ pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExp // `foo(10)` p.next() // `<` p.expr_mod = '' - mut generic_type = p.parse_type() + generic_type = p.parse_type() p.check(.gt) // `>` // In case of `foo()` // T is unwrapped and registered in the checker.