mirror of
https://github.com/vlang/v.git
synced 2025-09-10 07:47:20 -04:00
This commit is contained in:
parent
f80fc37775
commit
34c67878b9
@ -643,6 +643,7 @@ fn (mut c Checker) alias_type_decl(mut node ast.AliasTypeDecl) {
|
|||||||
info := parent_typ_sym.info as ast.Map
|
info := parent_typ_sym.info as ast.Map
|
||||||
c.check_alias_vs_element_type_of_parent(node, info.key_type, 'map key')
|
c.check_alias_vs_element_type_of_parent(node, info.key_type, 'map key')
|
||||||
c.check_alias_vs_element_type_of_parent(node, info.value_type, 'map value')
|
c.check_alias_vs_element_type_of_parent(node, info.value_type, 'map value')
|
||||||
|
c.markused_used_maps(c.table.used_features.used_maps == 0)
|
||||||
}
|
}
|
||||||
.sum_type {
|
.sum_type {
|
||||||
// TODO: decide whether the following should be allowed. Note that it currently works,
|
// TODO: decide whether the following should be allowed. Note that it currently works,
|
||||||
|
@ -523,16 +523,6 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
|
|||||||
c.error('union `${struct_sym.name}` can have only one field initialised',
|
c.error('union `${struct_sym.name}` can have only one field initialised',
|
||||||
node.pos)
|
node.pos)
|
||||||
}
|
}
|
||||||
} else if struct_sym.info is ast.Alias {
|
|
||||||
parent_sym := c.table.sym(struct_sym.info.parent_type)
|
|
||||||
// e.g. ´x := MyMapAlias{}´, should be a cast to alias type ´x := MyMapAlias(map[...]...)´
|
|
||||||
if parent_sym.kind == .map {
|
|
||||||
alias_str := c.table.type_to_str(node.typ)
|
|
||||||
map_str := c.table.type_to_str(struct_sym.info.parent_type)
|
|
||||||
c.error('direct map alias init is not possible, use `${alias_str}(${map_str}{})` instead',
|
|
||||||
node.pos)
|
|
||||||
return ast.void_type
|
|
||||||
}
|
|
||||||
} else if struct_sym.info is ast.FnType {
|
} else if struct_sym.info is ast.FnType {
|
||||||
c.error('functions must be defined, not instantiated like structs', node.pos)
|
c.error('functions must be defined, not instantiated like structs', node.pos)
|
||||||
}
|
}
|
||||||
@ -649,8 +639,8 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
|
|||||||
.struct {
|
.struct {
|
||||||
info = sym.info as ast.Struct
|
info = sym.info as ast.Struct
|
||||||
}
|
}
|
||||||
.array, .array_fixed {
|
.array, .array_fixed, .map {
|
||||||
// we do allow []int{}, [10]int{}
|
// we do allow []int{}, [10]int{}, map[string]int{}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
c.error('alias type name: ${sym.name} is not struct type', node.pos)
|
c.error('alias type name: ${sym.name} is not struct type', node.pos)
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
vlib/v/checker/tests/direct_map_alias_init_err.vv:4:7: error: direct map alias init is not possible, use `WordSet(map[string]bool{})` instead
|
|
||||||
2 |
|
|
||||||
3 | fn main() {
|
|
||||||
4 | _ := WordSet{}
|
|
||||||
| ~~~~~~~~~
|
|
||||||
5 | }
|
|
@ -1,5 +0,0 @@
|
|||||||
type WordSet = map[string]bool
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
_ := WordSet{}
|
|
||||||
}
|
|
@ -48,6 +48,9 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
|
|||||||
g.write(g.type_default_sumtype(unwrapped_typ, sym))
|
g.write(g.type_default_sumtype(unwrapped_typ, sym))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
} else if sym.kind == .map {
|
||||||
|
g.write(g.type_default(unwrapped_typ))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
is_amp := g.is_amp
|
is_amp := g.is_amp
|
||||||
is_multiline := node.init_fields.len > 5
|
is_multiline := node.init_fields.len > 5
|
||||||
|
9
vlib/v/tests/aliases/alias_map_test.v
Normal file
9
vlib/v/tests/aliases/alias_map_test.v
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
type Dict = map[string]string
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
mut x := Dict{}
|
||||||
|
x['foo'] = 'bar'
|
||||||
|
assert '${x}' == "Dict({'foo': 'bar'})"
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user