From 2cd1f6e924ed91e30a25cc6621b301aa9b0a3f77 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 1 Mar 2025 08:08:24 -0300 Subject: [PATCH] checker: add missing check for IfExpr and MatchExpr with no valid type (#23832) --- vlib/v/checker/if.v | 4 ++++ vlib/v/checker/match.v | 4 ++++ vlib/v/checker/tests/if_expr_with_none_only.out | 10 ++++++++++ vlib/v/checker/tests/if_expr_with_none_only.vv | 3 +++ vlib/v/checker/tests/match_expr_with_none_only.out | 12 ++++++++++++ vlib/v/checker/tests/match_expr_with_none_only.vv | 6 ++++++ 6 files changed, 39 insertions(+) create mode 100644 vlib/v/checker/tests/if_expr_with_none_only.out create mode 100644 vlib/v/checker/tests/if_expr_with_none_only.vv create mode 100644 vlib/v/checker/tests/match_expr_with_none_only.out create mode 100644 vlib/v/checker/tests/match_expr_with_none_only.vv diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index b3f3ac78f..eecb72628 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -575,6 +575,10 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { c.returns = false } } + if node.typ == ast.none_type { + c.error('invalid if expression, must supply at least one value other than `none`', + node.pos) + } // if only untyped literals were given default to int/f64 node.typ = ast.mktyp(node.typ) if expr_required && !node.has_else { diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index ba49aef36..2b4bc0451 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -245,6 +245,10 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type { c.returns = false } } + if ret_type == ast.none_type { + c.error('invalid match expression, must supply at least one value other than `none`', + node.pos) + } node.return_type = ret_type cond_var := c.get_base_name(&node.cond) if cond_var != '' { diff --git a/vlib/v/checker/tests/if_expr_with_none_only.out b/vlib/v/checker/tests/if_expr_with_none_only.out new file mode 100644 index 000000000..b0350ccb7 --- /dev/null +++ b/vlib/v/checker/tests/if_expr_with_none_only.out @@ -0,0 +1,10 @@ +vlib/v/checker/tests/if_expr_with_none_only.vv:2:2: warning: unused variable: `a` + 1 | fn main() { + 2 | a := if true { none } else { none } + | ^ + 3 | } +vlib/v/checker/tests/if_expr_with_none_only.vv:2:7: error: invalid if expression, must supply at least one value other than `none` + 1 | fn main() { + 2 | a := if true { none } else { none } + | ~~ + 3 | } diff --git a/vlib/v/checker/tests/if_expr_with_none_only.vv b/vlib/v/checker/tests/if_expr_with_none_only.vv new file mode 100644 index 000000000..88685686d --- /dev/null +++ b/vlib/v/checker/tests/if_expr_with_none_only.vv @@ -0,0 +1,3 @@ +fn main() { + a := if true { none } else { none } +} diff --git a/vlib/v/checker/tests/match_expr_with_none_only.out b/vlib/v/checker/tests/match_expr_with_none_only.out new file mode 100644 index 000000000..c1226c5ff --- /dev/null +++ b/vlib/v/checker/tests/match_expr_with_none_only.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/match_expr_with_none_only.vv:2:2: warning: unused variable: `a` + 1 | fn main() { + 2 | a := match 1 { + | ^ + 3 | 1 { none } + 4 | else { none } +vlib/v/checker/tests/match_expr_with_none_only.vv:2:7: error: invalid match expression, must supply at least one value other than `none` + 1 | fn main() { + 2 | a := match 1 { + | ~~~~~~~~~ + 3 | 1 { none } + 4 | else { none } diff --git a/vlib/v/checker/tests/match_expr_with_none_only.vv b/vlib/v/checker/tests/match_expr_with_none_only.vv new file mode 100644 index 000000000..b4936ca42 --- /dev/null +++ b/vlib/v/checker/tests/match_expr_with_none_only.vv @@ -0,0 +1,6 @@ +fn main() { + a := match 1 { + 1 { none } + else { none } + } +} -- 2.39.5