diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index cb55c5841f..c44b33b04d 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -781,7 +781,12 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { if g.pref.translated && node.typ.is_number() { g.writeln('_const_main__${val};') } else { - g.writeln('${g.styp(node.typ)}__${val};') + node_sym := g.table.sym(node.typ) + if node_sym.info is ast.Alias { + g.writeln('${g.styp(node_sym.info.parent_type)}__${val};') + } else { + g.writeln('${g.styp(node.typ)}__${val};') + } } enum_attrs := sym.info.attrs[val] if enum_attrs.len == 0 { diff --git a/vlib/v/tests/comptime/comptime_enum_values_test.v b/vlib/v/tests/comptime/comptime_enum_values_test.v index 912fee9e73..a652798bd3 100644 --- a/vlib/v/tests/comptime/comptime_enum_values_test.v +++ b/vlib/v/tests/comptime/comptime_enum_values_test.v @@ -5,6 +5,8 @@ enum CharacterGroup { special } +type AnotherCharGroup = CharacterGroup + fn (self CharacterGroup) value() string { return match self { .chars { 'first' } @@ -33,3 +35,21 @@ fn test_main() { assert values == [CharacterGroup.chars, CharacterGroup.alphanumerics, CharacterGroup.numeric, CharacterGroup.special] } + +fn test_alias_enum() { + mut values := []EnumData{} + $for entry in AnotherCharGroup.values { + values << entry + } + assert values[0].value == int(CharacterGroup.chars) + assert values[0].name == CharacterGroup.chars.str() + + assert values[1].value == int(CharacterGroup.alphanumerics) + assert values[1].name == CharacterGroup.alphanumerics.str() + + assert values[2].value == int(CharacterGroup.numeric) + assert values[2].name == CharacterGroup.numeric.str() + + assert values[3].value == int(CharacterGroup.special) + assert values[3].name == CharacterGroup.special.str() +}