cgen: fix using brackets for match expression (#6768)

pull/6773/head
Nick Treleaven 2020-11-07 13:47:27 +00:00 committed by GitHub
parent 0d8114e14d
commit 125650c986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -2820,12 +2820,17 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
g.expr(node.cond)
g.writeln(';')
g.write(cur_line)
if is_expr {
// brackets needed otherwise '?' will apply to everything on the left
g.write('(')
}
if node.is_sum_type {
g.match_expr_sumtype(node, is_expr, cond_var)
} else {
g.match_expr_classic(node, is_expr, cond_var)
}
if is_expr {
g.write(')')
g.decrement_inside_ternary()
}
}

View File

@ -222,3 +222,10 @@ fn test_match_sumtype_multiple_types() {
}
}
}
fn test_sub_expression() {
b := false && match 1 {0 {true} else {true}}
assert !b
c := true || match 1 {0 {false} else {false}}
assert c
}