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:
parent
8d48b89e8e
commit
9d77fd90bf
@ -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())
|
||||
}
|
||||
|
7
vlib/v/checker/tests/diff_type_map_value_err.out
Normal file
7
vlib/v/checker/tests/diff_type_map_value_err.out
Normal 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 |
|
16
vlib/v/checker/tests/diff_type_map_value_err.vv
Normal file
16
vlib/v/checker/tests/diff_type_map_value_err.vv
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user