1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix if expr with enum value (#13685)

This commit is contained in:
yuyi 2022-03-08 17:55:17 +08:00 committed by GitHub
parent f6aba9a3fe
commit 3fe8204062
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -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

View File

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