Skip to content

Commit 1e0bda4

Browse files
authored
checker: fix static init var (fix #25203) (#25209)
1 parent 487feb9 commit 1e0bda4

4 files changed

Lines changed: 76 additions & 2 deletions

File tree

vlib/v/ast/ast.v

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,23 @@ pub fn (expr Expr) pos() token.Pos {
23062306
}
23072307
}
23082308

2309+
pub fn (expr Expr) is_constant() bool {
2310+
return match expr {
2311+
IntegerLiteral, FloatLiteral, BoolLiteral, StringLiteral {
2312+
true
2313+
}
2314+
InfixExpr, CastExpr, ArrayInit {
2315+
true
2316+
}
2317+
UnsafeExpr {
2318+
expr.expr.is_constant()
2319+
}
2320+
else {
2321+
return false
2322+
}
2323+
}
2324+
}
2325+
23092326
pub fn (expr Expr) is_lvalue() bool {
23102327
return match expr {
23112328
Ident, CTempVar { true }

vlib/v/checker/assign.v

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,13 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
661661
}
662662
}
663663
if mut left is ast.Ident && left.info is ast.IdentVar {
664-
if left.info.is_static && right_sym.kind == .map {
665-
c.error('maps cannot be static', left.pos)
664+
if left.info.is_static {
665+
if right_sym.kind == .map {
666+
c.error('maps cannot be static', left.pos)
667+
} else if !right.is_constant() {
668+
c.error('cannot initialized static variable with non-constant value',
669+
left.pos)
670+
}
666671
}
667672
}
668673
// Single side check
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
vlib/v/checker/tests/static_init_err.vv:10:2: warning: function `foo` must be called from an `unsafe` block
2+
8 |
3+
9 | fn main() {
4+
10 | foo()
5+
| ~~~~~
6+
11 | }
7+
vlib/v/checker/tests/static_init_err.vv:4:9: warning: unused variable: `aa`
8+
2 | fn foo() {
9+
3 | b := 23
10+
4 | static aa := b
11+
| ~~
12+
5 | static bb := 1 + 1
13+
6 | static cc := ''.str()
14+
vlib/v/checker/tests/static_init_err.vv:5:9: warning: unused variable: `bb`
15+
3 | b := 23
16+
4 | static aa := b
17+
5 | static bb := 1 + 1
18+
| ~~
19+
6 | static cc := ''.str()
20+
7 | }
21+
vlib/v/checker/tests/static_init_err.vv:6:9: warning: unused variable: `cc`
22+
4 | static aa := b
23+
5 | static bb := 1 + 1
24+
6 | static cc := ''.str()
25+
| ~~
26+
7 | }
27+
8 |
28+
vlib/v/checker/tests/static_init_err.vv:4:9: error: cannot initialized static variable with non-constant value
29+
2 | fn foo() {
30+
3 | b := 23
31+
4 | static aa := b
32+
| ~~
33+
5 | static bb := 1 + 1
34+
6 | static cc := ''.str()
35+
vlib/v/checker/tests/static_init_err.vv:6:9: error: cannot initialized static variable with non-constant value
36+
4 | static aa := b
37+
5 | static bb := 1 + 1
38+
6 | static cc := ''.str()
39+
| ~~
40+
7 | }
41+
8 |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@[unsafe]
2+
fn foo() {
3+
b := 23
4+
static aa := b
5+
static bb := 1 + 1
6+
static cc := ''.str()
7+
}
8+
9+
fn main() {
10+
foo()
11+
}

0 commit comments

Comments
 (0)