checker: disallow static maps: mut static x := map[string]int{} (#20596)

This commit is contained in:
Swastik Baranwal 2024-01-20 22:46:44 +05:30 committed by GitHub
parent 9092d7fd64
commit 0d9e5e5747
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 0 deletions

View file

@ -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 // Single side check
match node.op { match node.op {
.assign {} // No need to do single side check for =. But here put it first for speed. .assign {} // No need to do single side check for =. But here put it first for speed.

View file

@ -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 | }

View file

@ -0,0 +1,11 @@
@[unsafe]
fn foo() map[string]int {
mut static x := map[string]int{}
return x
}
fn main() {
unsafe {
foo()
}
}