parser: error on variable redefinition

pull/5420/head
joe-conigliaro 2020-06-19 06:16:38 +10:00
parent 68967e833d
commit 1c68417918
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
1 changed files with 13 additions and 10 deletions

View File

@ -91,30 +91,33 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt {
match lx { match lx {
ast.Ident { ast.Ident {
if op == .decl_assign { if op == .decl_assign {
if p.scope.known_var(lx.name) {
p.error_with_pos('redefinition of `$lx.name`', lx.pos)
}
if left.len == right.len { if left.len == right.len {
p.scope.register(it.name, ast.Var{ p.scope.register(lx.name, ast.Var{
name: it.name name: lx.name
expr: right[i] expr: right[i]
is_mut: it.is_mut || p.inside_for is_mut: lx.is_mut || p.inside_for
pos: it.pos pos: lx.pos
}) })
} else { } else {
p.scope.register(it.name, ast.Var{ p.scope.register(lx.name, ast.Var{
name: it.name name: lx.name
is_mut: it.is_mut || p.inside_for is_mut: lx.is_mut || p.inside_for
pos: it.pos pos: lx.pos
}) })
} }
} }
} }
ast.IndexExpr { ast.IndexExpr {
it.is_setter = true lx.is_setter = true
} }
ast.ParExpr {} ast.ParExpr {}
ast.PrefixExpr {} ast.PrefixExpr {}
ast.SelectorExpr { ast.SelectorExpr {
if op == .decl_assign { if op == .decl_assign {
p.error_with_pos('struct fields can only be declared during the initialization', it.pos) p.error_with_pos('struct fields can only be declared during the initialization', lx.pos)
} }
} }
else {} else {}