diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 27e06627c6..2f210b2ccd 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3508,7 +3508,7 @@ fn (c &Checker) has_return(stmts []ast.Stmt) ?bool { pub fn (mut c Checker) is_comptime_var(node ast.Expr) bool { return c.inside_comptime_for_field && node is ast.Ident - && (node as ast.Ident).info is ast.IdentVar && ((node as ast.Ident).obj as ast.Var).is_comptime_field + && (node as ast.Ident).info is ast.IdentVar && (node as ast.Ident).kind == .variable && ((node as ast.Ident).obj as ast.Var).is_comptime_field } fn (mut c Checker) postfix_expr(mut node ast.PostfixExpr) ast.Type { diff --git a/vlib/v/checker/tests/undefined_var_in_comptime_for_test.out b/vlib/v/checker/tests/undefined_var_in_comptime_for_test.out new file mode 100644 index 0000000000..0da1dc1fa9 --- /dev/null +++ b/vlib/v/checker/tests/undefined_var_in_comptime_for_test.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/undefined_var_in_comptime_for_test.vv:11:3: error: undefined ident: `fields_len` + 9 | fn test[U](val U) { + 10 | $for field in U.fields { + 11 | fields_len++ + | ~~~~~~~~~~ + 12 | println(field) + 13 | } diff --git a/vlib/v/checker/tests/undefined_var_in_comptime_for_test.vv b/vlib/v/checker/tests/undefined_var_in_comptime_for_test.vv new file mode 100644 index 0000000000..e31ee192cd --- /dev/null +++ b/vlib/v/checker/tests/undefined_var_in_comptime_for_test.vv @@ -0,0 +1,18 @@ +module main + +struct Struct { + b string + a ?string + c string +} + +fn test[U](val U) { + $for field in U.fields { + fields_len++ + println(field) + } +} + +fn test_main() { + test(Struct{}) +}