checker: check for_in_map using one variable error

pull/4794/head
yuyi 2020-05-10 08:28:43 +08:00 committed by GitHub
parent 10da871743
commit 85723e3799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View File

@ -1472,6 +1472,9 @@ fn (mut c Checker) stmt(node ast.Stmt) {
} else {
mut scope := c.file.scope.innermost(it.pos.pos)
sym := c.table.get_type_symbol(typ)
if sym.kind == .map && !(it.key_var.len > 0 && it.val_var.len > 0) {
c.error('for in: cannot use one variable in map', it.pos)
}
if it.key_var.len > 0 {
key_type := match sym.kind {
.map { sym.map_info().key_type }

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/for_in_map_one_variable_err.v:3:6: error: for in: cannot use one variable in map
1 | fn main() {
2 | kvs := {'foo':'bar'}
3 | for k in kvs {
| ^
4 | println('$k')
5 | }

View File

@ -0,0 +1,6 @@
fn main() {
kvs := {'foo':'bar'}
for k in kvs {
println('$k')
}
}