File tree Expand file tree Collapse file tree 5 files changed +37
-5
lines changed Expand file tree Collapse file tree 5 files changed +37
-5
lines changed Original file line number Diff line number Diff line change @@ -3537,6 +3537,7 @@ pub fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
3537
3537
}
3538
3538
3539
3539
pub fn (mut c Checker) lock_expr (mut node ast.LockExpr) ast.Type {
3540
+ expected_type := c.expected_type
3540
3541
if c.rlocked_names.len > 0 || c.locked_names.len > 0 {
3541
3542
c.error ('nested `lock`/`rlock` not allowed' , node.pos)
3542
3543
}
@@ -3560,16 +3561,17 @@ pub fn (mut c Checker) lock_expr(mut node ast.LockExpr) ast.Type {
3560
3561
}
3561
3562
}
3562
3563
c.stmts (node.stmts)
3563
- c.rlocked_names = []
3564
- c.locked_names = []
3565
3564
// handle `x := rlock a { a.getval() }`
3566
3565
mut ret_type := ast.void_type
3567
3566
if node.stmts.len > 0 {
3568
3567
last_stmt := node.stmts.last ()
3569
3568
if last_stmt is ast.ExprStmt {
3570
- ret_type = last_stmt.typ
3569
+ c.expected_type = expected_type
3570
+ ret_type = c.expr (last_stmt.expr)
3571
3571
}
3572
3572
}
3573
+ c.rlocked_names = []
3574
+ c.locked_names = []
3573
3575
if ret_type != ast.void_type {
3574
3576
node.is_expr = true
3575
3577
}
Original file line number Diff line number Diff line change @@ -5,6 +5,13 @@ vlib/v/checker/tests/lock_already_locked.vv:11:3: error: nested `lock`/`rlock` n
5
5
| ~~~~~
6
6
12 | a.x++
7
7
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 | }
8
15
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
9
16
13 | }
10
17
14 | }
Original file line number Diff line number Diff line change @@ -456,6 +456,8 @@ fn (mut g Gen) gen_assign_stmt(node_ ast.AssignStmt) {
456
456
}
457
457
if val is ast.ArrayInit {
458
458
g.array_init (val, ident.name)
459
+ } else if val_type.has_flag (.shared_f) {
460
+ g.expr_with_cast (val, val_type, var_type)
459
461
} else {
460
462
g.expr (val)
461
463
}
Original file line number Diff line number Diff line change @@ -78,7 +78,8 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
78
78
g.write (' ? ' )
79
79
}
80
80
prev_expected_cast_type := g.expected_cast_type
81
- if node.is_expr && g.table.sym (node.typ).kind == .sum_type {
81
+ if node.is_expr
82
+ && (g.table.sym (node.typ).kind == .sum_type || node.typ.has_flag (.shared_f)) {
82
83
g.expected_cast_type = node.typ
83
84
}
84
85
g.stmts (branch.stmts)
@@ -204,7 +205,8 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
204
205
}
205
206
if needs_tmp_var {
206
207
prev_expected_cast_type := g.expected_cast_type
207
- if node.is_expr && g.table.sym (node.typ).kind == .sum_type {
208
+ if node.is_expr
209
+ && (g.table.sym (node.typ).kind == .sum_type || node.typ.has_flag (.shared_f)) {
208
210
g.expected_cast_type = node.typ
209
211
}
210
212
g.stmts_with_tmp_var (branch.stmts, tmp)
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments