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

checker: fix missing check for diff type on map value declaration (#18522)

This commit is contained in:
Felipe Pena 2023-06-22 16:48:15 -03:00 committed by GitHub
parent 8d48b89e8e
commit 9d77fd90bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View File

@ -440,8 +440,10 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
c.error('invalid map value: ${msg}', val.pos())
}
}
if !c.check_types(val_type, val0_type) || (i == 0 && val_type.is_number()
&& val0_type.is_number() && val0_type != ast.mktyp(val_type)) {
if !c.check_types(val_type, val0_type)
|| val0_type.has_flag(.option) != val_type.has_flag(.option)
|| (i == 0 && val_type.is_number() && val0_type.is_number()
&& val0_type != ast.mktyp(val_type)) {
msg := c.expected_msg(val_type, val0_type)
c.error('invalid map value: ${msg}', val.pos())
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/diff_type_map_value_err.vv:10:44: error: invalid map value: expected `?MyStruct`, not `MyStruct`
8 |
9 | fn empty() map[string]?MyStruct {
10 | return {'key1': ?MyStruct(none), 'key2': MyStruct{field: 10}}
| ~~~~~~~~~~~~~~~~~~~
11 | }
12 |

View File

@ -0,0 +1,16 @@
struct MyStruct {
field int
}
struct MyStruct2 {
field int
}
fn empty() map[string]?MyStruct {
return {'key1': ?MyStruct(none), 'key2': MyStruct{field: 10}}
}
fn test_empty() {
expected := {'key': ?MyStruct(none)}
assert empty() == expected
}