diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index cf4330f83d..199700dbd4 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -191,6 +191,9 @@ fn (mut c Checker) check_file_in_main(file ast.File) bool { } ast.FnDecl { if stmt.name == 'main.main' { + if has_main_fn { + c.error('function `main` is already defined', stmt.pos) + } has_main_fn = true if stmt.is_pub { c.error('function `main` cannot be declared public', stmt.pos) diff --git a/vlib/v/checker/tests/main_and_script_err.out b/vlib/v/checker/tests/main_and_script_err.out new file mode 100644 index 0000000000..71b5f75084 --- /dev/null +++ b/vlib/v/checker/tests/main_and_script_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/main_and_script_err.vv:1:1: error: function `main` is already defined + 1 | fn main() { + | ^ + 2 | println('main') + 3 | } diff --git a/vlib/v/checker/tests/main_and_script_err.vv b/vlib/v/checker/tests/main_and_script_err.vv new file mode 100644 index 0000000000..e746460bd9 --- /dev/null +++ b/vlib/v/checker/tests/main_and_script_err.vv @@ -0,0 +1,4 @@ +fn main() { + println('main') +} +println('out')