Skip to content

Commit 34c6787

Browse files
authored
checker,cgen: allow alias to map type Dict = map[string]string (fix #24878) (#24883)
1 parent f80fc37 commit 34c6787

File tree

6 files changed

+15
-23
lines changed

6 files changed

+15
-23
lines changed

vlib/v/checker/checker.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ fn (mut c Checker) alias_type_decl(mut node ast.AliasTypeDecl) {
643643
info := parent_typ_sym.info as ast.Map
644644
c.check_alias_vs_element_type_of_parent(node, info.key_type, 'map key')
645645
c.check_alias_vs_element_type_of_parent(node, info.value_type, 'map value')
646+
c.markused_used_maps(c.table.used_features.used_maps == 0)
646647
}
647648
.sum_type {
648649
// TODO: decide whether the following should be allowed. Note that it currently works,

vlib/v/checker/struct.v

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,6 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
523523
c.error('union `${struct_sym.name}` can have only one field initialised',
524524
node.pos)
525525
}
526-
} else if struct_sym.info is ast.Alias {
527-
parent_sym := c.table.sym(struct_sym.info.parent_type)
528-
// e.g. ´x := MyMapAlias{}´, should be a cast to alias type ´x := MyMapAlias(map[...]...)´
529-
if parent_sym.kind == .map {
530-
alias_str := c.table.type_to_str(node.typ)
531-
map_str := c.table.type_to_str(struct_sym.info.parent_type)
532-
c.error('direct map alias init is not possible, use `${alias_str}(${map_str}{})` instead',
533-
node.pos)
534-
return ast.void_type
535-
}
536526
} else if struct_sym.info is ast.FnType {
537527
c.error('functions must be defined, not instantiated like structs', node.pos)
538528
}
@@ -649,8 +639,8 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
649639
.struct {
650640
info = sym.info as ast.Struct
651641
}
652-
.array, .array_fixed {
653-
// we do allow []int{}, [10]int{}
642+
.array, .array_fixed, .map {
643+
// we do allow []int{}, [10]int{}, map[string]int{}
654644
}
655645
else {
656646
c.error('alias type name: ${sym.name} is not struct type', node.pos)

vlib/v/checker/tests/direct_map_alias_init_err.out

Lines changed: 0 additions & 6 deletions
This file was deleted.

vlib/v/checker/tests/direct_map_alias_init_err.vv

Lines changed: 0 additions & 5 deletions
This file was deleted.

vlib/v/gen/c/struct.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
4848
g.write(g.type_default_sumtype(unwrapped_typ, sym))
4949
}
5050
return
51+
} else if sym.kind == .map {
52+
g.write(g.type_default(unwrapped_typ))
53+
return
5154
}
5255
is_amp := g.is_amp
5356
is_multiline := node.init_fields.len > 5

vlib/v/tests/aliases/alias_map_test.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module main
2+
3+
type Dict = map[string]string
4+
5+
fn test_main() {
6+
mut x := Dict{}
7+
x['foo'] = 'bar'
8+
assert '${x}' == "Dict({'foo': 'bar'})"
9+
}

0 commit comments

Comments
 (0)