From 83dfc6b9b9caeb2f274234a3398365d10d50faa2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 10 Apr 2020 15:38:43 +0300 Subject: [PATCH] checker: add more checks for multivar assignments --- Makefile | 4 ++-- vlib/v/checker/checker.v | 11 +++++++++-- vlib/v/parser/parser.v | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 064882b102..d31a5c0dda 100644 --- a/Makefile +++ b/Makefile @@ -94,10 +94,10 @@ $(TMPVC)/.git/config: $(MAKE) fresh_vc selfcompile: - ./v -csource keep -cg -o v cmd/v + ./v -keepc -cg -o v cmd/v selfcompile-static: - ./v -csource keep -cg -cflags '--static' -o v-static cmd/v + ./v -keepc -cg -cflags '--static' -o v-static cmd/v modules: module_builtin module_strings module_strconv module_builtin: diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ec97a62f4c..48ffeb8778 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -590,14 +590,21 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) { } right_type := c.expr(assign_stmt.right[0]) right_type_sym := c.table.get_type_symbol(right_type) - mr_info := right_type_sym.mr_info() if right_type_sym.kind != .multi_return { - c.error('wrong number of vars', assign_stmt.pos) + c.error('expression on the right does not return multiple values, while at least $assign_stmt.left.len are expected', assign_stmt.pos) + return + } + mr_info := right_type_sym.mr_info() + if mr_info.types.len < assign_stmt.left.len { + c.error('right expression returns only $mr_info.types.len values, but left one expects $assign_stmt.left.len', assign_stmt.pos) } mut scope := c.file.scope.innermost(assign_stmt.pos.pos) for i, _ in assign_stmt.left { mut ident := assign_stmt.left[i] mut ident_var_info := ident.var_info() + if i >= mr_info.types.len { + continue + } val_type := mr_info.types[i] if assign_stmt.op == .assign { var_type := c.expr(ident) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 7d87e2bebb..252d24ba79 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1712,9 +1712,9 @@ fn (p mut Parser) assign_stmt() ast.Stmt { p.next() } idents := p.parse_assign_lhs() - pos := p.tok.position() op := p.tok.kind p.next() // :=, = + pos := p.tok.position() exprs := p.parse_assign_rhs() is_decl := op == .decl_assign for i, ident in idents {