parser: error for consts in fns

pull/4885/head
yuyi 2020-05-14 00:39:02 +08:00 committed by GitHub
parent 172e4ff853
commit 2e0b9de31c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 0 deletions

View File

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

View File

@ -0,0 +1,4 @@
fn main() {
const (a = 1)
println(a)
}

View File

@ -504,6 +504,9 @@ pub fn (mut p Parser) stmt() ast.Stmt {
} }
} }
else { 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() epos := p.tok.position()
return ast.ExprStmt{ return ast.ExprStmt{
expr: p.expr(0) expr: p.expr(0)