diff --git a/vlib/v/checker/tests/const_define_in_function_err.out b/vlib/v/checker/tests/const_define_in_function_err.out new file mode 100644 index 0000000000..7c65f354a1 --- /dev/null +++ b/vlib/v/checker/tests/const_define_in_function_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/const_define_in_function_err.v:2:2: error: const can only be defined at the top level (outside of functions) + 1 | fn main() { + 2 | const (a = 1) + | ~~~~~ + 3 | println(a) + 4 | } diff --git a/vlib/v/checker/tests/const_define_in_function_err.vv b/vlib/v/checker/tests/const_define_in_function_err.vv new file mode 100644 index 0000000000..e6da548076 --- /dev/null +++ b/vlib/v/checker/tests/const_define_in_function_err.vv @@ -0,0 +1,4 @@ +fn main() { + const (a = 1) + println(a) +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index f0da9bfe70..69fc8e15b6 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -504,6 +504,9 @@ pub fn (mut p Parser) stmt() ast.Stmt { } } else { + if p.tok.kind == .key_const { + p.error_with_pos('const can only be defined at the top level (outside of functions)', p.tok.position()) + } epos := p.tok.position() return ast.ExprStmt{ expr: p.expr(0)