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

checker: check if condition optional (#10921)

This commit is contained in:
Daniel Däschle 2021-07-23 07:52:51 +02:00 committed by GitHub
parent fe5e3c452f
commit a2de3ffcdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -6498,9 +6498,10 @@ 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.expr(branch.cond)
if cond_typ.idx() != ast.bool_type_idx && !c.pref.translated {
typ_sym := c.table.get_type_symbol(cond_typ)
c.error('non-bool type `$typ_sym.name` used as if condition', branch.cond.position())
if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.optional))
&& !c.pref.translated {
c.error('non-bool type `${c.table.type_to_str(cond_typ)}` used as if condition',
branch.cond.position())
}
}
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/if_expr_optional_err.vv:7:5: error: non-bool type `?bool` used as if condition
5 | fn main() {
6 |
7 | if get_bool() {
| ~~~~~~~~~~
8 | println("Using plain lists")
9 | }

View File

@ -0,0 +1,10 @@
fn get_bool() ?bool {
return true
}
fn main() {
if get_bool() {
println("Using plain lists")
}
}