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

checker: reject void type condition (#8737)

This commit is contained in:
zakuro 2021-02-14 15:33:24 +09:00 committed by GitHub
parent 3d29f819cf
commit e4f15605c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 5 deletions

View File

@ -213,7 +213,7 @@ fn C.RemoveDirectory() int
fn C.GetStdHandle(u32) voidptr
// fn C.SetConsoleMode()
fn C.SetConsoleMode(voidptr, u32)
fn C.SetConsoleMode(voidptr, u32) int
// fn C.GetConsoleMode() int
fn C.GetConsoleMode(voidptr, &u32) int

View File

@ -4755,10 +4755,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
// check condition type is boolean
c.expected_type = table.bool_type
cond_typ := c.expr(branch.cond)
if cond_typ.idx() !in [table.bool_type_idx, table.void_type_idx]
&& !c.pref.translated {
// void types are skipped, because they mean the var was initialized incorrectly
// (via missing function etc)
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)
}

View File

@ -0,0 +1,20 @@
vlib/v/checker/tests/if_non_bool_cond.vv:2:2: 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
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
8 | }
9 |
10 | if println('v') {
| ~~~~~~~~~~~~~~~
11 | println('println')
12 | }

View File

@ -0,0 +1,13 @@
fn main() {
if '10' {
println('10')
}
if 5 {
println(5)
}
if println('v') {
println('println')
}
}