diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index ae9572856c..42c60d7e0a 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -678,6 +678,16 @@ pub fn (t &Table) unalias_num_type(typ Type) Type { return typ } +[inline] +pub fn (t &Table) unaliased_type(typ Type) Type { + sym := t.sym(typ) + if sym.kind == .alias { + pt := (sym.info as Alias).parent_type + return pt + } + return typ +} + fn (mut t Table) rewrite_already_registered_symbol(typ TypeSymbol, existing_idx int) int { existing_symbol := t.type_symbols[existing_idx] if existing_symbol.kind == .placeholder { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6068db1d44..424caa69a6 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4428,12 +4428,13 @@ pub fn (mut c Checker) enum_val(mut node ast.EnumVal) ast.Type { typ = array_info.elem_type typ_sym = c.table.sym(typ) } - if typ_sym.kind != .enum_ && !c.pref.translated { + fsym := c.table.final_sym(typ) + if fsym.kind != .enum_ && !c.pref.translated { // TODO in C int fields can be compared to enums, need to handle that in C2V c.error('expected type is not an enum (`$typ_sym.name`)', node.pos) return ast.void_type } - if typ_sym.info !is ast.Enum { + if fsym.info !is ast.Enum { c.error('not an enum', node.pos) return ast.void_type } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9abbb39919..1413f05dba 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3153,7 +3153,7 @@ fn (mut g Gen) expr(node ast.Expr) { ast.EnumVal { // g.write('${it.mod}${it.enum_name}_$it.val') // g.enum_expr(node) - styp := g.typ(node.typ) + styp := g.typ(g.table.unaliased_type(node.typ)) g.write('${styp}__$node.val') } ast.FloatLiteral { diff --git a/vlib/v/tests/enum_aliases_test.v b/vlib/v/tests/enum_aliases_test.v new file mode 100644 index 0000000000..bfc6a904d9 --- /dev/null +++ b/vlib/v/tests/enum_aliases_test.v @@ -0,0 +1,21 @@ +enum MyEnum { + something + another + third +} + +type MyEnumAlias = MyEnum + +fn test_enum_aliases() { + x := MyEnum.something + dump(x) + a := MyEnumAlias.something + dump(a) + assert x == a + // + dump(MyEnum.third) + dump(MyEnumAlias.third) + dump(int(MyEnum.third)) + dump(int(MyEnumAlias.third)) + assert MyEnum.third == MyEnumAlias.third +}