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 { 1 | fn counter() int {
2 | mut static icounter := 0 2 | mut static icounter := 0
| ~~~~~~~~ | ~~~~~~~~

View File

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

View File

@ -1221,6 +1221,10 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
if node.info.is_mut { if node.info.is_mut {
f.write(node.info.share.str() + ' ') 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) f.write_language_prefix(node.language)
if node.name == 'it' && f.it_name != '' && !f.inside_lambda { // allow `it` in lambdas 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, {}) f.comments(node.comments, {})
for i, left in node.left { for i, left in node.left {
if left is ast.Ident { if left is ast.Ident {
var_info := left.var_info()
if var_info.is_static {
f.write('static ')
}
f.expr(left) f.expr(left)
} else { } else {
f.expr(left) 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 name: lx.name
expr: if left.len == right.len { right[i] } else { ast.Expr{} } expr: if left.len == right.len { right[i] } else { ast.Expr{} }
share: share share: share
is_mut: lx.is_mut || p.inside_for || is_static is_mut: lx.is_mut || p.inside_for
pos: lx.pos pos: lx.pos
} }
if p.pref.autofree { if p.pref.autofree {

View File

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