v: fix AssignStmt

pull/3984/head
Joe Conigliaro 2020-03-11 00:45:21 +11:00
parent 2f0bb11a96
commit ddd5a9443d
2 changed files with 23 additions and 15 deletions

View File

@ -388,9 +388,10 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
ident.info = var_info ident.info = var_info
assign_stmt.left[i] = ident assign_stmt.left[i] = ident
if assign_stmt.op == .assign { 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) 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) 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) 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]
val := assign_stmt.right[i] val_type := c.expr(assign_stmt.right[i])
val_type := c.expr(val)
if assign_stmt.op == .assign { if assign_stmt.op == .assign {
var_info := ident.var_info() var_type := c.expr(ident)
if !c.table.check(val_type, var_info.typ) { if !c.table.check(val_type, var_type) {
val_type_sym := c.table.get_type_symbol(val_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) 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 { else {
mut var_info := ident.var_info() mut ident_var_info := ident.var_info()
var_info.typ = val_type ident_var_info.typ = val_type
ident.info = var_info ident.info = ident_var_info
assign_stmt.left[i] = ident assign_stmt.left[i] = ident
} }
scope.override_var(ast.Var{ scope.override_var(ast.Var{

View File

@ -1514,7 +1514,7 @@ fn (p mut Parser) assign_stmt() ast.Stmt {
p.next() // :=, = p.next() // :=, =
exprs := p.parse_assign_rhs() exprs := p.parse_assign_rhs()
is_decl := op == .decl_assign is_decl := op == .decl_assign
for ident in idents { for i, ident in idents {
known_var := p.scope.known_var(ident.name) known_var := p.scope.known_var(ident.name)
if !is_decl && !known_var { if !is_decl && !known_var {
p.error('unknown variable `$ident.name`') 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) { if p.scope.known_var(ident.name) {
p.error('redefinition of `$ident.name`') p.error('redefinition of `$ident.name`')
} }
p.scope.register_var(ast.Var{ if idents.len == exprs.len {
name: ident.name 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{ return ast.AssignStmt{