mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: allow omitting the enum name of map key (#9919)
This commit is contained in:
parent
e711e8634e
commit
191a167f42
@ -6109,14 +6109,16 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
|
||||
typ = typ.set_nr_muls(0)
|
||||
}
|
||||
} else { // [1]
|
||||
index_type := c.expr(node.index)
|
||||
if typ_sym.kind == .map {
|
||||
info := typ_sym.info as ast.Map
|
||||
c.expected_type = info.key_type
|
||||
index_type := c.expr(node.index)
|
||||
if !c.check_types(index_type, info.key_type) {
|
||||
err := c.expected_msg(index_type, info.key_type)
|
||||
c.error('invalid key: $err', node.pos)
|
||||
}
|
||||
} else {
|
||||
index_type := c.expr(node.index)
|
||||
c.check_index(typ_sym, node.index, index_type, node.pos, false)
|
||||
}
|
||||
value_type := c.table.value_type(typ)
|
||||
|
@ -6,10 +6,10 @@ enum Token {
|
||||
|
||||
fn test_map_with_enum_keys() {
|
||||
mut m := map[Token]string{}
|
||||
m[Token.aa] = 'abc'
|
||||
m[.aa] = 'abc'
|
||||
m[Token.bb] = 'def'
|
||||
assert m[Token.aa] == 'abc'
|
||||
assert m[Token.bb] == 'def'
|
||||
assert m[.bb] == 'def'
|
||||
//
|
||||
s := '$m'
|
||||
assert s == "{aa: 'abc', bb: 'def'}"
|
||||
|
Loading…
Reference in New Issue
Block a user