checker: check index_expr or_expr types mismatch (#10224)

pull/10238/head
yuyi 2021-05-28 16:40:59 +08:00 committed by GitHub
parent 39de06c200
commit 6e493ca8ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 0 deletions

View File

@ -6522,6 +6522,7 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
}
}
c.stmts(node.or_expr.stmts)
c.check_expr_opt_call(node, typ)
return typ
}

View File

@ -0,0 +1,14 @@
vlib/v/checker/tests/or_expr_types_mismatch.vv:3:19: error: wrong return type `none` in the `or {}` block, expected `string`
1 | fn get_map() ?string {
2 | m := map{1: 'a', 2: 'b'}
3 | return m[1] or { none }
| ~~~~
4 | }
5 |
vlib/v/checker/tests/or_expr_types_mismatch.vv:8:19: error: wrong return type `none` in the `or {}` block, expected `int`
6 | fn get_array() ?int {
7 | a := [1, 2, 3]
8 | return a[4] or { none }
| ~~~~
9 | }
10 |

View File

@ -0,0 +1,17 @@
fn get_map() ?string {
m := map{1: 'a', 2: 'b'}
return m[1] or { none }
}
fn get_array() ?int {
a := [1, 2, 3]
return a[4] or { none }
}
fn main() {
map_result := get_map() or { return }
println(map_result)
array_result := get_array() or { return }
println(array_result)
}