Skip to content

Commit

Permalink
checker: fix anonymous enum evaluation value (part 2) (fix #19925) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 21, 2023
1 parent bbb712a commit e5fcf35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
24 changes: 23 additions & 1 deletion vlib/v/checker/comptime.v
Expand Up @@ -339,7 +339,12 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp
return c.eval_comptime_const_expr(expr.expr, nlevel + 1)
}
ast.EnumVal {
if val := c.table.find_enum_field_val(expr.enum_name, expr.val) {
enum_name := if expr.enum_name == '' {
c.table.type_to_str(c.expected_type)
} else {
expr.enum_name
}
if val := c.table.find_enum_field_val(enum_name, expr.val) {
return val
}
}
Expand Down Expand Up @@ -415,7 +420,24 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp
}
ast.InfixExpr {
left := c.eval_comptime_const_expr(expr.left, nlevel + 1)?
saved_expected_type := c.expected_type
if expr.left is ast.EnumVal {
c.expected_type = expr.left.typ
} else if expr.left is ast.InfixExpr {
mut infixexpr := expr
for {
if infixexpr.left is ast.InfixExpr {
infixexpr = infixexpr.left as ast.InfixExpr
} else {
break
}
}
if mut infixexpr.left is ast.EnumVal {
c.expected_type = infixexpr.left.typ
}
}
right := c.eval_comptime_const_expr(expr.right, nlevel + 1)?
c.expected_type = saved_expected_type
if left is string && right is string {
match expr.op {
.plus {
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/tests/enum_flag_test.v
Expand Up @@ -9,10 +9,16 @@ const a = Foo.a

const ab = Foo.a | Foo.b

const ab2 = Foo.a | .b

const abc = Foo.a | Foo.b | Foo.c

const abc2 = Foo.a | .b | .c

fn test_main() {
assert dump(a) == Foo.a
assert dump(ab) == Foo.a | Foo.b
assert dump(ab2) == Foo.a | .b
assert dump(abc) == Foo.a | Foo.b | Foo.c
assert dump(abc2) == Foo.a | .b | .c
}

0 comments on commit e5fcf35

Please sign in to comment.