From 1032cf5c04da17d8ff5b70d61212d962635dd533 Mon Sep 17 00:00:00 2001 From: Larpon Date: Tue, 22 Feb 2022 14:23:15 +0100 Subject: [PATCH] checker: only cast as ast.Var if not unresolved, fixes #13561 (#13562) --- vlib/v/checker/for.v | 8 +++++--- vlib/v/checker/tests/for_in_invalid_identifier.out | 13 +++++++++++++ vlib/v/checker/tests/for_in_invalid_identifier.vv | 5 +++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 vlib/v/checker/tests/for_in_invalid_identifier.out create mode 100644 vlib/v/checker/tests/for_in_invalid_identifier.vv diff --git a/vlib/v/checker/for.v b/vlib/v/checker/for.v index 91860ca871..ce7280a417 100644 --- a/vlib/v/checker/for.v +++ b/vlib/v/checker/for.v @@ -119,9 +119,11 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { } ast.SelectorExpr { root_ident := node.cond.root_ident() or { node.cond.expr as ast.Ident } - if !(root_ident.obj as ast.Var).is_mut { - c.error('field `$node.cond.field_name` is immutable, it cannot be changed', - node.cond.pos) + if root_ident.kind != .unresolved { + if !(root_ident.obj as ast.Var).is_mut { + c.error('field `$node.cond.field_name` is immutable, it cannot be changed', + node.cond.pos) + } } } else {} diff --git a/vlib/v/checker/tests/for_in_invalid_identifier.out b/vlib/v/checker/tests/for_in_invalid_identifier.out new file mode 100644 index 0000000000..79115d7d71 --- /dev/null +++ b/vlib/v/checker/tests/for_in_invalid_identifier.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/for_in_invalid_identifier.vv:2:26: error: undefined ident: `s` + 1 | fn main() { + 2 | for index, mut value in s.statements { + | ^ + 3 | println('Hello $index $value') + 4 | } +vlib/v/checker/tests/for_in_invalid_identifier.vv:3:26: error: no known default format for type `void` + 1 | fn main() { + 2 | for index, mut value in s.statements { + 3 | println('Hello $index $value') + | ~~~~~ + 4 | } + 5 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/for_in_invalid_identifier.vv b/vlib/v/checker/tests/for_in_invalid_identifier.vv new file mode 100644 index 0000000000..c062fed3d3 --- /dev/null +++ b/vlib/v/checker/tests/for_in_invalid_identifier.vv @@ -0,0 +1,5 @@ +fn main() { + for index, mut value in s.statements { + println('Hello $index $value') + } +}