parser: check variable redefinition error (#12992)

pull/12995/head
yuyi 2021-12-29 19:44:08 +08:00 committed by GitHub
parent 69c90ef50d
commit cc577e1bfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 18 deletions

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/assign_var_redefinition_err.vv:6:2: error: redefinition of `aaaa`
4 |
5 | fn test(aaaa string) string {
6 | aaaa := 'bbbb' + aaaa
| ~~~~
7 | return aaaa
8 | }

View File

@ -0,0 +1,8 @@
fn main() {
println(test('whatever'))
}
fn test(aaaa string) string {
aaaa := 'bbbb' + aaaa
return aaaa
}

View File

@ -144,24 +144,6 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
comments << right_comments
end_comments := p.eat_comments(same_line: true)
mut has_cross_var := false
if op == .decl_assign {
// a, b := a + 1, b
for r in right {
p.check_undefined_variables(left, r) or { return p.error_with_pos(err.msg, pos) }
}
} else if left.len > 1 {
// a, b = b, a
for r in right {
has_cross_var = p.check_cross_variables(left, r)
if op !in [.assign, .decl_assign] {
return p.error_with_pos('unexpected $op.str(), expecting := or = or comma',
pos)
}
if has_cross_var {
break
}
}
}
mut is_static := false
mut is_volatile := false
for i, lx in left {
@ -232,6 +214,24 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
}
}
}
if op == .decl_assign {
// a, b := a + 1, b
for r in right {
p.check_undefined_variables(left, r) or { return p.error_with_pos(err.msg, pos) }
}
} else if left.len > 1 {
// a, b = b, a
for r in right {
has_cross_var = p.check_cross_variables(left, r)
if op !in [.assign, .decl_assign] {
return p.error_with_pos('unexpected $op.str(), expecting := or = or comma',
pos)
}
if has_cross_var {
break
}
}
}
pos.update_last_line(p.prev_tok.line_nr)
return ast.AssignStmt{
op: op