1
0
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:
Xavier B 2020-09-04 17:59:54 -04:00 committed by GitHub
parent 81778e507f
commit 8ef55bd8c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -343,6 +343,17 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
field.pos) 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 { if field.has_default_expr {
c.expected_type = field.typ c.expected_type = field.typ
field_expr_type := c.expr(field.default_expr) 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 // `x ;= map[string]string` - set in parser
if node.typ != 0 { if node.typ != 0 {
info := c.table.get_type_symbol(node.typ).map_info() 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.key_type = info.key_type
node.value_type = info.value_type node.value_type = info.value_type
return node.typ return node.typ

View 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 | }

View File

@ -0,0 +1,3 @@
struct App {
my_map map[string]DoesNotExist
}