diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 96b487ca59..64df87e729 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -135,6 +135,7 @@ mut: comptime_call_pos int // needed for correctly checking use before decl for templates goto_labels map[string]ast.GotoLabel // to check for unused goto labels enum_data_type ast.Type + field_data_type ast.Type fn_return_type ast.Type orm_table_fields map[string][]ast.StructField // known table structs // @@ -1470,7 +1471,7 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type { return ast.void_type } else if c.inside_comptime_for_field && typ == c.enum_data_type && node.field_name == 'value' { // for comp-time enum.values - node.expr_type = c.comptime_fields_type[c.comptime_for_field_var] + node.expr_type = c.comptime_fields_type['${c.comptime_for_field_var}.typ'] node.typ = typ return node.expr_type } diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 37f4c76201..ac6c24eada 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -275,9 +275,13 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { } c.inside_comptime_for_field = true for field in fields { + if c.field_data_type == 0 { + c.field_data_type = ast.Type(c.table.find_type_idx('FieldData')) + } c.comptime_for_field_value = field c.comptime_for_field_var = node.val_var - c.comptime_fields_type[node.val_var] = node.typ + c.comptime_fields_type[node.val_var] = c.field_data_type + c.comptime_fields_type['${node.val_var}.typ'] = node.typ c.comptime_fields_default_type = field.typ c.stmts(mut node.stmts) @@ -310,7 +314,8 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { for field in sym_info.vals { c.comptime_enum_field_value = field c.comptime_for_field_var = node.val_var - c.comptime_fields_type[node.val_var] = node.typ + c.comptime_fields_type[node.val_var] = c.enum_data_type + c.comptime_fields_type['${node.val_var}.typ'] = node.typ c.stmts(mut node.stmts) } } else { diff --git a/vlib/v/tests/comptime_dump_test.v b/vlib/v/tests/comptime_dump_test.v new file mode 100644 index 0000000000..4fd64e6470 --- /dev/null +++ b/vlib/v/tests/comptime_dump_test.v @@ -0,0 +1,48 @@ +@[abc] +struct Another { + a []int + b u8 + c u32 +} + +fn (f Another) test() {} + +enum Abc { + a + b + c +} + +fn test_main() { + mut c := 0 + $for f in Abc.values { + dump(f) + dump(f.value) + c += 1 + assert typeof(f).name == 'EnumData' + } + assert c == 3 + + $for f in Another.fields { + dump(f) + dump(f.name) + c += 1 + } + assert c == 6 + + $for f in Another.methods { + dump(f) + dump(f.name) + c += 1 + assert typeof(f).name == 'FunctionData' + } + assert c == 7 + + $for f in Another.attributes { + dump(f) + dump(f.name) + c += 1 + assert typeof(f).name == 'StructAttribute' + } + assert c == 8 +}