checker: handle `a[i] or { statements expr }` the same as the other or blocks

pull/8197/head
Delyan Angelov 2021-01-19 09:28:34 +02:00
parent 24d1ec2714
commit 85bcfdd636
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 15 additions and 3 deletions

View File

@ -1940,6 +1940,10 @@ pub fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type table.Type) t
c.error('unexpected `?`, the function `$expr.name` does not return an optional', c.error('unexpected `?`, the function `$expr.name` does not return an optional',
expr.or_block.pos) expr.or_block.pos)
} }
} else if expr is ast.IndexExpr {
if expr.or_expr.kind != .absent {
c.check_or_expr(expr.or_expr, ret_type, ret_type)
}
} }
return ret_type return ret_type
} }

View File

@ -3,25 +3,33 @@ fn test_array_or() {
mut testvar := 17 mut testvar := 17
el := m[4] or { el := m[4] or {
testvar = -43 testvar = -43
999
} }
good := m[1] or { good := m[1] or {
testvar = 11 testvar = 11
0
} }
assert testvar == -43 assert testvar == -43
assert el == 0 assert el == 999
assert good == 4 assert good == 4
} }
fn test_map_or() { fn test_map_or() {
m := {'as': 3, 'qw': 4, 'kl': 5} m := {
'as': 3
'qw': 4
'kl': 5
}
mut testvar := -21 mut testvar := -21
el := m['pp'] or { el := m['pp'] or {
testvar = 7931 testvar = 7931
7
} }
good := m['kl'] or { good := m['kl'] or {
testvar = -45 testvar = -45
999
} }
assert testvar == 7931 assert testvar == 7931
assert el == 0 assert el == 7
assert good == 5 assert good == 5
} }