diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_j.out b/vlib/v/checker/tests/assign_expr_undefined_err_j.out new file mode 100644 index 0000000000..5ca8f5a06d --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_undefined_err_j.out @@ -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 | } diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_j.vv b/vlib/v/checker/tests/assign_expr_undefined_err_j.vv new file mode 100644 index 0000000000..0406da790a --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_undefined_err_j.vv @@ -0,0 +1,4 @@ +fn main() { + mut a := [a] + println(a) +} diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_k.out b/vlib/v/checker/tests/assign_expr_undefined_err_k.out new file mode 100644 index 0000000000..0dd8e1ca2c --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_undefined_err_k.out @@ -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 | } diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_k.vv b/vlib/v/checker/tests/assign_expr_undefined_err_k.vv new file mode 100644 index 0000000000..e4d1e89974 --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_undefined_err_k.vv @@ -0,0 +1,4 @@ +fn main() { + mut a := map{'one': a} + println(a) +} diff --git a/vlib/v/parser/assign.v b/vlib/v/parser/assign.v index 8284230d75..eff8d0af09 100644 --- a/vlib/v/parser/assign.v +++ b/vlib/v/parser/assign.v @@ -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) ? }