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

checker: check for map_init key duplicate

This commit is contained in:
yuyi 2020-05-09 22:55:38 +08:00 committed by GitHub
parent dea9ca2491
commit 3eeef6203e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -2139,6 +2139,13 @@ pub fn (mut c Checker) map_init(node mut ast.MapInit) table.Type {
key0_type := c.expr(node.keys[0])
val0_type := c.expr(node.vals[0])
for i, key in node.keys {
key_i := key as ast.StringLiteral
for j in 0..i {
key_j := node.keys[j] as ast.StringLiteral
if key_i.val == key_j.val {
c.error('duplicate key "$key_i.val" in map literal', key.position())
}
}
if i == 0 {
continue
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/map_init_key_duplicate_err.v:5:3: error: duplicate key "foo" in map literal
3 | 'foo': 'bar'
4 | 'abc': 'abc'
5 | 'foo': 'bar'
| ~~~~~
6 | }
7 | println(a)

View File

@ -0,0 +1,8 @@
fn main() {
a := {
'foo': 'bar'
'abc': 'abc'
'foo': 'bar'
}
println(a)
}