Skip to content

Commit

Permalink
checker: check shared variables types (fix #23313, fix #23314) (#23316)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Dec 30, 2024
1 parent d9f5112 commit fb528cd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
} else if right is ast.ComptimeSelector {
right_type = c.comptime.comptime_for_field_type
}
if is_decl && left is ast.Ident && left.info is ast.IdentVar
&& (left.info as ast.IdentVar).share == .shared_t && c.table.sym(right_type).kind !in [.array, .map, .struct] {
c.fatal('shared variables must be structs, arrays or maps', right.pos())
}
if is_decl || is_shared_re_assign {
// check generic struct init and return unwrap generic struct type
if mut right is ast.StructInit {
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/globals/assign_global_to_shared_err.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ vlib/v/checker/tests/globals/assign_global_to_shared_err.vv:5:14: error: cannot
5 | shared b := a
| ^
6 | }
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)
3 |
4 | fn main() {
5 | shared b := a
| ^
6 | }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@[has_globals]
__global a = 0
__global a = {1: 'one', 2: 'two'}

fn main() {
shared b := a
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/shared_variables_type_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/shared_variables_type_err.vv:7:16: error: shared variables must be structs, arrays or maps
5 |
6 | fn main() {
7 | shared foo := Foo.one
| ~~~~~~~
8 | rlock foo {
9 | match foo {
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/shared_variables_type_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
enum Foo {
zero
one
}

fn main() {
shared foo := Foo.one
rlock foo {
match foo {
.zero { println('0000') }
.one { println('1111') }
}
}
}

0 comments on commit fb528cd

Please sign in to comment.