checker: fix for_in_index type error

pull/5221/head
yuyi 2020-06-05 15:44:25 +08:00 committed by GitHub
parent ef46fbb96f
commit c9b395f9cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 4 deletions

View File

@ -1663,9 +1663,8 @@ fn (mut c Checker) stmt(node ast.Stmt) {
scope.update_var_type(it.key_var, key_type)
}
value_type := c.table.value_type(typ)
if value_type == table.void_type {
typ_sym := c.table.get_type_symbol(typ)
c.error('for in: cannot index `$typ_sym.name`', it.cond.position())
if value_type == table.void_type || typ.has_flag(.optional) {
c.error('for in: cannot index `${c.table.type_to_str(typ)}`', it.cond.position())
}
it.cond_type = typ
it.kind = sym.kind

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/for_in_index_optional.v:3:18: error: for in: cannot index `?[]string`
1 | import os
2 | fn main() {
3 | for file in os.ls('.') {
| ~~~~~~~
4 | println(file)
5 | }

View File

@ -0,0 +1,6 @@
import os
fn main() {
for file in os.ls('.') {
println(file)
}
}

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/for-in-index-type.v:2:11: error: for in: cannot index `any_int`
vlib/v/checker/tests/for_in_index_type.v:2:11: error: for in: cannot index `any_int`
1 | fn main() {
2 | for a in 52 {
| ~~