1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix if cond with alias (fix #17818) (#17821)

This commit is contained in:
yuyi 2023-03-30 19:29:08 +08:00 committed by GitHub
parent 4ef6e16e3b
commit d0702f3897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -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',

View File

@ -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
}
}