Skip to content

Commit cc577e1

Browse files
authored
parser: check variable redefinition error (#12992)
1 parent 69c90ef commit cc577e1

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/assign_var_redefinition_err.vv:6:2: error: redefinition of `aaaa`
2+
4 |
3+
5 | fn test(aaaa string) string {
4+
6 | aaaa := 'bbbb' + aaaa
5+
| ~~~~
6+
7 | return aaaa
7+
8 | }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
println(test('whatever'))
3+
}
4+
5+
fn test(aaaa string) string {
6+
aaaa := 'bbbb' + aaaa
7+
return aaaa
8+
}

vlib/v/parser/assign.v

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,6 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
144144
comments << right_comments
145145
end_comments := p.eat_comments(same_line: true)
146146
mut has_cross_var := false
147-
if op == .decl_assign {
148-
// a, b := a + 1, b
149-
for r in right {
150-
p.check_undefined_variables(left, r) or { return p.error_with_pos(err.msg, pos) }
151-
}
152-
} else if left.len > 1 {
153-
// a, b = b, a
154-
for r in right {
155-
has_cross_var = p.check_cross_variables(left, r)
156-
if op !in [.assign, .decl_assign] {
157-
return p.error_with_pos('unexpected $op.str(), expecting := or = or comma',
158-
pos)
159-
}
160-
if has_cross_var {
161-
break
162-
}
163-
}
164-
}
165147
mut is_static := false
166148
mut is_volatile := false
167149
for i, lx in left {
@@ -232,6 +214,24 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
232214
}
233215
}
234216
}
217+
if op == .decl_assign {
218+
// a, b := a + 1, b
219+
for r in right {
220+
p.check_undefined_variables(left, r) or { return p.error_with_pos(err.msg, pos) }
221+
}
222+
} else if left.len > 1 {
223+
// a, b = b, a
224+
for r in right {
225+
has_cross_var = p.check_cross_variables(left, r)
226+
if op !in [.assign, .decl_assign] {
227+
return p.error_with_pos('unexpected $op.str(), expecting := or = or comma',
228+
pos)
229+
}
230+
if has_cross_var {
231+
break
232+
}
233+
}
234+
}
235235
pos.update_last_line(p.prev_tok.line_nr)
236236
return ast.AssignStmt{
237237
op: op

0 commit comments

Comments
 (0)