checker: add more checks for multivar assignments
parent
3573030b9b
commit
83dfc6b9b9
4
Makefile
4
Makefile
|
@ -94,10 +94,10 @@ $(TMPVC)/.git/config:
|
||||||
$(MAKE) fresh_vc
|
$(MAKE) fresh_vc
|
||||||
|
|
||||||
selfcompile:
|
selfcompile:
|
||||||
./v -csource keep -cg -o v cmd/v
|
./v -keepc -cg -o v cmd/v
|
||||||
|
|
||||||
selfcompile-static:
|
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
|
modules: module_builtin module_strings module_strconv
|
||||||
module_builtin:
|
module_builtin:
|
||||||
|
|
|
@ -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 := c.expr(assign_stmt.right[0])
|
||||||
right_type_sym := c.table.get_type_symbol(right_type)
|
right_type_sym := c.table.get_type_symbol(right_type)
|
||||||
mr_info := right_type_sym.mr_info()
|
|
||||||
if right_type_sym.kind != .multi_return {
|
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)
|
mut scope := c.file.scope.innermost(assign_stmt.pos.pos)
|
||||||
for i, _ in assign_stmt.left {
|
for i, _ in assign_stmt.left {
|
||||||
mut ident := assign_stmt.left[i]
|
mut ident := assign_stmt.left[i]
|
||||||
mut ident_var_info := ident.var_info()
|
mut ident_var_info := ident.var_info()
|
||||||
|
if i >= mr_info.types.len {
|
||||||
|
continue
|
||||||
|
}
|
||||||
val_type := mr_info.types[i]
|
val_type := mr_info.types[i]
|
||||||
if assign_stmt.op == .assign {
|
if assign_stmt.op == .assign {
|
||||||
var_type := c.expr(ident)
|
var_type := c.expr(ident)
|
||||||
|
|
|
@ -1712,9 +1712,9 @@ fn (p mut Parser) assign_stmt() ast.Stmt {
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
idents := p.parse_assign_lhs()
|
idents := p.parse_assign_lhs()
|
||||||
pos := p.tok.position()
|
|
||||||
op := p.tok.kind
|
op := p.tok.kind
|
||||||
p.next() // :=, =
|
p.next() // :=, =
|
||||||
|
pos := p.tok.position()
|
||||||
exprs := p.parse_assign_rhs()
|
exprs := p.parse_assign_rhs()
|
||||||
is_decl := op == .decl_assign
|
is_decl := op == .decl_assign
|
||||||
for i, ident in idents {
|
for i, ident in idents {
|
||||||
|
|
Loading…
Reference in New Issue