checker: fix multi assignment for multiple fns (#5716)

pull/5724/head
Tarang11 2020-07-07 17:48:51 +05:30 committed by GitHub
parent d2d4ea42ce
commit 68e01d87be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -1536,7 +1536,9 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
}
if assign_stmt.right_types.len < assign_stmt.left.len { // first type or multi return types added above
right_type := c.expr(assign_stmt.right[i])
assign_stmt.right_types << c.check_expr_opt_call(assign_stmt.right[i], right_type)
if assign_stmt.right_types.len == i {
assign_stmt.right_types << c.check_expr_opt_call(assign_stmt.right[i], right_type)
}
}
right := if i < assign_stmt.right.len { assign_stmt.right[i] } else { assign_stmt.right[0] }
right_type := assign_stmt.right_types[i]

View File

@ -0,0 +1,55 @@
fn test() int {
return 10
}
fn test1() int {
return 11
}
fn test_fn_assignment_var() {
mut a := 0
mut b := 0
a , b = test(), test1()
assert a == 10
assert b == 11
a, b = test(), test()
assert a == 10
assert b == 10
a, b = test(), 12
assert a == 10
assert b == 12
a, b = 12, test()
assert a == 12
assert b == 10
}
fn test_fn_assignment_array() {
mut a := [0, 1]
a[0], a[1] = test(), test1()
assert a[0] == 10
assert a[1] == 11
a[0], a[1] = test() , test()
assert a[0] == 10
assert a[1] == 10
a[0], a[1] = test(), 12
assert a[0] == 10
assert a[1] == 12
a[0], a[1] = 12 , test()
assert a[0] == 12
assert a[1] == 10
}