From 689f3f712862940866c89e65ffceb2d2c1175817 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 16 May 2022 14:48:21 +0800 Subject: [PATCH] fmt: fix fmt error of anon fn with if expr (fix #14393) (#14413) --- vlib/v/fmt/fmt.v | 3 ++- vlib/v/fmt/tests/anon_fn_with_if_expr_expected.vv | 10 ++++++++++ vlib/v/fmt/tests/anon_fn_with_if_expr_input.vv | 10 ++++++++++ vlib/v/fmt/tests/shared_input.vv | 3 +-- vlib/v/tests/bench/math_big_gcd/bench_euclid.v | 6 +++++- 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 vlib/v/fmt/tests/anon_fn_with_if_expr_expected.vv create mode 100644 vlib/v/fmt/tests/anon_fn_with_if_expr_input.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 92e762b585..60899e5af6 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1951,7 +1951,8 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) { } fn branch_is_single_line(b ast.IfBranch) bool { - if b.stmts.len == 1 && b.comments.len == 0 && stmt_is_single_line(b.stmts[0]) { + if b.stmts.len == 1 && b.comments.len == 0 && stmt_is_single_line(b.stmts[0]) + && b.pos.line_nr == b.stmts[0].pos.line_nr { return true } return false diff --git a/vlib/v/fmt/tests/anon_fn_with_if_expr_expected.vv b/vlib/v/fmt/tests/anon_fn_with_if_expr_expected.vv new file mode 100644 index 0000000000..ac8992d063 --- /dev/null +++ b/vlib/v/fmt/tests/anon_fn_with_if_expr_expected.vv @@ -0,0 +1,10 @@ +fn main() { + a := fn () { + if true { + println('a') + } else { + println('a') + } + } + a() +} diff --git a/vlib/v/fmt/tests/anon_fn_with_if_expr_input.vv b/vlib/v/fmt/tests/anon_fn_with_if_expr_input.vv new file mode 100644 index 0000000000..7da1079e05 --- /dev/null +++ b/vlib/v/fmt/tests/anon_fn_with_if_expr_input.vv @@ -0,0 +1,10 @@ +fn main() { +a := fn () { + if true { + println('a') + } else { + println('a') + } +} + a() +} diff --git a/vlib/v/fmt/tests/shared_input.vv b/vlib/v/fmt/tests/shared_input.vv index 9ebf6a7f7f..800a5fc67d 100644 --- a/vlib/v/fmt/tests/shared_input.vv +++ b/vlib/v/fmt/tests/shared_input.vv @@ -64,8 +64,7 @@ a: 5 lock x { // wait for ongoing reads to finish, don't start new ones x.a = 17 // this value should never be read time.sleep(50* time.millisecond) - x.a = if (i & 1) == 0 { -7} else {5} + x.a = if (i & 1) == 0 { 7 } else { 5 } } // now new reads are possible again time.sleep(20*time.millisecond) } diff --git a/vlib/v/tests/bench/math_big_gcd/bench_euclid.v b/vlib/v/tests/bench/math_big_gcd/bench_euclid.v index ec5091b1c5..600a8f280c 100644 --- a/vlib/v/tests/bench/math_big_gcd/bench_euclid.v +++ b/vlib/v/tests/bench/math_big_gcd/bench_euclid.v @@ -60,7 +60,11 @@ fn main() { (aa % gcd) == big.zero_int, (bb % gcd) == big.zero_int, ].all(it == true) - { true } else { false } + { + true + } else { + false + } } cfgs := [