From 74d4081f1be5001470bccff12b09d67c72b9ae1f Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Wed, 31 Jan 2024 10:15:00 +0530 Subject: [PATCH] checker: disallow `none` as match cond (#20688) --- vlib/v/checker/match.v | 3 +++ vlib/v/checker/tests/none_match_cond_err.out | 5 +++++ vlib/v/checker/tests/none_match_cond_err.vv | 4 ++++ 3 files changed, 12 insertions(+) create mode 100644 vlib/v/checker/tests/none_match_cond_err.out create mode 100644 vlib/v/checker/tests/none_match_cond_err.vv diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 57affaa8176ea0..b52e4983efa667 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -59,6 +59,9 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type { c.error('`match` expression with Option type only checks against `none`, to match its value you must unwrap it first `var?`', branch.pos) } + if cond_type_sym.kind == .none_ { + c.error('`none` cannot be a match condition', node.pos) + } // If the last statement is an expression, return its type if branch.stmts.len > 0 { mut stmt := branch.stmts.last() diff --git a/vlib/v/checker/tests/none_match_cond_err.out b/vlib/v/checker/tests/none_match_cond_err.out new file mode 100644 index 00000000000000..9853c66e45a44b --- /dev/null +++ b/vlib/v/checker/tests/none_match_cond_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/none_match_cond_err.vv:1:1: error: `none` cannot be a match condition + 1 | match none { + | ~~~~~~~~~~~~ + 2 | none {} + 3 | else {} diff --git a/vlib/v/checker/tests/none_match_cond_err.vv b/vlib/v/checker/tests/none_match_cond_err.vv new file mode 100644 index 00000000000000..85ad2b7ec7cae7 --- /dev/null +++ b/vlib/v/checker/tests/none_match_cond_err.vv @@ -0,0 +1,4 @@ +match none { + none {} + else {} +}