From 148bb31f6ea1ecd9e5e3160160079e710beb534a Mon Sep 17 00:00:00 2001 From: Wertzui123 <46199283+Wertzui123@users.noreply.github.com> Date: Tue, 1 Jun 2021 11:19:39 +0200 Subject: [PATCH] v.parser: allow for `if x { $if y {} } else {}`, fix #10243 (#10294) --- vlib/v/parser/if_match.v | 2 +- vlib/v/tests/comptime_if_test.v | 54 ++++++++++++++++++++ vlib/v/tests/comptime_if_test_support_test.v | 10 ---- 3 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 vlib/v/tests/comptime_if_test.v delete mode 100644 vlib/v/tests/comptime_if_test_support_test.v diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index 513b4bbe6d..caf103669a 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -134,7 +134,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { p.error('use `\$else` instead of `else` in compile-time `if` branches') return ast.IfExpr{} } - if p.peek_tok.kind == .key_else { + if p.tok.kind != .rcbr && p.peek_tok.kind == .key_else { p.check(.dollar) } } diff --git a/vlib/v/tests/comptime_if_test.v b/vlib/v/tests/comptime_if_test.v new file mode 100644 index 0000000000..4e5d60d0e1 --- /dev/null +++ b/vlib/v/tests/comptime_if_test.v @@ -0,0 +1,54 @@ +fn test_comptime_if_test() { + mut i := 0 + $if test { + i++ + } + $if !test { + i-- + } + assert i == 1 +} + +fn test_comptime_if_parsing_in_combination_with_ordinary_if_1() { + if true { + $if debug { + println('debug') + } + } else { + assert false + } + assert true +} + +fn test_comptime_if_parsing_in_combination_with_ordinary_if_2() { + if true { + if true { + $if debug { + println('debug') + } + } else { + assert false + } + } else { + assert false + } + assert true +} + +fn test_comptime_if_parsing_in_combination_with_ordinary_if_3() { + println(@LINE) + $if true { + println(@LINE) + $if true { + println(@LINE) + $if debug { + println('debug') + } + } $else { + assert false + } + } $else { + assert false + } + assert true +} diff --git a/vlib/v/tests/comptime_if_test_support_test.v b/vlib/v/tests/comptime_if_test_support_test.v deleted file mode 100644 index 5eaf2bbd7e..0000000000 --- a/vlib/v/tests/comptime_if_test_support_test.v +++ /dev/null @@ -1,10 +0,0 @@ -fn test_comptime_if_test() { - mut i := 0 - $if test { - i++ - } - $if !test { - i-- - } - assert i == 1 -}