parser: check undefined variables in assign_stmt (#10755)

pull/10770/head
yuyi 2021-07-12 00:09:35 +08:00 committed by GitHub
parent 533f09f7a2
commit 938e9b61b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 1 deletions

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/assign_expr_undefined_err_j.vv:2:12: error: undefined variable: `a`
1 | fn main() {
2 | mut a := [a]
| ^
3 | println(a)
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
mut a := [a]
println(a)
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/assign_expr_undefined_err_k.vv:2:22: error: undefined variable: `a`
1 | fn main() {
2 | mut a := map{'one': a}
| ^
3 | println(a)
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
mut a := map{'one': a}
println(a)
}

View File

@ -23,7 +23,7 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? {
ast.Ident {
for expr in exprs {
if expr is ast.Ident {
if expr.name == val.name {
if expr.name == val.name && expr.kind != .blank_ident {
p.error_with_pos('undefined variable: `$val.name`', val.pos)
return error('undefined variable: `$val.name`')
}
@ -40,6 +40,9 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? {
if val.has_default {
p.check_undefined_variables(exprs, val.default_expr) ?
}
for expr in val.exprs {
p.check_undefined_variables(exprs, expr) ?
}
}
ast.CallExpr {
p.check_undefined_variables(exprs, val.left) ?
@ -51,6 +54,14 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? {
p.check_undefined_variables(exprs, val.left) ?
p.check_undefined_variables(exprs, val.right) ?
}
ast.MapInit {
for key in val.keys {
p.check_undefined_variables(exprs, key) ?
}
for value in val.vals {
p.check_undefined_variables(exprs, value) ?
}
}
ast.ParExpr {
p.check_undefined_variables(exprs, val.expr) ?
}