parser: fix maps with aliases as key (#9900)

pull/9892/head^2
ka-weihe 2021-04-28 06:45:21 +02:00 committed by GitHub
parent 4f246222b0
commit 3edbf71770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -606,6 +606,19 @@ fn test_int_keys() {
m3.delete(1)
}
enum Color {
red
green
blue
}
type ColorAlias = Color
fn test_alias_enum() {
mut m := map[ColorAlias]string{}
m[Color.red] = 'hi'
}
fn test_voidptr_keys() {
mut m := map[voidptr]string{}
v := 5

View File

@ -83,14 +83,14 @@ pub fn (mut p Parser) parse_map_type() ast.Type {
// error is reported in parse_type
return 0
}
if is_alias && !(key_type in [ast.string_type_idx, ast.voidptr_type_idx]
|| ((key_type.is_int() || key_type.is_float()) && !key_type.is_ptr())) {
key_type_supported := key_type in [ast.string_type_idx, ast.voidptr_type_idx]
|| key_sym.kind == .enum_ || ((key_type.is_int() || key_type.is_float()
|| is_alias) && !key_type.is_ptr())
if !key_type_supported {
if is_alias {
p.error('cannot use the alias type as the parent type is unsupported')
return 0
}
if !(key_type in [ast.string_type_idx, ast.voidptr_type_idx]
|| key_sym.kind == .enum_ || ((key_type.is_int() || key_type.is_float()
|| is_alias) && !key_type.is_ptr())) {
s := p.table.type_to_str(key_type)
p.error_with_pos('maps only support string, integer, float, rune, enum or voidptr keys for now (not `$s`)',
p.tok.position())