From 2f96bff64a7f1946075b49c41befe090744f25b6 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Mon, 17 Nov 2025 02:52:10 +0800 Subject: [PATCH] checker: fix comptime main fn (#25747) --- vlib/v/checker/checker.v | 30 ++++++++++++++++--- .../tests/run/comptime_main_fn.run.out | 1 + vlib/v/checker/tests/run/comptime_main_fn.vv | 9 ++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 vlib/v/checker/tests/run/comptime_main_fn.run.out create mode 100644 vlib/v/checker/tests/run/comptime_main_fn.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4943f426e..c10b89618 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -476,11 +476,27 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) { } } -// do checks specific to files in main module -// returns `true` if a main function is in the file -fn (mut c Checker) file_has_main_fn(file &ast.File) bool { +fn (mut c Checker) stmts_has_main_fn(stmts []ast.Stmt) bool { mut has_main_fn := false - for stmt in file.stmts { + for stmt in stmts { + if stmt is ast.ExprStmt { + // top level comptime main fn + if stmt.expr is ast.IfExpr && stmt.expr.is_comptime { + // $if a ? { fn main(){} } $else { fn main() {} } + for branch in stmt.expr.branches { + if c.stmts_has_main_fn(branch.stmts) { + has_main_fn = true + } + } + } else if stmt.expr is ast.MatchExpr && stmt.expr.is_comptime { + // $match os { 'windows' { fn main() {} } ... + for branch in stmt.expr.branches { + if c.stmts_has_main_fn(branch.stmts) { + has_main_fn = true + } + } + } + } if stmt is ast.FnDecl { if stmt.name == 'main.main' { if has_main_fn { @@ -504,6 +520,12 @@ fn (mut c Checker) file_has_main_fn(file &ast.File) bool { return has_main_fn } +// do checks specific to files in main module +// returns `true` if a main function is in the file +fn (mut c Checker) file_has_main_fn(file &ast.File) bool { + return c.stmts_has_main_fn(file.stmts) +} + @[direct_array_access] fn (mut c Checker) check_valid_snake_case(name string, identifier string, pos token.Pos) { if c.pref.translated || c.file.is_translated { diff --git a/vlib/v/checker/tests/run/comptime_main_fn.run.out b/vlib/v/checker/tests/run/comptime_main_fn.run.out new file mode 100644 index 000000000..204118454 --- /dev/null +++ b/vlib/v/checker/tests/run/comptime_main_fn.run.out @@ -0,0 +1 @@ +main2 diff --git a/vlib/v/checker/tests/run/comptime_main_fn.vv b/vlib/v/checker/tests/run/comptime_main_fn.vv new file mode 100644 index 000000000..229329996 --- /dev/null +++ b/vlib/v/checker/tests/run/comptime_main_fn.vv @@ -0,0 +1,9 @@ +$if usemain1 ? { + fn main() { + println('main1') + } +} $else { + fn main() { + println('main2') + } +} -- 2.39.5