checker: don't error if last statement in match branch produces a value (#6523)

pull/6535/head
Nick Treleaven 2020-10-02 08:33:04 +01:00 committed by GitHub
parent 8e8e808fc9
commit 7c86b03505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 24 deletions

View File

@ -3021,7 +3021,11 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) table.Type {
for branch in node.branches { for branch in node.branches {
c.stmts(branch.stmts) c.stmts(branch.stmts)
if node.is_expr { if node.is_expr {
for st in branch.stmts { // ignore last statement - workaround
// currently the last statement in a match branch does not have an
// expected value set, so e.g. IfExpr.is_expr is not set.
// probably any mismatch will be caught by not producing a value instead
for st in branch.stmts[0..branch.stmts.len - 1] {
st.check_c_expr() or { st.check_c_expr() or {
c.error('`match` expression branch has $err', st.position()) c.error('`match` expression branch has $err', st.position())
} }

View File

@ -1,28 +1,42 @@
vlib/v/checker/tests/if_match_expr.vv:8:6: error: `if` expression branch has unsupported statement (`v.ast.ForStmt`) vlib/v/checker/tests/if_match_expr.vv:8:6: error: `if` expression branch has unsupported statement (`v.ast.ForStmt`)
6 | if true {1} else {-1} // OK 6 | if true {1} else {-1} // result
7 | } else { 7 | } else {
8 | for {break} 8 | for {break}
| ^ | ^
9 | {} 9 | {}
10 | -1 10 | match true {true {} else {}} // statement not expression
vlib/v/checker/tests/if_match_expr.vv:9:2: error: `if` expression branch has unsupported statement (`v.ast.Block`) vlib/v/checker/tests/if_match_expr.vv:9:2: error: `if` expression branch has unsupported statement (`v.ast.Block`)
7 | } else { 7 | } else {
8 | for {break} 8 | for {break}
9 | {} 9 | {}
| ^ | ^
10 | -1 10 | match true {true {} else {}} // statement not expression
11 | } 11 | _ = match true {true {1} else {-1}} // OK
vlib/v/checker/tests/if_match_expr.vv:15:10: error: `match` expression branch has unsupported statement (`v.ast.AssertStmt`) vlib/v/checker/tests/if_match_expr.vv:10:2: error: `if` expression branch has unsupported statement (`v.ast.MatchExpr`)
13 | _ = match true { 8 | for {break}
14 | true { 9 | {}
15 | assert true 10 | match true {true {} else {}} // statement not expression
| ~~~~ | ~~~~~
16 | 1 11 | _ = match true {true {1} else {-1}} // OK
17 | } 12 | match true {true {1} else {-1}} // result
vlib/v/checker/tests/if_match_expr.vv:19:3: error: `match` expression branch has unsupported statement (`v.ast.IfExpr`) vlib/v/checker/tests/if_match_expr.vv:17:3: error: `match` expression branch has unsupported statement (`v.ast.IfExpr`)
17 | } 15 | _ = match true {
18 | else { 16 | true {
19 | if true {} // statement not expression 17 | if true {} // statement not expression
| ~~ | ~~
20 | (-1) // parens needed ATM due to bug 18 | _ = if true {1} else {-1} // OK
21 | } 19 | if true {1} else {-1} // result
vlib/v/checker/tests/if_match_expr.vv:22:10: error: `match` expression branch has unsupported statement (`v.ast.AssertStmt`)
20 | }
21 | else {
22 | assert true
| ~~~~
23 | match true {true {} else {}} // statement not expression
24 | _ = match true {true {1} else {-1}} // OK
vlib/v/checker/tests/if_match_expr.vv:23:3: error: `match` expression branch has unsupported statement (`v.ast.MatchExpr`)
21 | else {
22 | assert true
23 | match true {true {} else {}} // statement not expression
| ~~~~~
24 | _ = match true {true {1} else {-1}} // OK
25 | match true {true {1} else {-1}} // result

View File

@ -3,20 +3,25 @@
_ = if true { _ = if true {
if true {} // FIXME should error, if statement if true {} // FIXME should error, if statement
_ = if true {1} else {-1} // OK _ = if true {1} else {-1} // OK
if true {1} else {-1} // OK if true {1} else {-1} // result
} else { } else {
for {break} for {break}
{} {}
-1 match true {true {} else {}} // statement not expression
_ = match true {true {1} else {-1}} // OK
match true {true {1} else {-1}} // result
} }
_ = match true { _ = match true {
true { true {
assert true if true {} // statement not expression
1 _ = if true {1} else {-1} // OK
if true {1} else {-1} // result
} }
else { else {
if true {} // statement not expression assert true
(-1) // parens needed ATM due to bug match true {true {} else {}} // statement not expression
_ = match true {true {1} else {-1}} // OK
match true {true {1} else {-1}} // result
} }
} }