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:
parent
dea9ca2491
commit
3eeef6203e
@ -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
|
||||
}
|
||||
|
7
vlib/v/checker/tests/map_init_key_duplicate_err.out
Normal file
7
vlib/v/checker/tests/map_init_key_duplicate_err.out
Normal 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)
|
8
vlib/v/checker/tests/map_init_key_duplicate_err.vv
Normal file
8
vlib/v/checker/tests/map_init_key_duplicate_err.vv
Normal file
@ -0,0 +1,8 @@
|
||||
fn main() {
|
||||
a := {
|
||||
'foo': 'bar'
|
||||
'abc': 'abc'
|
||||
'foo': 'bar'
|
||||
}
|
||||
println(a)
|
||||
}
|
Loading…
Reference in New Issue
Block a user