From 5ec7ee916a106f8f4ea3dc0e3cd568b0f0635a40 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 3 Aug 2023 14:30:00 +0800 Subject: [PATCH] parser: fix formatting comptime if expr script (v fmt no longer eats the body of top level `$if xyz ? {}`) (#19044) --- vlib/v/fmt/tests/comptime_if_expr_script_keep.vv | 6 ++++++ vlib/v/parser/parser.v | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/comptime_if_expr_script_keep.vv diff --git a/vlib/v/fmt/tests/comptime_if_expr_script_keep.vv b/vlib/v/fmt/tests/comptime_if_expr_script_keep.vv new file mode 100644 index 0000000000..a9e64e9d6e --- /dev/null +++ b/vlib/v/fmt/tests/comptime_if_expr_script_keep.vv @@ -0,0 +1,6 @@ +mut pre_built_str := '' +$if prebuilt ? { + the_date := if $env('DATE') != '' { $env('DATE') } else { '##DATE##' } + pre_built_str = '[pre-built binary release (${the_date})]\n' +} +dump(pre_built_str) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index bb698b244a..9de815ae7b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -764,7 +764,7 @@ fn (mut p Parser) top_stmt() ast.Stmt { expr: if_expr pos: if_expr.pos } - if comptime_if_expr_contains_top_stmt(if_expr) { + if p.pref.is_fmt || comptime_if_expr_contains_top_stmt(if_expr) { return cur_stmt } else { return p.other_stmts(cur_stmt)