From f3c5f24c170a0aaaea63acf290af447f3f623320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Tue, 9 Feb 2021 16:09:10 +0100 Subject: [PATCH] allow `lock` expression to return multiple objects (#8657) --- vlib/v/checker/checker.v | 3 ++- vlib/v/gen/c/cgen.v | 1 + vlib/v/tests/shared_lock_expr_test.v | 29 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 873c2a5859..6fb275302d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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), diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index f0928bf778..deff9a6fc0 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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 {} diff --git a/vlib/v/tests/shared_lock_expr_test.v b/vlib/v/tests/shared_lock_expr_test.v index fa488d3551..488ff9fa43 100644 --- a/vlib/v/tests/shared_lock_expr_test.v +++ b/vlib/v/tests/shared_lock_expr_test.v @@ -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 +}