parser: check error for script mode that define main function (fix #14467) (#14469)

yuyi 2022-05-20 19:20:18 +08:00 committed by Jef Roosens
parent b1c2a72118
commit d6fcd24e9d
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 9 additions and 5 deletions

View File

@ -1,5 +0,0 @@
vlib/v/checker/tests/main_and_script_err.vv:1:1: error: function `main` is already defined
1 | fn main() {
| ^
2 | println('main')
3 | }

View File

@ -693,6 +693,10 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
p.script_mode = true
p.script_mode_start_token = p.tok
if p.table.known_fn('main.main') {
p.error('function `main` is already defined, put your script statements inside it')
}
p.open_scope()
mut stmts := []ast.Stmt{}
for p.tok.kind != .eof {

View File

@ -0,0 +1,5 @@
vlib/v/parser/tests/main_and_script_err.vv:4:1: error: function `main` is already defined, put your script statements inside it
2 | println('main')
3 | }
4 | println('out')
| ~~~~~~~