parser: check assign expr with undefined variable (#13653)
parent
63b41e67fa
commit
f70e5bd69b
|
@ -0,0 +1,7 @@
|
|||
vlib/v/checker/tests/assign_expr_undefined_err_m.vv:5:14: error: undefined variable: `x`
|
||||
3 | n := 2
|
||||
4 | x := match a {
|
||||
5 | 'add'{ n + x }
|
||||
| ^
|
||||
6 | else{0}
|
||||
7 | }
|
|
@ -0,0 +1,9 @@
|
|||
fn main(){
|
||||
a := 'add'
|
||||
n := 2
|
||||
x := match a {
|
||||
'add'{ n + x }
|
||||
else{0}
|
||||
}
|
||||
println(x)
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
vlib/v/checker/tests/assign_expr_undefined_err_n.vv:4:27: error: undefined variable: `x`
|
||||
2 | a := 'add'
|
||||
3 | n := 2
|
||||
4 | x := if a == 'add' { n + x } else { 0 }
|
||||
| ^
|
||||
5 | println(x)
|
||||
6 | }
|
|
@ -0,0 +1,6 @@
|
|||
fn main(){
|
||||
a := 'add'
|
||||
n := 2
|
||||
x := if a == 'add' { n + x } else { 0 }
|
||||
println(x)
|
||||
}
|
|
@ -63,6 +63,17 @@ 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.IfExpr {
|
||||
p.check_undefined_variables(exprs, val.left) ?
|
||||
for branch in val.branches {
|
||||
p.check_undefined_variables(exprs, branch.cond) ?
|
||||
for stmt in branch.stmts {
|
||||
if stmt is ast.ExprStmt {
|
||||
p.check_undefined_variables(exprs, stmt.expr) ?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ast.MapInit {
|
||||
for key in val.keys {
|
||||
p.check_undefined_variables(exprs, key) ?
|
||||
|
@ -71,6 +82,19 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? {
|
|||
p.check_undefined_variables(exprs, value) ?
|
||||
}
|
||||
}
|
||||
ast.MatchExpr {
|
||||
p.check_undefined_variables(exprs, val.cond) ?
|
||||
for branch in val.branches {
|
||||
for expr in branch.exprs {
|
||||
p.check_undefined_variables(exprs, expr) ?
|
||||
}
|
||||
for stmt in branch.stmts {
|
||||
if stmt is ast.ExprStmt {
|
||||
p.check_undefined_variables(exprs, stmt.expr) ?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ast.ParExpr {
|
||||
p.check_undefined_variables(exprs, val.expr) ?
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue