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 {
ast.Ident {
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 {
p.scope.register(it.name, ast.Var{
name: it.name
p.scope.register(lx.name, ast.Var{
name: lx.name
expr: right[i]
is_mut: it.is_mut || p.inside_for
pos: it.pos
is_mut: lx.is_mut || p.inside_for
pos: lx.pos
})
} else {
p.scope.register(it.name, ast.Var{
name: it.name
is_mut: it.is_mut || p.inside_for
pos: it.pos
p.scope.register(lx.name, ast.Var{
name: lx.name
is_mut: lx.is_mut || p.inside_for
pos: lx.pos
})
}
}
}
ast.IndexExpr {
it.is_setter = true
lx.is_setter = true
}
ast.ParExpr {}
ast.PrefixExpr {}
ast.SelectorExpr {
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 {}