1
0
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:
zakuro 2021-04-29 14:33:41 +09:00 committed by GitHub
parent e711e8634e
commit 191a167f42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -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)

View File

@ -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'}"