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' +}