From d0702f3897adc6ea31f2010cb396a6c65368ef2a Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 30 Mar 2023 19:29:08 +0800 Subject: [PATCH] checker: fix if cond with alias (fix #17818) (#17821) --- vlib/v/checker/if.v | 2 +- vlib/v/tests/if_cond_with_alias_test.v | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/if_cond_with_alias_test.v diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 91134d102a..2dad0887dc 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -62,7 +62,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } else { // check condition type is boolean c.expected_type = ast.bool_type - cond_typ := c.unwrap_generic(c.expr(branch.cond)) + cond_typ := c.table.unaliased_type(c.unwrap_generic(c.expr(branch.cond))) if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.option) || 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', diff --git a/vlib/v/tests/if_cond_with_alias_test.v b/vlib/v/tests/if_cond_with_alias_test.v new file mode 100644 index 0000000000..9b99c4fe08 --- /dev/null +++ b/vlib/v/tests/if_cond_with_alias_test.v @@ -0,0 +1,14 @@ +type BOOL = bool + +fn example() BOOL { + return true +} + +fn test_if_cond_with_alias() { + if example() { + println('Should work, or not?') + assert true + } else { + assert false + } +}