parser: check undefined variables in assign_stmt (#10755)
parent
533f09f7a2
commit
938e9b61b5
|
@ -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 | }
|
|
@ -0,0 +1,4 @@
|
|||
fn main() {
|
||||
mut a := [a]
|
||||
println(a)
|
||||
}
|
|
@ -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 | }
|
|
@ -0,0 +1,4 @@
|
|||
fn main() {
|
||||
mut a := map{'one': a}
|
||||
println(a)
|
||||
}
|
|
@ -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) ?
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue