From b0e7ddfd976e0796dd550f0353e9dd3fd0489480 Mon Sep 17 00:00:00 2001 From: ChAoS_UnItY Date: Fri, 26 Aug 2022 12:08:57 +0800 Subject: [PATCH] checker: fix non-bool check on use of result bool (fix #15539) (#15540) --- vlib/v/checker/if.v | 4 ++-- vlib/v/checker/tests/if_expr_optional_err.out | 10 +++++----- vlib/v/checker/tests/if_expr_optional_err.vv | 1 - vlib/v/checker/tests/if_expr_result_err.out | 7 +++++++ vlib/v/checker/tests/if_expr_result_err.vv | 9 +++++++++ 5 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 vlib/v/checker/tests/if_expr_result_err.out create mode 100644 vlib/v/checker/tests/if_expr_result_err.vv diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 473ebaf3e..f3287636b 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -47,8 +47,8 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { // check condition type is boolean c.expected_type = ast.bool_type cond_typ := c.unwrap_generic(c.expr(branch.cond)) - if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.optional)) - && !c.pref.translated && !c.file.is_translated { + if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.optional) + || cond_typ.has_flag(.result)) && !c.pref.translated && !c.file.is_translated { c.error('non-bool type `${c.table.type_to_str(cond_typ)}` used as if condition', branch.cond.pos()) } diff --git a/vlib/v/checker/tests/if_expr_optional_err.out b/vlib/v/checker/tests/if_expr_optional_err.out index 9adbc4a24..9df929bfc 100644 --- a/vlib/v/checker/tests/if_expr_optional_err.out +++ b/vlib/v/checker/tests/if_expr_optional_err.out @@ -1,7 +1,7 @@ -vlib/v/checker/tests/if_expr_optional_err.vv:7:5: error: non-bool type `?bool` used as if condition +vlib/v/checker/tests/if_expr_optional_err.vv:6:5: error: non-bool type `?bool` used as if condition + 4 | 5 | fn main() { - 6 | - 7 | if get_bool() { + 6 | if get_bool() { | ~~~~~~~~~~ - 8 | println("Using plain lists") - 9 | } + 7 | println("Using plain lists") + 8 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/if_expr_optional_err.vv b/vlib/v/checker/tests/if_expr_optional_err.vv index 88d617d43..432e4bb7b 100644 --- a/vlib/v/checker/tests/if_expr_optional_err.vv +++ b/vlib/v/checker/tests/if_expr_optional_err.vv @@ -3,7 +3,6 @@ fn get_bool() ?bool { } fn main() { - if get_bool() { println("Using plain lists") } diff --git a/vlib/v/checker/tests/if_expr_result_err.out b/vlib/v/checker/tests/if_expr_result_err.out new file mode 100644 index 000000000..c67e1f8c0 --- /dev/null +++ b/vlib/v/checker/tests/if_expr_result_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/if_expr_result_err.vv:6:5: error: non-bool type `!bool` used as if condition + 4 | + 5 | fn main() { + 6 | if get_bool() { + | ~~~~~~~~~~ + 7 | println("Using plain lists") + 8 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/if_expr_result_err.vv b/vlib/v/checker/tests/if_expr_result_err.vv new file mode 100644 index 000000000..1dcce464f --- /dev/null +++ b/vlib/v/checker/tests/if_expr_result_err.vv @@ -0,0 +1,9 @@ +fn get_bool() !bool { + return true +} + +fn main() { + if get_bool() { + println("Using plain lists") + } +} -- 2.30.2