checker: check for map_init key duplicate
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])
|
key0_type := c.expr(node.keys[0])
|
||||||
val0_type := c.expr(node.vals[0])
|
val0_type := c.expr(node.vals[0])
|
||||||
for i, key in node.keys {
|
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 {
|
if i == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
|
@ -0,0 +1,8 @@
|
||||||
|
fn main() {
|
||||||
|
a := {
|
||||||
|
'foo': 'bar'
|
||||||
|
'abc': 'abc'
|
||||||
|
'foo': 'bar'
|
||||||
|
}
|
||||||
|
println(a)
|
||||||
|
}
|
Loading…
Reference in New Issue