Skip to content

Commit 93c40e6

Browse files
committed
all: add support for type MyEnumAlias = MyEnum
1 parent bf9f684 commit 93c40e6

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

vlib/v/ast/table.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,16 @@ pub fn (t &Table) unalias_num_type(typ Type) Type {
678678
return typ
679679
}
680680

681+
[inline]
682+
pub fn (t &Table) unaliased_type(typ Type) Type {
683+
sym := t.sym(typ)
684+
if sym.kind == .alias {
685+
pt := (sym.info as Alias).parent_type
686+
return pt
687+
}
688+
return typ
689+
}
690+
681691
fn (mut t Table) rewrite_already_registered_symbol(typ TypeSymbol, existing_idx int) int {
682692
existing_symbol := t.type_symbols[existing_idx]
683693
if existing_symbol.kind == .placeholder {

vlib/v/checker/checker.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,12 +4428,13 @@ pub fn (mut c Checker) enum_val(mut node ast.EnumVal) ast.Type {
44284428
typ = array_info.elem_type
44294429
typ_sym = c.table.sym(typ)
44304430
}
4431-
if typ_sym.kind != .enum_ && !c.pref.translated {
4431+
fsym := c.table.final_sym(typ)
4432+
if fsym.kind != .enum_ && !c.pref.translated {
44324433
// TODO in C int fields can be compared to enums, need to handle that in C2V
44334434
c.error('expected type is not an enum (`$typ_sym.name`)', node.pos)
44344435
return ast.void_type
44354436
}
4436-
if typ_sym.info !is ast.Enum {
4437+
if fsym.info !is ast.Enum {
44374438
c.error('not an enum', node.pos)
44384439
return ast.void_type
44394440
}

vlib/v/gen/c/cgen.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3153,7 +3153,7 @@ fn (mut g Gen) expr(node ast.Expr) {
31533153
ast.EnumVal {
31543154
// g.write('${it.mod}${it.enum_name}_$it.val')
31553155
// g.enum_expr(node)
3156-
styp := g.typ(node.typ)
3156+
styp := g.typ(g.table.unaliased_type(node.typ))
31573157
g.write('${styp}__$node.val')
31583158
}
31593159
ast.FloatLiteral {

vlib/v/tests/enum_aliases_test.v

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
enum MyEnum {
2+
something
3+
another
4+
third
5+
}
6+
7+
type MyEnumAlias = MyEnum
8+
9+
fn test_enum_aliases() {
10+
x := MyEnum.something
11+
dump(x)
12+
a := MyEnumAlias.something
13+
dump(a)
14+
assert x == a
15+
//
16+
dump(MyEnum.third)
17+
dump(MyEnumAlias.third)
18+
dump(int(MyEnum.third))
19+
dump(int(MyEnumAlias.third))
20+
assert MyEnum.third == MyEnumAlias.third
21+
}

0 commit comments

Comments
 (0)