checker: check redefine global 'main' function (#13803)

pull/13808/head
yuyi 2022-03-22 23:39:12 +08:00 committed by GitHub
parent 894080b844
commit 0337882240
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 0 deletions

View File

@ -2097,6 +2097,10 @@ fn (mut c Checker) global_decl(mut node ast.GlobalDecl) {
c.error('unknown type `$sym.name`', field.typ_pos) c.error('unknown type `$sym.name`', field.typ_pos)
} }
if field.has_expr { if field.has_expr {
if field.expr is ast.AnonFn && field.name == 'main' {
c.error('the `main` function is the program entry point, cannot redefine it',
field.pos)
}
field.typ = c.expr(field.expr) field.typ = c.expr(field.expr)
mut v := c.file.global_scope.find_global(field.name) or { mut v := c.file.global_scope.find_global(field.name) or {
panic('internal compiler error - could not find global in scope') panic('internal compiler error - could not find global in scope')

View File

@ -0,0 +1,3 @@
vlib/v/checker/tests/globals/redefine_main.vv:1:10: error: the `main` function is the program entry point, cannot redefine it
1 | __global main = fn () int { return 22 }
| ~~~~

View File

@ -0,0 +1 @@
__global main = fn () int { return 22 }