From 077e06b44eba40146c79c3381baf6e43294da0da Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 29 May 2020 21:22:27 +0300 Subject: [PATCH] fmt: fix struct field default expressions and tags --- vlib/v/fmt/fmt.v | 17 ++++++++++++++++- .../struct_default_field_expressions_keep.vv | 8 ++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/struct_default_field_expressions_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6f3f9d082c..8c601c54cf 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -463,9 +463,24 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { f.write('\t$field.name ') f.write(strings.repeat(` `, max - field.name.len)) f.write(f.type_to_str(field.typ)) + if field.attrs.len > 0 { + f.write(' [' + field.attrs.join(';') + ']') + } if field.has_default_expr { f.write(' = ') - f.expr(field.default_expr) + mut is_pe_amp_ce := false + mut ce := ast.CastExpr{} + if field.default_expr is ast.PrefixExpr { + pe := field.default_expr as ast.PrefixExpr + if pe.right is ast.CastExpr && pe.op == .amp { + ce = pe.right as ast.CastExpr + is_pe_amp_ce = true + f.expr(ce) + } + } + if !is_pe_amp_ce { + f.expr(field.default_expr) + } } // f.write('// $field.pos.line_nr') if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr { diff --git a/vlib/v/fmt/tests/struct_default_field_expressions_keep.vv b/vlib/v/fmt/tests/struct_default_field_expressions_keep.vv new file mode 100644 index 0000000000..78e6852af6 --- /dev/null +++ b/vlib/v/fmt/tests/struct_default_field_expressions_keep.vv @@ -0,0 +1,8 @@ +struct Foo { + i int = 1 +} + +struct Bar { + f Foo = &Foo(0) + z int [skip] = -1 +}