Skip to content

Commit 6257768

Browse files
committed
fix if expr in lock
1 parent 5c89715 commit 6257768

5 files changed

Lines changed: 40 additions & 6 deletions

File tree

vlib/v/checker/checker.v

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,6 +3443,7 @@ pub fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
34433443
}
34443444

34453445
pub fn (mut c Checker) lock_expr(mut node ast.LockExpr) ast.Type {
3446+
expected_type := c.expected_type
34463447
if c.rlocked_names.len > 0 || c.locked_names.len > 0 {
34473448
c.error('nested `lock`/`rlock` not allowed', node.pos)
34483449
}
@@ -3466,16 +3467,17 @@ pub fn (mut c Checker) lock_expr(mut node ast.LockExpr) ast.Type {
34663467
}
34673468
}
34683469
c.stmts(node.stmts)
3469-
c.rlocked_names = []
3470-
c.locked_names = []
34713470
// handle `x := rlock a { a.getval() }`
34723471
mut ret_type := ast.void_type
34733472
if node.stmts.len > 0 {
34743473
last_stmt := node.stmts[node.stmts.len - 1]
34753474
if last_stmt is ast.ExprStmt {
3476-
ret_type = last_stmt.typ
3475+
c.expected_type = expected_type
3476+
ret_type = c.expr(last_stmt.expr)
34773477
}
34783478
}
3479+
c.rlocked_names = []
3480+
c.locked_names = []
34793481
if ret_type != ast.void_type {
34803482
node.is_expr = true
34813483
}

vlib/v/checker/tests/lock_already_locked.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ vlib/v/checker/tests/lock_already_locked.vv:11:3: error: nested `lock`/`rlock` n
55
| ~~~~~
66
12 | a.x++
77
13 | }
8+
vlib/v/checker/tests/lock_already_locked.vv:12:4: error: a has an `rlock` but needs a `lock`
9+
10 | lock a {
10+
11 | rlock a {
11+
12 | a.x++
12+
| ^
13+
13 | }
14+
14 | }
815
vlib/v/checker/tests/lock_already_locked.vv:15:10: error: `a` is `shared` and must be `rlock`ed or `lock`ed to be used as non-mut argument to print
916
13 | }
1017
14 | }

vlib/v/gen/c/assign.v

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,11 @@ fn (mut g Gen) gen_assign_stmt(node_ ast.AssignStmt) {
430430
if val.is_auto_deref_var() {
431431
g.write('*')
432432
}
433-
g.expr(val)
433+
if val_type.has_flag(.shared_f) {
434+
g.expr_with_cast(val, val_type, var_type)
435+
} else {
436+
g.expr(val)
437+
}
434438
if is_auto_heap {
435439
g.write('))')
436440
}

vlib/v/gen/c/if.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
7575
g.write(' ? ')
7676
}
7777
prev_expected_cast_type := g.expected_cast_type
78-
if node.is_expr && g.table.sym(node.typ).kind == .sum_type {
78+
if node.is_expr
79+
&& (g.table.sym(node.typ).kind == .sum_type || node.typ.has_flag(.shared_f)) {
7980
g.expected_cast_type = node.typ
8081
}
8182
g.stmts(branch.stmts)
@@ -201,7 +202,8 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
201202
}
202203
if needs_tmp_var {
203204
prev_expected_cast_type := g.expected_cast_type
204-
if node.is_expr && g.table.sym(node.typ).kind == .sum_type {
205+
if node.is_expr
206+
&& (g.table.sym(node.typ).kind == .sum_type || node.typ.has_flag(.shared_f)) {
205207
g.expected_cast_type = node.typ
206208
}
207209
g.stmts_with_tmp_var(branch.stmts, tmp)

vlib/v/tests/shared_if_expr_test.v

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type AA = bool | int
2+
3+
fn test_shared_if_expr() {
4+
shared a := [1, 2, 3]
5+
b := [4, 5, 6]
6+
c := lock a {
7+
if a == b { a } else { b }
8+
}
9+
assert c == [4, 5, 6]
10+
d := lock a {
11+
if a != b {
12+
a << 5
13+
a
14+
} else {
15+
b
16+
}
17+
}
18+
assert d == [1, 2, 3, 5]
19+
}

0 commit comments

Comments
 (0)