checker: check empty map invalid syntax (fix #9162) (#9186)

pull/9199/head
yuyi 2021-03-09 01:48:17 +08:00 committed by GitHub
parent 9a7d9e047b
commit 10c9f61d61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 34 deletions

View File

@ -5501,6 +5501,9 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type {
node.key_type = info.key_type node.key_type = info.key_type
node.value_type = info.value_type node.value_type = info.value_type
return node.typ return node.typ
} else {
c.error('invalid empty map initilization syntax, use e.g. map[string]int{} instead',
node.pos)
} }
} }
// `x := map[string]string` - set in parser // `x := map[string]string` - set in parser
@ -5512,6 +5515,7 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type {
node.value_type = info.value_type node.value_type = info.value_type
return node.typ return node.typ
} }
if node.keys.len > 0 && node.vals.len > 0 {
// `{'age': 20}` // `{'age': 20}`
mut key0_type := c.table.mktyp(c.expr(node.keys[0])) mut key0_type := c.table.mktyp(c.expr(node.keys[0]))
if node.keys[0].is_auto_deref_var() { if node.keys[0].is_auto_deref_var() {
@ -5550,6 +5554,8 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type {
node.key_type = key0_type node.key_type = key0_type
node.value_type = val0_type node.value_type = val0_type
return map_type return map_type
}
return node.typ
} }
// call this *before* calling error or warn // call this *before* calling error or warn

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/map_init_invalid_syntax.vv:2:10: error: invalid empty map initilization syntax, use e.g. map[string]int{} instead
1 | fn main() {
2 | a := map{}
| ~~
3 | println(a)
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
a := map{}
println(a)
}