allow `lock` expression to return multiple objects (#8657)

pull/8664/head
Uwe Krüger 2021-02-09 16:09:10 +01:00 committed by GitHub
parent d37fb5641f
commit f3c5f24c17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View File

@ -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),

View File

@ -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 {}

View File

@ -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
}