checker: check for unwrapped results in map keys and vals (fix #22521) (#22525)

This commit is contained in:
Swastik Baranwal 2024-10-15 10:59:09 +05:30 committed by GitHub
parent 7220f75b07
commit 4c55de5e65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 0 deletions

View file

@ -517,6 +517,8 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
if node.keys.len == 1 && map_val_type == ast.none_type {
c.error('map value cannot be only `none`', node.vals[0].pos())
}
c.check_expr_option_or_result_call(key_, map_key_type)
c.check_expr_option_or_result_call(val_, map_val_type)
}
map_key_type = c.unwrap_generic(map_key_type)
map_val_type = c.unwrap_generic(map_val_type)
@ -539,6 +541,8 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
val_type := c.expr(mut val)
node.val_types << val_type
val_type_sym := c.table.sym(val_type)
c.check_expr_option_or_result_call(key, key_type)
c.check_expr_option_or_result_call(val, val_type)
if !c.check_types(key_type, map_key_type)
|| (i == 0 && key_type.is_number() && map_key_type.is_number()
&& map_key_type != ast.mktyp(key_type)) {

View file

@ -0,0 +1,21 @@
vlib/v/checker/tests/map_key_val_or_not_progagate_err.vv:6:7: error: f() returns `!int`, so it should have either an `or {}` block, or `!` at the end
4 |
5 | a := {
6 | 2: f() // first value
| ~~~
7 | 3: f() // second value
8 | f(): 1
vlib/v/checker/tests/map_key_val_or_not_progagate_err.vv:7:7: error: f() returns `!int`, so it should have either an `or {}` block, or `!` at the end
5 | a := {
6 | 2: f() // first value
7 | 3: f() // second value
| ~~~
8 | f(): 1
9 | }
vlib/v/checker/tests/map_key_val_or_not_progagate_err.vv:8:2: error: f() returns `!int`, so it should have either an `or {}` block, or `!` at the end
6 | 2: f() // first value
7 | 3: f() // second value
8 | f(): 1
| ~~~
9 | }
10 |

View file

@ -0,0 +1,11 @@
fn f() !int {
return 42
}
a := {
2: f() // first value
3: f() // second value
f(): 1
}
println(a)