checker: report correct position for non-bool cond error (#8735)

pull/8757/head
zakuro 2021-02-15 03:19:41 +09:00 committed by GitHub
parent 82650ee813
commit e534b4397d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 7 deletions

View File

@ -4757,7 +4757,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
cond_typ := c.expr(branch.cond)
if cond_typ.idx() != table.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.pos)
c.error('non-bool type `$typ_sym.name` used as if condition', branch.cond.position())
}
}
}

View File

@ -1,20 +1,20 @@
vlib/v/checker/tests/if_non_bool_cond.vv:2:2: error: non-bool type `string` used as if condition
vlib/v/checker/tests/if_non_bool_cond.vv:2:5: error: non-bool type `string` used as if condition
1 | fn main() {
2 | if '10' {
| ~~~~~~~
| ~~~~
3 | println('10')
4 | }
vlib/v/checker/tests/if_non_bool_cond.vv:6:2: error: non-bool type `int literal` used as if condition
vlib/v/checker/tests/if_non_bool_cond.vv:6:5: error: non-bool type `int literal` used as if condition
4 | }
5 |
6 | if 5 {
| ~~~~
| ^
7 | println(5)
8 | }
vlib/v/checker/tests/if_non_bool_cond.vv:10:2: error: non-bool type `void` used as if condition
vlib/v/checker/tests/if_non_bool_cond.vv:10:5: error: non-bool type `void` used as if condition
8 | }
9 |
10 | if println('v') {
| ~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~
11 | println('println')
12 | }