1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: add missing check for global var on assignment to shared var (#18125)

This commit is contained in:
Felipe Pena 2023-05-10 12:30:57 -03:00 committed by GitHub
parent 61a5fbea35
commit d62c4c9fc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -221,6 +221,13 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
// c.error('cannot assign a `none` value to a non-option variable', right.pos())
// }
}
if mut left is ast.Ident
&& (left as ast.Ident).info is ast.IdentVar && right is ast.Ident && (right as ast.Ident).name in c.global_names {
ident_var_info := left.info as ast.IdentVar
if ident_var_info.share == .shared_t {
c.error('cannot assign global variable to shared variable', right.pos())
}
}
if right_type.is_ptr() && left_type.is_ptr() {
if mut right is ast.Ident {
if mut right.obj is ast.Var {

View File

@ -0,0 +1,12 @@
vlib/v/checker/tests/globals/assign_global_to_shared_err.vv:5:9: warning: unused variable: `b`
3 |
4 | fn main() {
5 | shared b := a
| ^
6 | }
vlib/v/checker/tests/globals/assign_global_to_shared_err.vv:5:14: error: cannot assign global variable to shared variable
3 |
4 | fn main() {
5 | shared b := a
| ^
6 | }

View File

@ -0,0 +1,6 @@
[has_globals]
__global a = 0
fn main() {
shared b := a
}