From 3fe820406299b950541b0111ee399089c315de0f Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 8 Mar 2022 17:55:17 +0800 Subject: [PATCH] checker: fix if expr with enum value (#13685) --- vlib/v/checker/if.v | 6 ++++++ vlib/v/tests/if_expr_with_enum_test.v | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 vlib/v/tests/if_expr_with_enum_test.v diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 68d02b92f8..ee0f45f147 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -21,6 +21,12 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } expr_required := c.expected_type != ast.void_type former_expected_type := c.expected_type + if node_is_expr { + c.expected_expr_type = c.expected_type + defer { + c.expected_expr_type = ast.void_type + } + } node.typ = ast.void_type mut nbranches_with_return := 0 mut nbranches_without_return := 0 diff --git a/vlib/v/tests/if_expr_with_enum_test.v b/vlib/v/tests/if_expr_with_enum_test.v new file mode 100644 index 0000000000..39e7b07485 --- /dev/null +++ b/vlib/v/tests/if_expr_with_enum_test.v @@ -0,0 +1,19 @@ +enum Foo { + a + b +} + +fn get() Foo { + return .a +} + +fn foo(f Foo) string { + println(f) + return '$f' +} + +fn test_if_expr_with_enum_value() { + ret := foo(if get() == .a { .b } else { .a }) + println(ret) + assert ret == 'b' +}