Skip to content

Commit

Permalink
checker: disallow static maps: mut static x := map[string]int{} (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Jan 20, 2024
1 parent 9092d7f commit 0d9e5e5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vlib/v/checker/assign.v
Expand Up @@ -597,6 +597,13 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
}
}
}
if mut left is ast.Ident {
if mut left.info is ast.IdentVar {
if left.info.is_static && right_sym.kind == .map {
c.error('maps cannot be static', left.pos)
}
}
}
// Single side check
match node.op {
.assign {} // No need to do single side check for =. But here put it first for speed.
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/static_maps_err.out
@@ -0,0 +1,7 @@
vlib/v/checker/tests/static_maps_err.vv:3:13: error: maps cannot be static
1 | @[unsafe]
2 | fn foo() map[string]int {
3 | mut static x := map[string]int{}
| ^
4 | return x
5 | }
11 changes: 11 additions & 0 deletions vlib/v/checker/tests/static_maps_err.vv
@@ -0,0 +1,11 @@
@[unsafe]
fn foo() map[string]int {
mut static x := map[string]int{}
return x
}

fn main() {
unsafe {
foo()
}
}

0 comments on commit 0d9e5e5

Please sign in to comment.