From e4f6369cd11ff3336a0e2a5fe6e92bd11b56fa6a Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 30 May 2021 00:30:57 +0800 Subject: [PATCH] checker: fix match error to none (#10245) --- vlib/v/checker/checker.v | 2 ++ vlib/v/tests/match_error_to_none_test.v | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 vlib/v/tests/match_error_to_none_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8893835fdf..d9b6f44cd0 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -5332,6 +5332,8 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type { cond_type_sym := c.table.get_type_symbol(cond_type) if cond_type_sym.kind !in [.interface_, .sum_type] { node.is_sum_type = false + } else { + node.is_sum_type = true } c.match_exprs(mut node, cond_type_sym) c.expected_type = cond_type diff --git a/vlib/v/tests/match_error_to_none_test.v b/vlib/v/tests/match_error_to_none_test.v new file mode 100644 index 0000000000..1a2c99d7d1 --- /dev/null +++ b/vlib/v/tests/match_error_to_none_test.v @@ -0,0 +1,25 @@ +fn do_a_thing(i int) ?int { + if i < 0 { + return error("can't be negative") + } + if i == 0 { + return none + } + return i +} + +fn test_match_error_to_none() { + i := 0 + if r := do_a_thing(i) { + println(r) + } else { + match err { + none { + assert true + } + else { + assert false + } + } + } +}