all: add support for `type MyEnumAlias = MyEnum`

pull/13004/head
Delyan Angelov 2021-12-30 13:42:06 +02:00
parent bf9f684c59
commit 93c40e696d
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 35 additions and 3 deletions

View File

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

View File

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

View File

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

View 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
}