Skip to content

Commit b158da3

Browse files
authored
parser: fix checking for duplicate main functions (#15946)
1 parent ed2960a commit b158da3

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

vlib/v/parser/fn.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module parser
66
import v.ast
77
import v.token
88
import v.util
9+
import os
910

1011
pub fn (mut p Parser) call_expr(language ast.Language, mod string) ast.CallExpr {
1112
first_pos := p.tok.pos()
@@ -399,6 +400,16 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
399400
is_test := (!is_method && params.len == 0) && p.inside_test_file
400401
&& (short_fn_name.starts_with('test_') || short_fn_name.starts_with('testsuite_'))
401402
file_mode := p.file_backend_mode
403+
if is_main {
404+
if _ := p.table.find_fn('main.main') {
405+
if '.' in os.args {
406+
p.error_with_pos('multiple `main` functions detected, and you ran `v .`
407+
perhaps there are multiple V programs in this directory, and you need to
408+
run them via `v file.v` instead',
409+
name_pos)
410+
}
411+
}
412+
}
402413
// Register
403414
if is_method {
404415
mut type_sym := p.table.sym(rec.typ)

vlib/v/parser/struct.v

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module parser
66
import v.ast
77
import v.token
88
import v.util
9-
import os
109

1110
fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
1211
p.top_level_statement_start()
@@ -361,15 +360,6 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
361360
}
362361
// allow duplicate c struct declarations
363362
if ret == -1 && language != .c {
364-
if _ := p.table.find_fn('main.main') {
365-
if '.' in os.args {
366-
p.error_with_pos('multiple `main` functions detected, and you ran `v .`
367-
perhaps there are multiple V programs in this directory, and you need to
368-
run them via `v file.v` instead',
369-
name_pos)
370-
return ast.StructDecl{}
371-
}
372-
}
373363
p.error_with_pos('cannot register struct `$name`, another type with this name exists',
374364
name_pos)
375365
return ast.StructDecl{}

0 commit comments

Comments
 (0)