cgen: fix match expression with complex boolean conditions

pull/9275/head
Delyan Angelov 2021-03-12 19:54:50 +02:00
parent 3cb1bb7c36
commit c474106511
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 16 additions and 1 deletions

View File

@ -3808,7 +3808,9 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str
} else {
g.write(cond_var)
g.write(' == ')
g.write('(')
g.expr(expr)
g.write(')')
}
}
if is_expr && tmp_var.len == 0 {

View File

@ -4,7 +4,9 @@ fn test_match_with_array_map_in_branches() {
arr := Arr([0, 1])
ret := match arr {
[]int {
arr.map(fn(s int) string { return s.str() }).str()
arr.map(fn (s int) string {
return s.str()
}).str()
}
else {
''
@ -29,3 +31,14 @@ fn test_match_expr_of_multi_expr_stmts() {
println(ret)
assert ret == 2
}
fn test_match_expression_on_complex_bool_conditions() {
s := 'hello'
x := match true {
s[1] == `e` { 'first' }
(s[1] == `e`) { 'second' }
else { 'not found' }
}
println(x)
assert x == 'first'
}