checker: fix static init var (fix #25203) (#25209)

This commit is contained in:
Felipe Pena 2025-09-01 15:12:04 -03:00 committed by GitHub
parent 487feb9b0e
commit 1e0bda4ebc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 76 additions and 2 deletions

View file

@ -2306,6 +2306,23 @@ pub fn (expr Expr) pos() token.Pos {
} }
} }
pub fn (expr Expr) is_constant() bool {
return match expr {
IntegerLiteral, FloatLiteral, BoolLiteral, StringLiteral {
true
}
InfixExpr, CastExpr, ArrayInit {
true
}
UnsafeExpr {
expr.expr.is_constant()
}
else {
return false
}
}
}
pub fn (expr Expr) is_lvalue() bool { pub fn (expr Expr) is_lvalue() bool {
return match expr { return match expr {
Ident, CTempVar { true } Ident, CTempVar { true }

View file

@ -661,8 +661,13 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
} }
} }
if mut left is ast.Ident && left.info is ast.IdentVar { if mut left is ast.Ident && left.info is ast.IdentVar {
if left.info.is_static && right_sym.kind == .map { if left.info.is_static {
if right_sym.kind == .map {
c.error('maps cannot be static', left.pos) c.error('maps cannot be static', left.pos)
} else if !right.is_constant() {
c.error('cannot initialized static variable with non-constant value',
left.pos)
}
} }
} }
// Single side check // Single side check

View file

@ -0,0 +1,41 @@
vlib/v/checker/tests/static_init_err.vv:10:2: warning: function `foo` must be called from an `unsafe` block
8 |
9 | fn main() {
10 | foo()
| ~~~~~
11 | }
vlib/v/checker/tests/static_init_err.vv:4:9: warning: unused variable: `aa`
2 | fn foo() {
3 | b := 23
4 | static aa := b
| ~~
5 | static bb := 1 + 1
6 | static cc := ''.str()
vlib/v/checker/tests/static_init_err.vv:5:9: warning: unused variable: `bb`
3 | b := 23
4 | static aa := b
5 | static bb := 1 + 1
| ~~
6 | static cc := ''.str()
7 | }
vlib/v/checker/tests/static_init_err.vv:6:9: warning: unused variable: `cc`
4 | static aa := b
5 | static bb := 1 + 1
6 | static cc := ''.str()
| ~~
7 | }
8 |
vlib/v/checker/tests/static_init_err.vv:4:9: error: cannot initialized static variable with non-constant value
2 | fn foo() {
3 | b := 23
4 | static aa := b
| ~~
5 | static bb := 1 + 1
6 | static cc := ''.str()
vlib/v/checker/tests/static_init_err.vv:6:9: error: cannot initialized static variable with non-constant value
4 | static aa := b
5 | static bb := 1 + 1
6 | static cc := ''.str()
| ~~
7 | }
8 |

View file

@ -0,0 +1,11 @@
@[unsafe]
fn foo() {
b := 23
static aa := b
static bb := 1 + 1
static cc := ''.str()
}
fn main() {
foo()
}