fmt: fix support for `mut static x := 42`

pull/9019/head
Delyan Angelov 2021-02-28 19:38:22 +02:00
parent 4076e8eaa0
commit e354dcefc2
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
6 changed files with 20 additions and 9 deletions

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/static_vars_in_translated_mode.vv:2:13: error: static variables are supported only in -translated mode
vlib/v/checker/tests/static_vars_in_translated_mode.vv:2:13: error: static variables are supported only in -translated mode or in [unsafe] fn
1 | fn counter() int {
2 | mut static icounter := 0
| ~~~~~~~~

View File

@ -1,6 +1,5 @@
[unsafe]
fn counter() int {
static icounter := 0
mut static icounter := 0
icounter++
return icounter
}

View File

@ -1221,6 +1221,10 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
if node.info.is_mut {
f.write(node.info.share.str() + ' ')
}
var_info := node.var_info()
if var_info.is_static {
f.write('static ')
}
}
f.write_language_prefix(node.language)
if node.name == 'it' && f.it_name != '' && !f.inside_lambda { // allow `it` in lambdas
@ -2315,10 +2319,6 @@ pub fn (mut f Fmt) assign_stmt(node ast.AssignStmt) {
f.comments(node.comments, {})
for i, left in node.left {
if left is ast.Ident {
var_info := left.var_info()
if var_info.is_static {
f.write('static ')
}
f.expr(left)
} else {
f.expr(left)

View File

@ -0,0 +1,12 @@
[unsafe]
fn foo() int {
mut static x := 42
x++
return x
}
[unsafe]
fn foo() int {
static x := 42 // a immutable static is not very useful, but vfmt should support that too
return x
}

View File

@ -154,7 +154,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
name: lx.name
expr: if left.len == right.len { right[i] } else { ast.Expr{} }
share: share
is_mut: lx.is_mut || p.inside_for || is_static
is_mut: lx.is_mut || p.inside_for
pos: lx.pos
}
if p.pref.autofree {

View File

@ -1,6 +1,6 @@
[unsafe]
fn foo() int {
static x := 42
mut static x := 42
x++
return x
}