From bb705c01d2520e87c2d618a52bbc39c9a6fa987b Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Wed, 7 Dec 2022 01:14:33 +0530 Subject: [PATCH] parser: add proper error msg for `:=` used in const decl (#16607) --- vlib/v/parser/parser.v | 3 +++ vlib/v/parser/tests/const_init_decl_assign_err.out | 5 +++++ vlib/v/parser/tests/const_init_decl_assign_err.vv | 3 +++ 3 files changed, 11 insertions(+) create mode 100644 vlib/v/parser/tests/const_init_decl_assign_err.out create mode 100644 vlib/v/parser/tests/const_init_decl_assign_err.vv diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 18891ac5ee..97ec3cd7eb 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3555,6 +3555,9 @@ fn (mut p Parser) const_decl() ast.ConstDecl { if p.tok.kind == .comma { p.error_with_pos('const declaration do not support multiple assign yet', p.tok.pos()) } + if p.tok.kind == .decl_assign { + p.error_with_pos('cannot use `:=` to declare a const, use `=` instead', p.tok.pos()) + } p.check(.assign) end_comments << p.eat_comments() if p.tok.kind == .key_fn { diff --git a/vlib/v/parser/tests/const_init_decl_assign_err.out b/vlib/v/parser/tests/const_init_decl_assign_err.out new file mode 100644 index 0000000000..9099e5f7f4 --- /dev/null +++ b/vlib/v/parser/tests/const_init_decl_assign_err.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/const_init_decl_assign_err.vv:2:4: error: cannot use `:=` to declare a const, use `=` instead + 1 | const ( + 2 | a := 43 + | ~~ + 3 | ) diff --git a/vlib/v/parser/tests/const_init_decl_assign_err.vv b/vlib/v/parser/tests/const_init_decl_assign_err.vv new file mode 100644 index 0000000000..9b9cafba4a --- /dev/null +++ b/vlib/v/parser/tests/const_init_decl_assign_err.vv @@ -0,0 +1,3 @@ +const ( + a := 43 +)