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

master
yuyi 2022-05-20 19:20:18 +08:00 committed by GitHub
parent 913164bc73
commit 28b0cbddad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 = true
p.script_mode_start_token = p.tok 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() p.open_scope()
mut stmts := []ast.Stmt{} mut stmts := []ast.Stmt{}
for p.tok.kind != .eof { 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')
| ~~~~~~~