Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: check shared variables types (fix #23313) #23316

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') }
}
}
}
Loading