mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: fix maps with aliases as key (#9900)
This commit is contained in:
parent
4f246222b0
commit
3edbf71770
@ -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
|
||||
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user