Skip to content

Commit

Permalink
allow lock expression to return multiple objects (#8657)
Browse files Browse the repository at this point in the history
  • Loading branch information
UweKrueger committed Feb 9, 2021
1 parent d37fb56 commit f3c5f24
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/checker/checker.v
Expand Up @@ -2551,7 +2551,8 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
mut right_len := assign_stmt.right.len
mut right_type0 := table.void_type
for right in assign_stmt.right {
if right is ast.CallExpr || right is ast.IfExpr || right is ast.MatchExpr {
if right is ast.CallExpr || right is ast.IfExpr || right is ast.LockExpr
|| right is ast.MatchExpr {
right_type0 = c.expr(right)
assign_stmt.right_types = [
c.check_expr_opt_call(right, right_type0),
Expand Down
1 change: 1 addition & 0 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -1742,6 +1742,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
right_expr := assign_stmt.right[0]
match right_expr {
ast.CallExpr { return_type = right_expr.return_type }
ast.LockExpr { return_type = right_expr.typ }
ast.MatchExpr { return_type = right_expr.return_type }
ast.IfExpr { return_type = right_expr.typ }
else {}
Expand Down
29 changes: 29 additions & 0 deletions vlib/v/tests/shared_lock_expr_test.v
Expand Up @@ -12,3 +12,32 @@ fn test_lock_expr() {
assert m == -57
assert n == 173
}

struct Abc {
mut:
a f64
}

fn test_multi_objects() {
shared x := Abc{ a: 12.5 }
shared y := Abc{ a: -7.5 }
shared z := Abc{ a: 13.125 }
a, b, c := rlock z, x, y { y.a, z.a, x.a }
assert a == -7.5
assert b == 13.125
assert c == 12.5
}

fn (mut st Abc) getvals(mut a Abc, mut b Abc) (f64, f64, f64) {
return a.a, st.a, b.a
}

fn test_mult_ret_method() {
shared x := Abc{ a: 12.5 }
shared y := Abc{ a: -7.5 }
shared z := Abc{ a: 13.125 }
a, b, c := lock z, x, y { z.getvals(mut x, mut y) }
assert a == 12.5
assert b == 13.125
assert c == -7.5
}

0 comments on commit f3c5f24

Please sign in to comment.