From e354dcefc2bbe48a45653771afd95c38ade1bc49 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 28 Feb 2021 19:38:22 +0200 Subject: [PATCH] fmt: fix support for `mut static x := 42` --- .../checker/tests/static_vars_in_translated_mode.out | 2 +- .../checker/tests/static_vars_in_translated_mode.vv | 3 +-- vlib/v/fmt/fmt.v | 8 ++++---- vlib/v/fmt/tests/static_mut_keep.vv | 12 ++++++++++++ vlib/v/parser/assign.v | 2 +- vlib/v/tests/static_vars_test.v | 2 +- 6 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 vlib/v/fmt/tests/static_mut_keep.vv diff --git a/vlib/v/checker/tests/static_vars_in_translated_mode.out b/vlib/v/checker/tests/static_vars_in_translated_mode.out index f2974febd8..18c297a3fd 100644 --- a/vlib/v/checker/tests/static_vars_in_translated_mode.out +++ b/vlib/v/checker/tests/static_vars_in_translated_mode.out @@ -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 | ~~~~~~~~ diff --git a/vlib/v/checker/tests/static_vars_in_translated_mode.vv b/vlib/v/checker/tests/static_vars_in_translated_mode.vv index 4c8b38a607..0f153c8f05 100644 --- a/vlib/v/checker/tests/static_vars_in_translated_mode.vv +++ b/vlib/v/checker/tests/static_vars_in_translated_mode.vv @@ -1,6 +1,5 @@ -[unsafe] fn counter() int { - static icounter := 0 + mut static icounter := 0 icounter++ return icounter } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 07f9bf5479..30b3a05ff6 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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) diff --git a/vlib/v/fmt/tests/static_mut_keep.vv b/vlib/v/fmt/tests/static_mut_keep.vv new file mode 100644 index 0000000000..4d4d61b94b --- /dev/null +++ b/vlib/v/fmt/tests/static_mut_keep.vv @@ -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 +} diff --git a/vlib/v/parser/assign.v b/vlib/v/parser/assign.v index f164f34931..2a11a5e83a 100644 --- a/vlib/v/parser/assign.v +++ b/vlib/v/parser/assign.v @@ -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 { diff --git a/vlib/v/tests/static_vars_test.v b/vlib/v/tests/static_vars_test.v index b1320605a2..030b0860b2 100644 --- a/vlib/v/tests/static_vars_test.v +++ b/vlib/v/tests/static_vars_test.v @@ -1,6 +1,6 @@ [unsafe] fn foo() int { - static x := 42 + mut static x := 42 x++ return x }