From 582f7be2e15a59427fdf1e15656cde86bd20fd2e Mon Sep 17 00:00:00 2001 From: shove Date: Fri, 5 Jan 2024 18:10:23 +0800 Subject: [PATCH] checker: fix checking got 'none' from or_block of map index (fix #20390) (#20394) --- vlib/v/checker/checker.v | 2 +- .../tests/map_index_or_block_type_mismatch_err.out | 13 +++++++++++++ .../tests/map_index_or_block_type_mismatch_err.vv | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/map_index_or_block_type_mismatch_err.out create mode 100644 vlib/v/checker/tests/map_index_or_block_type_mismatch_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e0ffe7ad87..c8ef0c0eae 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1284,7 +1284,7 @@ fn (mut c Checker) check_or_last_stmt(mut stmt ast.Stmt, ret_type ast.Type, expr last_stmt_typ := c.expr(mut stmt.expr) if last_stmt_typ.has_flag(.option) || last_stmt_typ == ast.none_type { - if stmt.expr in [ast.Ident, ast.SelectorExpr, ast.CallExpr, ast.None] { + if stmt.expr in [ast.Ident, ast.SelectorExpr, ast.CallExpr, ast.None, ast.CastExpr] { expected_type_name := c.table.type_to_str(ret_type.clear_flags(.option, .result)) got_type_name := c.table.type_to_str(last_stmt_typ) diff --git a/vlib/v/checker/tests/map_index_or_block_type_mismatch_err.out b/vlib/v/checker/tests/map_index_or_block_type_mismatch_err.out new file mode 100644 index 0000000000..2f00a1758a --- /dev/null +++ b/vlib/v/checker/tests/map_index_or_block_type_mismatch_err.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/map_index_or_block_type_mismatch_err.vv:4:20: error: `or` block must provide a value of type `int`, not `none` + 2 | fn index_got_none_from_or_block() { + 3 | m := map[string]int{} + 4 | _ = m['key'] or { none } + | ~~~~ + 5 | _ = m['key'] or { ?int(none) } + 6 | } +vlib/v/checker/tests/map_index_or_block_type_mismatch_err.vv:5:21: error: `or` block must provide a value of type `int`, not `?int` + 3 | m := map[string]int{} + 4 | _ = m['key'] or { none } + 5 | _ = m['key'] or { ?int(none) } + | ~~~~~~~~~ + 6 | } diff --git a/vlib/v/checker/tests/map_index_or_block_type_mismatch_err.vv b/vlib/v/checker/tests/map_index_or_block_type_mismatch_err.vv new file mode 100644 index 0000000000..5419e4a9a6 --- /dev/null +++ b/vlib/v/checker/tests/map_index_or_block_type_mismatch_err.vv @@ -0,0 +1,6 @@ +// for issue 20390 +fn index_got_none_from_or_block() { + m := map[string]int{} + _ = m['key'] or { none } + _ = m['key'] or { ?int(none) } +}