Skip to content

Commit fb528cd

Browse files
authored
checker: check shared variables types (fix #23313, fix #23314) (#23316)
1 parent d9f5112 commit fb528cd

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

vlib/v/checker/assign.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
217217
} else if right is ast.ComptimeSelector {
218218
right_type = c.comptime.comptime_for_field_type
219219
}
220+
if is_decl && left is ast.Ident && left.info is ast.IdentVar
221+
&& (left.info as ast.IdentVar).share == .shared_t && c.table.sym(right_type).kind !in [.array, .map, .struct] {
222+
c.fatal('shared variables must be structs, arrays or maps', right.pos())
223+
}
220224
if is_decl || is_shared_re_assign {
221225
// check generic struct init and return unwrap generic struct type
222226
if mut right is ast.StructInit {

vlib/v/checker/tests/globals/assign_global_to_shared_err.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ vlib/v/checker/tests/globals/assign_global_to_shared_err.vv:5:14: error: cannot
1010
5 | shared b := a
1111
| ^
1212
6 | }
13+
vlib/v/checker/tests/globals/assign_global_to_shared_err.vv:5:14: error: cannot copy map: call `move` or `clone` method (or use a reference)
14+
3 |
15+
4 | fn main() {
16+
5 | shared b := a
17+
| ^
18+
6 | }

vlib/v/checker/tests/globals/assign_global_to_shared_err.vv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@[has_globals]
2-
__global a = 0
2+
__global a = {1: 'one', 2: 'two'}
33

44
fn main() {
55
shared b := a
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/shared_variables_type_err.vv:7:16: error: shared variables must be structs, arrays or maps
2+
5 |
3+
6 | fn main() {
4+
7 | shared foo := Foo.one
5+
| ~~~~~~~
6+
8 | rlock foo {
7+
9 | match foo {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
enum Foo {
2+
zero
3+
one
4+
}
5+
6+
fn main() {
7+
shared foo := Foo.one
8+
rlock foo {
9+
match foo {
10+
.zero { println('0000') }
11+
.one { println('1111') }
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)