mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: add support for type MyEnumAlias = MyEnum
This commit is contained in:
parent
bf9f684c59
commit
93c40e696d
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
21
vlib/v/tests/enum_aliases_test.v
Normal file
21
vlib/v/tests/enum_aliases_test.v
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user