From b482c0512bca0bfe7eda64bd67b0cbcbfec98d85 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 19 May 2022 01:38:53 +0800 Subject: [PATCH] parser: fix 'val in array' as condition in for stmt (fix #14440) (#14451) --- vlib/v/parser/for.v | 3 ++- vlib/v/tests/for_cond_test.v | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/for_cond_test.v diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index d9a2efb22b..6b2d09b6a7 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -82,7 +82,8 @@ fn (mut p Parser) for_stmt() ast.Stmt { } p.close_scope() return for_c_stmt - } else if p.peek_tok.kind in [.key_in, .comma] + } else if (p.peek_tok.kind in [.key_in, .comma] && !(p.tok.kind == .name + && p.peek_tok.kind == .key_in && p.scope.known_var(p.tok.lit))) || (p.tok.kind == .key_mut && p.peek_token(2).kind in [.key_in, .comma]) { // `for i in vals`, `for i in start .. end`, `for mut user in users`, `for i, mut user in users` mut val_is_mut := p.tok.kind == .key_mut diff --git a/vlib/v/tests/for_cond_test.v b/vlib/v/tests/for_cond_test.v new file mode 100644 index 0000000000..6d027614e9 --- /dev/null +++ b/vlib/v/tests/for_cond_test.v @@ -0,0 +1,10 @@ +type TokenValue = rune | u64 + +fn test_for_cond() { + val := `+` + for val in [TokenValue(`+`), TokenValue(`-`)] { + println('ok') + break + } + assert true +}