mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: add checks for map key/value types (#6283)
This commit is contained in:
parent
81778e507f
commit
8ef55bd8c9
@ -343,6 +343,17 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
|
||||
field.pos)
|
||||
}
|
||||
}
|
||||
if sym.kind == .map {
|
||||
info := sym.map_info()
|
||||
key_sym := c.table.get_type_symbol(info.key_type)
|
||||
value_sym := c.table.get_type_symbol(info.value_type)
|
||||
if key_sym.kind == .placeholder {
|
||||
c.error('unknown type `$key_sym.source_name`', field.pos)
|
||||
}
|
||||
if value_sym.kind == .placeholder {
|
||||
c.error('unknown type `$value_sym.source_name`', field.pos)
|
||||
}
|
||||
}
|
||||
if field.has_default_expr {
|
||||
c.expected_type = field.typ
|
||||
field_expr_type := c.expr(field.default_expr)
|
||||
@ -3365,6 +3376,14 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type {
|
||||
// `x ;= map[string]string` - set in parser
|
||||
if node.typ != 0 {
|
||||
info := c.table.get_type_symbol(node.typ).map_info()
|
||||
key_sym := c.table.get_type_symbol(info.key_type)
|
||||
value_sym := c.table.get_type_symbol(info.value_type)
|
||||
if key_sym.kind == .placeholder {
|
||||
c.error('unknown type `$key_sym.source_name`', node.pos)
|
||||
}
|
||||
if value_sym.kind == .placeholder {
|
||||
c.error('unknown type `$value_sym.source_name`', node.pos)
|
||||
}
|
||||
node.key_type = info.key_type
|
||||
node.value_type = info.value_type
|
||||
return node.typ
|
||||
|
5
vlib/v/checker/tests/map_unknown_value.out
Normal file
5
vlib/v/checker/tests/map_unknown_value.out
Normal file
@ -0,0 +1,5 @@
|
||||
vlib/v/checker/tests/map_unknown_value.vv:2:5: error: unknown type `DoesNotExist`
|
||||
1 | struct App {
|
||||
2 | my_map map[string]DoesNotExist
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
3 | }
|
3
vlib/v/checker/tests/map_unknown_value.vv
Normal file
3
vlib/v/checker/tests/map_unknown_value.vv
Normal file
@ -0,0 +1,3 @@
|
||||
struct App {
|
||||
my_map map[string]DoesNotExist
|
||||
}
|
Loading…
Reference in New Issue
Block a user