From b677ad9ca5bbcb7cd7c0e1e6c91fceb62f6b125b Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sat, 10 Oct 2020 14:42:40 +0100 Subject: [PATCH] checker: fix panic on match expression without results (#6597) --- vlib/v/checker/checker.v | 4 ++- vlib/v/checker/tests/if_match_result.out | 34 ++++++++++++++++++++++++ vlib/v/checker/tests/if_match_result.vv | 19 +++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/if_match_result.out create mode 100644 vlib/v/checker/tests/if_match_result.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f0c0dc8426..116038f63d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3062,12 +3062,13 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) table.Type { mut branch_without_return := false for branch in node.branches { c.stmts(branch.stmts) - if node.is_expr { + if node.is_expr && branch.stmts.len > 0 { // 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] { + // must not contain C statements st.check_c_expr() or { c.error('`match` expression branch has $err', st.position()) } @@ -3476,6 +3477,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type { branch.pos) } for st in branch.stmts { + // must not contain C statements st.check_c_expr() or { c.error('`if` expression branch has $err', st.position()) } diff --git a/vlib/v/checker/tests/if_match_result.out b/vlib/v/checker/tests/if_match_result.out new file mode 100644 index 0000000000..53a154ea2f --- /dev/null +++ b/vlib/v/checker/tests/if_match_result.out @@ -0,0 +1,34 @@ +vlib/v/checker/tests/if_match_result.vv:2:3: error: assignment mismatch: 1 variable(s) 0 value(s) + 1 | // missing results + 2 | _ = match 4 { + | ^ + 3 | 1 {} + 4 | else {} +vlib/v/checker/tests/if_match_result.vv:6:5: error: `if` expression requires an expression as the last statement of every branch + 4 | else {} + 5 | } + 6 | _ = if true { + | ~~~~~~~ + 7 | } else { + 8 | } +vlib/v/checker/tests/if_match_result.vv:7:3: error: `if` expression requires an expression as the last statement of every branch + 5 | } + 6 | _ = if true { + 7 | } else { + | ~~~~ + 8 | } + 9 | +vlib/v/checker/tests/if_match_result.vv:11:3: error: assignment mismatch: 1 variable(s) 0 value(s) + 9 | + 10 | // void results + 11 | _ = match 4 { + | ^ + 12 | 1 {println('')} + 13 | else {exit(0)} +vlib/v/checker/tests/if_match_result.vv:15:3: error: assignment mismatch: 1 variable(s) 0 value(s) + 13 | else {exit(0)} + 14 | } + 15 | _ = if true { + | ^ + 16 | println('') + 17 | } else { diff --git a/vlib/v/checker/tests/if_match_result.vv b/vlib/v/checker/tests/if_match_result.vv new file mode 100644 index 0000000000..ad85b13bcc --- /dev/null +++ b/vlib/v/checker/tests/if_match_result.vv @@ -0,0 +1,19 @@ +// missing results +_ = match 4 { + 1 {} + else {} +} +_ = if true { +} else { +} + +// void results +_ = match 4 { + 1 {println('')} + else {exit(0)} +} +_ = if true { + println('') +} else { + exit(0) +}