checker: fix checking got 'none' from or_block of map index (fix #20390) (#20394)

This commit is contained in:
shove 2024-01-05 18:10:23 +08:00 committed by GitHub
parent bfe2cb7d34
commit 582f7be2e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 1 deletions

View File

@ -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)

View File

@ -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 | }

View File

@ -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) }
}