From 1e504fb388b0a9413413387f055d4be8173387c2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 29 May 2020 23:38:40 +0800 Subject: [PATCH] parser: check undefined variable of assign_expr --- vlib/v/checker/tests/assign_expr_undefined_err_e.out | 6 ++++++ vlib/v/checker/tests/assign_expr_undefined_err_e.vv | 4 ++++ vlib/v/checker/tests/assign_expr_undefined_err_f.out | 6 ++++++ vlib/v/checker/tests/assign_expr_undefined_err_f.vv | 4 ++++ vlib/v/parser/assign.v | 9 +++++++++ 5 files changed, 29 insertions(+) create mode 100644 vlib/v/checker/tests/assign_expr_undefined_err_e.out create mode 100644 vlib/v/checker/tests/assign_expr_undefined_err_e.vv create mode 100644 vlib/v/checker/tests/assign_expr_undefined_err_f.out create mode 100644 vlib/v/checker/tests/assign_expr_undefined_err_f.vv diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_e.out b/vlib/v/checker/tests/assign_expr_undefined_err_e.out new file mode 100644 index 0000000000..185abb3be8 --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_undefined_err_e.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/assign_expr_undefined_err_e.v:2:11: error: undefined variable: `a` + 1 | fn main() { + 2 | a, b := -a, -b + | ^ + 3 | println(s) + 4 | } diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_e.vv b/vlib/v/checker/tests/assign_expr_undefined_err_e.vv new file mode 100644 index 0000000000..47e4ed5f6a --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_undefined_err_e.vv @@ -0,0 +1,4 @@ +fn main() { + a, b := -a, -b + println(s) +} diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_f.out b/vlib/v/checker/tests/assign_expr_undefined_err_f.out new file mode 100644 index 0000000000..62f7ed3c89 --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_undefined_err_f.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/assign_expr_undefined_err_f.v:2:12: error: undefined variable: `a` + 1 | fn main() { + 2 | a, b := (-a + 1), 1 + | ^ + 3 | println('$a, $b') + 4 | } diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_f.vv b/vlib/v/checker/tests/assign_expr_undefined_err_f.vv new file mode 100644 index 0000000000..79ec34862c --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_undefined_err_f.vv @@ -0,0 +1,4 @@ +fn main() { + a, b := (-a + 1), 1 + println('$a, $b') +} diff --git a/vlib/v/parser/assign.v b/vlib/v/parser/assign.v index 733af7cf9c..6917b819a3 100644 --- a/vlib/v/parser/assign.v +++ b/vlib/v/parser/assign.v @@ -22,6 +22,15 @@ fn (mut p Parser) check_undefined_variables(idents []ast.Ident, expr ast.Expr) { p.check_undefined_variables(idents, it.left) p.check_undefined_variables(idents, it.right) } + ast.ParExpr { + p.check_undefined_variables(idents, it.expr) + } + ast.PostfixExpr { + p.check_undefined_variables(idents, it.expr) + } + ast.PrefixExpr { + p.check_undefined_variables(idents, it.right) + } ast.StringInterLiteral { for expr_ in it.exprs { p.check_undefined_variables(idents, expr_)