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

parser: fix array method using it in defer (fix #18246) (#18267)

This commit is contained in:
yuyi 2023-05-26 08:29:52 +08:00 committed by GitHub
parent 79657a1a2f
commit 6db62e43d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 24 deletions

View File

@ -37,12 +37,7 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
.key_mut, .key_shared, .key_atomic, .key_static, .key_volatile { .key_mut, .key_shared, .key_atomic, .key_static, .key_volatile {
ident := p.ident(ast.Language.v) ident := p.ident(ast.Language.v)
node = ident node = ident
if p.inside_defer { p.add_defer_var(ident)
if !p.defer_vars.any(it.name == ident.name && it.mod == ident.mod)
&& ident.name != 'err' {
p.defer_vars << ident
}
}
p.is_stmt_ident = is_stmt_ident p.is_stmt_ident = is_stmt_ident
} }
.name, .question { .name, .question {

View File

@ -2557,12 +2557,7 @@ fn (mut p Parser) name_expr() ast.Expr {
// incomplete module selector must be handled by dot_expr instead // incomplete module selector must be handled by dot_expr instead
ident := p.ident(language) ident := p.ident(language)
node = ident node = ident
if p.inside_defer { p.add_defer_var(ident)
if !p.defer_vars.any(it.name == ident.name && it.mod == ident.mod)
&& ident.name != 'err' {
p.defer_vars << ident
}
}
return node return node
} }
} }
@ -2588,12 +2583,7 @@ fn (mut p Parser) name_expr() ast.Expr {
if !same_line && p.peek_tok.kind == .lpar { if !same_line && p.peek_tok.kind == .lpar {
ident := p.ident(language) ident := p.ident(language)
node = ident node = ident
if p.inside_defer { p.add_defer_var(ident)
if !p.defer_vars.any(it.name == ident.name && it.mod == ident.mod)
&& ident.name != 'err' {
p.defer_vars << ident
}
}
} else if p.peek_tok.kind == .lpar || is_generic_call || is_generic_cast } else if p.peek_tok.kind == .lpar || is_generic_call || is_generic_cast
|| (p.tok.kind == .lsbr && p.peek_tok.kind == .rsbr && (p.peek_token(3).kind == .lpar || (p.tok.kind == .lsbr && p.peek_tok.kind == .rsbr && (p.peek_token(3).kind == .lpar
|| p.peek_token(5).kind == .lpar)) || (p.tok.kind == .lsbr && p.peek_tok.kind == .number || p.peek_token(5).kind == .lpar)) || (p.tok.kind == .lsbr && p.peek_tok.kind == .number
@ -2794,12 +2784,7 @@ fn (mut p Parser) name_expr() ast.Expr {
} }
ident := p.ident(language) ident := p.ident(language)
node = ident node = ident
if p.inside_defer { p.add_defer_var(ident)
if !p.defer_vars.any(it.name == ident.name && it.mod == ident.mod)
&& ident.name != 'err' {
p.defer_vars << ident
}
}
} }
p.expr_mod = '' p.expr_mod = ''
return node return node
@ -4338,3 +4323,12 @@ fn (mut p Parser) show(params ParserShowParams) {
location := '${p.file_display_path}:${p.tok.line_nr}:' location := '${p.file_display_path}:${p.tok.line_nr}:'
println('>> ${location:-40s} ${params.msg} ${context.join(' ')}') println('>> ${location:-40s} ${params.msg} ${context.join(' ')}')
} }
fn (mut p Parser) add_defer_var(ident ast.Ident) {
if p.inside_defer {
if !p.defer_vars.any(it.name == ident.name && it.mod == ident.mod)
&& ident.name !in ['err', 'it'] {
p.defer_vars << ident
}
}
}

View File

@ -0,0 +1,9 @@
fn test_array_method_using_it_in_defer() {
arr := ['1', '1.1', '1.2', '2', '2.1', '2.2']
defer {
ret := arr.filter(it.contains('1'))
println(ret)
assert ret == ['1', '1.1', '1.2', '2.1']
}
}