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

checker: fix for in range type mismatch

This commit is contained in:
yuyi 2020-04-20 06:21:16 +08:00 committed by GitHub
parent 5edd9cdc3a
commit be3bd520f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 0 deletions

View File

@ -1105,6 +1105,16 @@ fn (c mut Checker) stmt(node ast.Stmt) {
c.in_for_count++
typ := c.expr(it.cond)
if it.is_range {
high_type := c.expr(it.high)
if typ in table.integer_type_idxs && high_type !in table.integer_type_idxs {
c.error('range types do not match', it.cond.position())
} else if typ in table.float_type_idxs || high_type in table.float_type_idxs {
c.error('range type can not be float', it.cond.position())
} else if typ == table.bool_type_idx || high_type == table.bool_type_idx {
c.error('range type can not be bool', it.cond.position())
} else if typ == table.string_type_idx || high_type == table.string_type_idx {
c.error('range type can not be string', it.cond.position())
}
c.expr(it.high)
} else {
mut scope := c.file.scope.innermost(it.pos.pos)

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/for_in_range_not_match_type.v:2:11: error: range types do not match
1| fn main() {
2| for i in 10..10.5 {
~~
3| println(i)
4| }

View File

@ -0,0 +1,5 @@
fn main() {
for i in 10..10.5 {
println(i)
}
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/for_in_range_string_type.v:2:11: error: range type can not be string
1| fn main() {
2| for i in 'a'..'b' {
~~~
3| println(i)
4| }

View File

@ -0,0 +1,5 @@
fn main() {
for i in 'a'..'b' {
println(i)
}
}