1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: add more checks for multivar assignments

This commit is contained in:
Delyan Angelov 2020-04-10 15:38:43 +03:00
parent 3573030b9b
commit 83dfc6b9b9
3 changed files with 12 additions and 5 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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 {