From c71d4195d389cb25eb586868e16d646dec5f6a00 Mon Sep 17 00:00:00 2001 From: Swastik Date: Tue, 15 Oct 2024 01:39:08 +0530 Subject: [PATCH] checker: check for unwrapped results in map keys and vals --- vlib/v/checker/containers.v | 4 ++++ .../map_key_val_or_not_progagate_err.out | 21 +++++++++++++++++++ .../tests/map_key_val_or_not_progagate_err.vv | 11 ++++++++++ 3 files changed, 36 insertions(+) create mode 100644 vlib/v/checker/tests/map_key_val_or_not_progagate_err.out create mode 100644 vlib/v/checker/tests/map_key_val_or_not_progagate_err.vv diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index 5475885fe8a05b..c5ae14777be139 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -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)) { diff --git a/vlib/v/checker/tests/map_key_val_or_not_progagate_err.out b/vlib/v/checker/tests/map_key_val_or_not_progagate_err.out new file mode 100644 index 00000000000000..229bf1ebaea3e3 --- /dev/null +++ b/vlib/v/checker/tests/map_key_val_or_not_progagate_err.out @@ -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 | diff --git a/vlib/v/checker/tests/map_key_val_or_not_progagate_err.vv b/vlib/v/checker/tests/map_key_val_or_not_progagate_err.vv new file mode 100644 index 00000000000000..7eb09fd9ddbe84 --- /dev/null +++ b/vlib/v/checker/tests/map_key_val_or_not_progagate_err.vv @@ -0,0 +1,11 @@ +fn f() !int { + return 42 +} + +a := { + 2: f() // first value + 3: f() // second value + f(): 1 +} + +println(a)