From e2ff2a5405d69020913e5a6cdb1dd3e794261322 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Mon, 8 Feb 2021 15:03:05 +0000 Subject: [PATCH] parser: deprecate `{var |` struct update (#8618) --- vlib/v/ast/ast.v | 2 ++ vlib/v/fmt/fmt.v | 3 +-- vlib/v/fmt/tests/struct_init_keep.vv | 5 ++++- vlib/v/gen/c/cgen.v | 4 ++-- vlib/v/parser/pratt.v | 3 ++- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index cf7499a1c0..eb9de82027 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1054,6 +1054,8 @@ pub: pos token.Position } */ + +// deprecated pub struct Assoc { pub: var_name string diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6cd64aad35..d0a3b4333f 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1136,8 +1136,7 @@ pub fn (mut f Fmt) cast_expr(node ast.CastExpr) { pub fn (mut f Fmt) assoc(node ast.Assoc) { f.writeln('{') // f.indent++ - f.writeln('\t$node.var_name |') - // TODO StructInit copy pasta + f.writeln('\t...$node.var_name') for i, field in node.fields { f.write('\t$field: ') f.expr(node.exprs[i]) diff --git a/vlib/v/fmt/tests/struct_init_keep.vv b/vlib/v/fmt/tests/struct_init_keep.vv index d996b93286..d4ec06a91e 100644 --- a/vlib/v/fmt/tests/struct_init_keep.vv +++ b/vlib/v/fmt/tests/struct_init_keep.vv @@ -7,5 +7,8 @@ fn main() { u := User{ age: 54 } - println(u) + _ = User{ + ...u + name: 'hi' + } } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 82f85debdd..66631b304b 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6240,9 +6240,9 @@ $staticprefix $interface_name* I_${cctype}_to_Interface_${interface_name}_ptr($c // params_start_pos := g.out.len mut params := method.params.clone() - first_param := params[0] // workaround, { params[0] | ... } doesn't work + // hack to mutate typ params[0] = { - first_param | + ...params[0] typ: params[0].typ.set_nr_muls(1) } fargs, _ := g.fn_args(params, false) // second argument is ignored anyway diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index e69a8e162a..f5879b98db 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -244,7 +244,8 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { node = p.map_init() } else { // it should be a struct - if p.peek_tok.kind == .pipe { + if p.tok.kind == .name && p.peek_tok.kind == .pipe { + p.warn_with_pos('use e.g. `...struct_var` instead', p.peek_tok.position()) node = p.assoc() } else if (p.tok.kind == .name && p.peek_tok.kind == .colon) || p.tok.kind in [.rcbr, .comment, .ellipsis] {