1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix for_c_stmt that init with var assign (#19004)

This commit is contained in:
yuyi 2023-07-29 20:29:35 +08:00 committed by GitHub
parent c4a679186f
commit a61a2fd328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -31,8 +31,9 @@ fn (mut p Parser) for_stmt() ast.Stmt {
p.close_scope() p.close_scope()
return for_stmt return for_stmt
} else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] } else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon]
|| p.tok.kind == .semicolon || (p.peek_tok.kind == .comma || p.peek_tok.kind.is_assign() || p.tok.kind == .semicolon
&& p.peek_token(2).kind != .key_mut && p.peek_token(3).kind != .key_in) { || (p.peek_tok.kind == .comma && p.peek_token(2).kind != .key_mut
&& p.peek_token(3).kind != .key_in) {
// `for i := 0; i < 10; i++ {` or `for a,b := 0,1; a < 10; a++ {` // `for i := 0; i < 10; i++ {` or `for a,b := 0,1; a < 10; a++ {`
if p.tok.kind == .key_mut { if p.tok.kind == .key_mut {
return p.error('`mut` is not needed in `for ;;` loops: use `for i := 0; i < n; i ++ {`') return p.error('`mut` is not needed in `for ;;` loops: use `for i := 0; i < n; i ++ {`')
@ -45,7 +46,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
mut has_inc := false mut has_inc := false
mut is_multi := p.peek_tok.kind == .comma && p.peek_token(2).kind != .key_mut mut is_multi := p.peek_tok.kind == .comma && p.peek_token(2).kind != .key_mut
&& p.peek_token(3).kind != .key_in && p.peek_token(3).kind != .key_in
if p.peek_tok.kind in [.assign, .decl_assign] || is_multi { if p.peek_tok.kind in [.assign, .decl_assign] || p.peek_tok.kind.is_assign() || is_multi {
init = p.assign_stmt() init = p.assign_stmt()
has_init = true has_init = true
} }

View File

@ -0,0 +1,9 @@
fn test_for_c_init_with_var_assign() {
mut v := 4
mut r := 0
for v >>= 1; v != 0; v >>= 1 {
r++
}
println(r)
assert r == 2
}