1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: disallow using Result !Type as a map[Key]!Type (#18543)

This commit is contained in:
Felipe Pena 2023-06-25 03:51:10 -03:00 committed by GitHub
parent 31f68eea94
commit 1ee83bf639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 2 deletions

View File

@ -349,6 +349,9 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
if node.typ != 0 { if node.typ != 0 {
info := c.table.sym(node.typ).map_info() info := c.table.sym(node.typ).map_info()
if info.value_type != 0 { if info.value_type != 0 {
if info.value_type.has_flag(.result) {
c.error('cannot use Result type as map value type', node.pos)
}
val_sym := c.table.sym(info.value_type) val_sym := c.table.sym(info.value_type)
if val_sym.kind == .struct_ { if val_sym.kind == .struct_ {
val_info := val_sym.info as ast.Struct val_info := val_sym.info as ast.Struct

View File

@ -129,6 +129,12 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
if sym.kind == .none_ { if sym.kind == .none_ {
c.error('cannot use `none` as field type', field.type_pos) c.error('cannot use `none` as field type', field.type_pos)
} }
if sym.kind == .map {
info := sym.map_info()
if info.value_type.has_flag(.result) {
c.error('cannot use Result type as map value type', field.type_pos)
}
}
if field.has_default_expr { if field.has_default_expr {
c.expected_type = field.typ c.expected_type = field.typ

View File

@ -0,0 +1,19 @@
vlib/v/checker/tests/map_with_result_value_err.vv:2:7: error: cannot use Result type as map value type
1 | struct Foo {
2 | map1 map[string]!string
| ~~~~~~~~~~~~~~~~~~
3 | }
4 |
vlib/v/checker/tests/map_with_result_value_err.vv:6:8: error: cannot use Result type as map value type
4 |
5 | fn main() {
6 | _ := map[string]!string{}
| ~~~~~~~~~~~~~~~~~~~~
7 |
8 | dump(map[string]!string{})
vlib/v/checker/tests/map_with_result_value_err.vv:8:8: error: cannot use Result type as map value type
6 | _ := map[string]!string{}
7 |
8 | dump(map[string]!string{})
| ~~~~~~~~~~~~~~~~~~~~
9 | }

View File

@ -0,0 +1,9 @@
struct Foo {
map1 map[string]!string
}
fn main() {
_ := map[string]!string{}
dump(map[string]!string{})
}

View File

@ -15,13 +15,11 @@ fn foo(arg map[string]?string) ?string {
} }
struct Foo { struct Foo {
map1 map[string]!string
map2 map[string]?string map2 map[string]?string
} }
fn bar() { fn bar() {
map1 := map[string]?string{} map1 := map[string]?string{}
map2 := map[string]!string{}
} }
fn baz(arg map[string]?string) ?string { fn baz(arg map[string]?string) ?string {