diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index a6bb768f52..92ceff3de5 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -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 diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 4b49f3a232..7fa520c1dd 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -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())) { - 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_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())) { + || 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 + } 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())