parser: fix if-guard redefinition (#9425)

pull/9429/head
ka-weihe 2021-03-23 04:38:36 +01:00 committed by GitHub
parent 1b572f75e8
commit 3753a58ce0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -87,6 +87,9 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
is_guard = true
var_pos := p.tok.position()
var_name := p.check_name()
if p.scope.known_var(var_name) {
p.error_with_pos('redefinition of `$var_name`', var_pos)
}
comments << p.eat_comments({})
p.check(.decl_assign)
comments << p.eat_comments({})

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/if_guard_redefinition.vv:7:8: error: redefinition of `x`
5 | fn main() {
6 | x := 1
7 | if x := opt_fn() {
| ^
8 | println(x)
9 | }

View File

@ -0,0 +1,10 @@
fn opt_fn() ?int {
return 2
}
fn main() {
x := 1
if x := opt_fn() {
println(x)
}
}