From ddd5a9443d3b1a403d1c6c22b21854bc753e5799 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Wed, 11 Mar 2020 00:45:21 +1100 Subject: [PATCH] v: fix AssignStmt --- vlib/v/checker/checker.v | 22 +++++++++++----------- vlib/v/parser/parser.v | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 371bf25a8d..1f1fd23447 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -388,9 +388,10 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) { ident.info = var_info assign_stmt.left[i] = ident if assign_stmt.op == .assign { - if !c.table.check(val_type, var_info.typ) { + var_type := c.expr(ident) + if !c.table.check(val_type, var_type) { val_type_sym := c.table.get_type_symbol(val_type) - var_type_sym := c.table.get_type_symbol(var_info.typ) + var_type_sym := c.table.get_type_symbol(var_type) c.error('assign stmt: cannot use `$val_type_sym.name` as `$var_type_sym.name`', assign_stmt.pos) } } @@ -408,20 +409,19 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) { mut scope := c.file.scope.innermost(assign_stmt.pos.pos) for i, _ in assign_stmt.left { mut ident := assign_stmt.left[i] - val := assign_stmt.right[i] - val_type := c.expr(val) + val_type := c.expr(assign_stmt.right[i]) if assign_stmt.op == .assign { - var_info := ident.var_info() - if !c.table.check(val_type, var_info.typ) { + var_type := c.expr(ident) + if !c.table.check(val_type, var_type) { val_type_sym := c.table.get_type_symbol(val_type) - var_type_sym := c.table.get_type_symbol(var_info.typ) + var_type_sym := c.table.get_type_symbol(var_type) c.error('assign stmt: cannot use `$val_type_sym.name` as `$var_type_sym.name`', assign_stmt.pos) } } - else if assign_stmt.op == .decl_assign { - mut var_info := ident.var_info() - var_info.typ = val_type - ident.info = var_info + else { + mut ident_var_info := ident.var_info() + ident_var_info.typ = val_type + ident.info = ident_var_info assign_stmt.left[i] = ident } scope.override_var(ast.Var{ diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 50ce1802a9..1887ec9dc8 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1514,7 +1514,7 @@ fn (p mut Parser) assign_stmt() ast.Stmt { p.next() // :=, = exprs := p.parse_assign_rhs() is_decl := op == .decl_assign - for ident in idents { + for i, ident in idents { known_var := p.scope.known_var(ident.name) if !is_decl && !known_var { p.error('unknown variable `$ident.name`') @@ -1523,9 +1523,17 @@ fn (p mut Parser) assign_stmt() ast.Stmt { if p.scope.known_var(ident.name) { p.error('redefinition of `$ident.name`') } - p.scope.register_var(ast.Var{ - name: ident.name - }) + if idents.len == exprs.len { + p.scope.register_var(ast.Var{ + name: ident.name + expr: exprs[i] + }) + } + else { + p.scope.register_var(ast.Var{ + name: ident.name + }) + } } } return ast.AssignStmt{