Skip to content

Commit

Permalink
checker: check for unwrapped results in map keys and vals (fix #22521) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 authored Oct 15, 2024
1 parent 7220f75 commit 4c55de5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vlib/v/checker/containers.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)) {
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/checker/tests/map_key_val_or_not_progagate_err.out
Original file line number Diff line number Diff line change
@@ -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 |
11 changes: 11 additions & 0 deletions vlib/v/checker/tests/map_key_val_or_not_progagate_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn f() !int {
return 42
}

a := {
2: f() // first value
3: f() // second value
f(): 1
}

println(a)

0 comments on commit 4c55de5

Please sign in to comment.