Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 15, 2023
1 parent 7c0aed1 commit 9ff1f4e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -302,7 +306,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 {
Expand Down
48 changes: 48 additions & 0 deletions vlib/v/tests/comptime_dump_test.v
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 9ff1f4e

Please sign in to comment.