From f634c6e0a486637959503cf31568013eaa938eec Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 17 Jan 2023 01:47:51 -0300 Subject: [PATCH] checker: fix postfix var checking break (#16984) --- vlib/v/checker/checker.v | 2 +- .../undefined_var_in_comptime_for_test.out | 7 +++++++ .../undefined_var_in_comptime_for_test.vv | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/undefined_var_in_comptime_for_test.out create mode 100644 vlib/v/checker/tests/undefined_var_in_comptime_for_test.vv 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{}) +}